Click here to Skip to main content
14,699,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
c# winForm project.
Has a
Windows.Forms.PictureBox pictureBox

Load a Image from a file as Bmp and assign image attribute to it
this->pictureBox->Image = bmp;


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


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!
Posted
Updated 21-Dec-14 20:49pm
v2
Comments
Kornfeld Eliyahu Peter 22-Dec-14 2:17am
   
Form has a method - DrawToBitmap = that may help you...

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



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.
   
v2
Comments
jiazhiqiang 22-Dec-14 3:23am
   
hi d@nish!
I have tried you method.it didn't works completely.It really export the bmp of picture-box but only the background image not include the shapes I draw. any hint?
lw@zi 22-Dec-14 3:24am
   
Updated response.
jiazhiqiang 22-Dec-14 3:57am
   
I will try,but by now I draw shape by :

Graphics g= pictureBox->CreateGraphics();
g.DrawLine(...)
inside form's member function.
You said "i'm not draw on the PictureBox",what's meaning? what's the different with
Paint of pictureBox?
lw@zi 22-Dec-14 4:22am
   
Have you handled Paint event for PictureBox? You should not be creating Graphics object when you are Painting anything in windows form.
jiazhiqiang 22-Dec-14 4:58am
   
no.
I used form event handler for pictureBox .e.g pictureBoxViewedImage_MouseUp(),MouseDown() ,MouseMove().finally ,To accomplish the drawing at MouseUp()
by following snippet:

Graphics^ g= pictureBoxViewedImage->CreateGraphics();
Pen^ pen1 = gcnew Pen(Color::Blue, 2);
g->DrawLine(pen1, pt1, pt2);

got ?
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.
   
Comments
BillWoodruff 22-Dec-14 23:31pm
   
Hi, Merry Xmas !

Any practical solution to adding undo/redo ... which is what you are describing ... to operations that create raster (paint) is going to involve creating a new "transparent" layer (to use the PhotoShop term which is pretty much a standard) on which you draw only the new bits, and then placing that layer over the "original" without merging them.

You then merge any or all of the "layers" when the user initiates that action via some user-interface you provide, or when you, for example, print. SnagIt, and every other graphics program with Undo, implements this.

If you are using the Paint Event in a WinForms Application to draw, everything you draw is "burned in" to the current image. Saving the current Image in a PictureBox should show its current graphic state including anything you've drawn on it. The only exception to this I can think of would be if you were using Visual Basic's 'Shapes library in C# (which is possible).

Taking a snapshot of the current Bitmap Image is easy, and you just need to copy the bits in the ClientRectangle of the PictureBox.
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;
                }
            }


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

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900