How to save picturebox image into jpg file?

  • Thread starter Thread starter yaya via DotNetMonster.com
  • Start date Start date
Y

yaya via DotNetMonster.com

Hi, I have a picture box with circles and rectangles, and I wana save all
the images into a jpg file, i tried
pictureBox1.Image.Save(@"c:\1.jpg");
but I got and error
"System.NullReferenceException: Object reference not set to an instance of
an object."

Can anyone help? Thanks...
 
Hi yaya,

Are you painting directly on a PictureBox? (which btw is not what a
PictureBox is meant for).
You need to somehow do the painting on a Bitmap, for instance when in your
paint procedure paint on the Bitmap instead and at the end use
DrawImageUnscaled to paint the result onto some control, or put the bitmap
into the PictureBox.Image property. You can then use Image.Save(filename,
ImageFormat) to save the Bitmap to file. Use Graphics.FromImage() to get
a paint canvas for your paint procedure.
 
Ya...it works...but how can I get a blank jpeg full with black color?
Thank again :D
 
Um, do you mean something like

g.FillRectangle(Brushes.Black, 0, 0, bm.Width, bm.Height);
 
In that case I'm not sure I understand your question.
You can fill the Bitmap with black color using the FillRectangle method.
 
Ops....sorry, I have misleading you...I meant that I got a blank black
picture in jpeg instead of the picture that shown in the picturebox...as I
din draw any retangle with black color... :p
 
I'm confused, do you draw on a PictureBox that is already showing a
Picture? If so, use that Image for Graphics.FromImage, if not, please
tell us, in some detail, what you are doing now, and what you want to
accomplish.
 
Sorry for the confusion, actually I have a blank picturebox initially, and
my program will draw some points and lines...and lastly, when the save
button is clicked, the picture(points and lines) drawn will save into a
jpeg file. It works and a jpeg file was saved, but the image of the file
was a blank black image instead of what the program has drawn on the
picturebox. Are you clear now? Thankx...
 
Well, you may have overlooked a point or two, so I'm repeating the
procedure.

All the drawing you are doing, do it directly on the Bitmap.

Whenever you need to refresh the screen or display the result of the
drawing, paste the Bitmap onto the PictureBox, using
Graphics.DrawImageUnscaled(Bitmap, x, y)

When you need to save the drawing to file, you just call Image.Save on the
Bitmap you use for drawing.

Bitmap bm;

public TestForm()
{
bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// If you aren't doing any drawing here you might just paste the Bitmap

// Graphics g = Graphics.FromImage(bm);
// g.DrawString("Hello World", Brushes.Red, this.Font, 5, 5);
// g.Dispose();

e.Graphics.DrawImageUnscaled(bm, 0, 0);
}

private void button1_Click(object sender, EventArgs e)
{
bm.Save("test.jpg", ImageFormat.Jpeg);
}

private void DrawSomeStuff()
{
using(Graphics g = Graphics.FromImage(bm))
{
g.DrawString("Hello World", Brushes.Red, this.Font, 5, 5);
}
using(Graphics h = pictureBox1.CreateGraphics())
{
h.DrawImageUnscaled(bm, 0, 0);
}
}

(PS! code may contain typos as it is not tested)
 
Back
Top