且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将Picturebox客户端保存为Bmp或Png文件?

更新时间:2021-09-02 21:18:39

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



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);