saving images fro mpicturebox..

  • Thread starter Thread starter Serdar C.
  • Start date Start date
S

Serdar C.

hi again.. i wrote a simple "paint" program which u can paint a picture on a
picture box and then save it to a bmpof jpg file..
my problem is whenever i try to save the file program throws
"'System.NullReferenceException".

i save the picture like this:
"picturebox1.image.save("temp.bmp",imageformat.bmp)

and also drawing the pictuer on the picturebox like this:

Mousedown event:

if(e.Button == MouseButtons.Left)
{
Graphics g = pictureBox1.CreateGraphics();
g.DrawEllipse(new Pen(Color.Black,5),e.X,e.Y,5,5);
}

i tried everything i know (and also found in google) but it still throws the
same exception. plase help :(

p.s: thanx for answering my everlasting questions by spending your time..
p.s2: im using this newsteller from outlook express and i cant reply from
there.. do u know a way to do that? thanx for spending your time agian. have
a nice day.
 
You are drawing on the canvas of the picturebox, but trying to save the
Image from the picturebox. That is the cause of the problem.

Instead of drawing straight to the canvas, try this:

In your form constructor,

pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);

In your event handler,

if (e.Button == MouseButtons.Left)
{
Graphics g = Graphics.FromImage(pictureBox1.Image);
g.DrawEllipse(new Pen(Color.Black, 5), e.X, e.Y, 5, 5);
g.Dispose();
}

And now, you can save the image,

pictureBox1.Image.Save(...);

-vJ
 
thanx for helping its working now :)


Vijaye Raji said:
You are drawing on the canvas of the picturebox, but trying to save the
Image from the picturebox. That is the cause of the problem.

Instead of drawing straight to the canvas, try this:

In your form constructor,

pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);

In your event handler,

if (e.Button == MouseButtons.Left)
{
Graphics g = Graphics.FromImage(pictureBox1.Image);
g.DrawEllipse(new Pen(Color.Black, 5), e.X, e.Y, 5, 5);
g.Dispose();
}

And now, you can save the image,

pictureBox1.Image.Save(...);

-vJ
 
Back
Top