Click to See Complete Forum and Search --> : Imageprocessing:Draw and Save the drawing


zeeShan anSari
June 30th, 2007, 12:29 PM
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

Aurrin
July 2nd, 2007, 02:36 AM
One problem with that setup is that you will likely lose the 'soft copy' in the PictureBox's frame every time it repaints, which could be at more or less random intervals.

But you're on the right track. A very simple, elegant solution is to just handle the Paint event of the PictureBox like this:



private void pictureBox1_OnPaint( PaintEventArgs e )
{
e.Graphics.DrawImage( 0, 0, bufferBitmap );
}



Now, any changes you make to that buffer will automatically be drawn onto the PictureBox anytime it re-paints. Then, if you want to force an immediate update, just call the Refresh() method on the PictureBox. That will help you r code stay cleaner as well, as you won't have to draw everything in duplicate.

Also, make sure to set the PictureBox's DoubleBuffered property to true, so it won't be as likely to flicker & paint poorly.