Search code examples
c#.netpicturebox

How to save a picturebox control as a jpeg file after it's edited


I have a PictureBox on my Windows Forms application.

I load a picture in it and I have enabled the Paint event in my code. It draws a rectangle.

Like this:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics gr = e.Graphics;
    Pen p = new Pen(Color.Red);
    p.Width = 5.0f;
    gr.DrawRectangle(p, 1, 2, 30, 40);
}

And I click the "save" button:

private void button2_Click(object sender, EventArgs e)
{
    pictureBox1.Image.Save(@"C:\Documents and Settings\tr1g3800\Desktop\WALKING\30P\100000test.jpg",ImageFormat.Jpeg);
}

But the saved file never contains the rectangle that I drew.

Does anyone have any idea?


Solution

  • You probably shouldn't draw directly on the PictureBox.

    You need to use a Bitmap instead. Try putting the bitmap in the PictureBox.Image and then call Save().

    Check this for more details