0
Answer

Imageprocessing:Draw and Save the drawing

Ask a question

Hi,

By your help I able to draw the any shape into the pictureBox and able to save the drawing.

It is a simple way to save any image from the pictureBox

pictureBox2.Image.Save(saveFileDialog1.FileName);

but above command do work when any image is already load onto the pictureBox.otherwise some error occur like that:

 “NullReferenceExpection was unhandled !”

 “Object reference not set to an instance of an object.”

 

 

“this is because you can't actually draw inside a picturebox. Although it may look like you're doing so, you are in fact drawing 'on top of' the picturebox and the image is not changed. So, if you haven't loaded an image, then the Image property will still be null.”

 

If you use a picturebox, put a picture in it, and draw on it first. Then you can save it. CreateGraphics should be used only for drawing things you want to be able to erase, such as rubber bands. It is not for persistent drawing, and certainly it makes your picture box a waste of time, it never does anything”


Solution of above problem is that

 

“You need to make the graphics drawn on the PictureBox to be a persistent bitmap then only you can save the image.
Use a bitmap to draw the images upon and then reflect it upon your pictureboc. When you want to save just save the bitmap.”

So, I use follwing command to draw any thing

 

Bitmap bmp = new Bitmap(pictureBox.Width, pictureBox.Height);
Graphics g1 = Graphics.FromImage(bmp);

Graphics  g2 = pictureBox.CreateGraphics();
Pen p = new Pen(Color.Red, 5);
g1.DrawEllipse(p, e.X, e.Y, 50, 70);

g2.DrawEllipse(p, e.X, e.Y, 50, 70); //etc…

 

And use follwing command for save the drawing

 

if (saveFileDialog1.ShowDialog() == DialogResult.OK)

            {

              

                bmp.Save(saveFileDialog1.FileName);

               

            }

 

Am I success???

You notice that I use both bitmap and pictureBox at a time


Bitmap bmp = new Bitmap(pictureBox.Width, pictureBox.Height);

Graphics g1 = Graphics.FromImage(bmp);


Graphics 
g2 = pictureBox.CreateGraphics();


g1.DrawEllipse(p, e.X, e.Y, 50, 70);


g2.DrawEllipse(p, e.X, e.Y, 50, 70);

 

I used bitmap for save(hard copy) the drawing and pictureBox used for seeing(soft copy) the image from the screen ………Am I right?

 

Many Thanks