如何将Picturebox客户端保存为Bmp或Png文件? [英] How Can I Save Picturebox Client As Bmp Or Png File?

查看:328
本文介绍了如何将Picturebox客户端保存为Bmp或Png文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#winForm项目。

有一个

c# winForm project.
Has a

Windows.Forms.PictureBox pictureBox



从中加载图像一个文件作为Bmp并为其分配图像属性


Load a Image from a file as Bmp and assign image attribute to it

this->pictureBox->Image = bmp;





现在我在pictureBox上绘制一些形状。





Now I draw some shapes upon the pictureBox.

Graphics g = thisform.CreateGraphics();//DC
Pen p = new Pen(Color.Blue, 2);
g.DrawLine(p, ps, pe);
g.DrawRectangle(p, SelectRect);
...





现在问题 1:

(1)我想将整个客户端(包括形状和加载的Bmp)保存到bmp或png?



问题 2:

(2)BTW,如上所述,在图片框上已经有很多形状,如果我想为用户提供一个功能来选择单个形状并将其移除而保持其他形状完好无损。我怎么能实现它?



任何建议都将不胜感激!



问候!



Now question 1:
(1)I want to save the whole client (including shapes and the Loaded Bmp) to a bmp or png?

question 2:
(2)BTW,as above mentioned,already have many shaped on the picture-box,If I want supply a function for user to select a single shape and remove it while keep others intact. How I can i achieve it?

any advice will be appreciated!

regards!

推荐答案

您可以尝试使用以下代码保存原始图像以及您对文件所做的所有绘图:



You can try following code to save the original image plus all the drawings that you would do to a file:

Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(mybmp, pictureBox1.Bounds);
bitmap.Save("C:\\someImage.bmp");







更新:您没有使用 PictureBox 。您需要使用 Paint PictureBox 的事件处理程序,然后使用 e.Graphics 要绘制的对象。




Update: You are not drawing on the PictureBox. You need to use Paint event handler of PictureBox and then use e.Graphics object to draw.


1。如果你的目标是以某种方式保存(序列化)整个WinForms PictureBox控件,那不是直接。你可以做的是创建一个Serializable类,其中包括所有细节,如PictureBox Name,Size,BackGroundColor,当然还有Bitmap,并序列化它,然后通过反序列化来恢复它。 />


我假设你知道保存当前的Bitmap是'PictureBox的图像很简单。



2.删除任何特定内容(光栅,像素)后,无论什么对象是该内容的来源(形状,另一个位图等),一旦这些位被写入(源光栅化),就不可能删除。



为了恢复任何形状,你必须首先保留在将这个形状渲染到位图时过度写入的位,然后,如果两个形状结束,该怎么办? -lapped?



但是你可以:



a。实现一般的撤消/重做工具,您可以在其中保留位图的副本(例如,在堆栈数据结构中)并恢复它们。 使用位图撤消的典型策略包括在每个快照中仅保留在源位图的某个区域内已更改的位。



b 。使用用户添加到位图的每个形状,使用透明度创建新的位图,这样新的位图可以覆盖在当前位图的顶部,而不会删除新位图的未绘制部分。



这种合成方法意味着你基本上创建了一个多层位图。
1. If your goal is to somehow save (serialize) the entire WinForms PictureBox Control, that's not directly possible. What you could do is to create a Serializable Class that included all the details like, PictureBox Name, Size, BackGroundColor, and, of course, the Bitmap, and serialize that, and, then, restore it by de-serializing it.

I assume you know that saving the current Bitmap which is the 'Image of the PictureBox is easy.

2. Erasing any specific content (raster, pixels) in a Bitmap after whatever object was the source of that content (shape, another bitmap, etc.) is impossible once those bits have been written (the source rasterized).

For you to restore any shape, you'd have to first preserve the bits you over-wrote when you rendered that shape into the bitmap, and, then, what if two shapes over-lapped ?

You could however:

a. implement a general undo/redo facility where you'd keep copies of the bitmap (in a Stack data structure, for example) and restore them. typical strategies for 'undo with a bitmap include preserving, in each "snapshot," only the bits that have been changed within a certain region of the source bitmap.

b. with each shape the user adds to the bitmap, create a new bitmap using transparency so this new bitmap can be overlayed on top of the current bitmap without erasing non-drawn parts of the new bitmap.

this "compositing" approach means you essentially create a multi-layered bitmap.


saveFileDialog1.Filter = "JPEG File (*.jpg)|*.jpg|Bitmap File (*.bmp)|*.bmp|PNG File(*.png)|*.png";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                switch (Path.GetExtension(saveFileDialog1.FileName))
                {
                    case ".jpg":
                        pictureBox1.Image.Save(saveFileDialog1.FileName, ImageFormat.Jpeg); break;
                    case ".bmp":
                        pictureBox1.Image.Save(saveFileDialog1.FileName, ImageFormat.Bmp); break;
                    case ".png":
                        pictureBox1.Image.Save(saveFileDialog1.FileName, ImageFormat.Png); break;
                }
            }





关注:ImageFormat在System.Drawing.Imaging类中可用



例如:



attention : ImageFormat is available in "System.Drawing.Imaging" class

example:

PictureBox1.Image.Save("Address",System.Drawing.Imaging.ImageFormat.Bmp);


这篇关于如何将Picturebox客户端保存为Bmp或Png文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆