views:

5040

answers:

5

Hi I have a picture box on my windows forms.

I load a picture in it and I have enabled Paint Event on 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 "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 rectangle that I draw.

Does any one have an idea ?

Thankx

Tugrul

A: 

You need paint to image of picture, not to the Graphics control on Paint event.

EDIT:

using( Graphics g = Graphics.FromImage( pictureBox1.Image ) ) {
    // there you will be do, what you do in Paint event
}

// ... somewhere else ...
pictureBox1.Save( _required_parameters_ );
TcKs
HiCan you clarify/give an example ?
tguclu
+1  A: 

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

tzup
A: 

Thanks.Your anwers all helped. This worked

        private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.ImageLocation=@"C:\Documents and Settings\tr1g3800\Desktop\WALKING\30P\100000.jpg" ;
    }

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

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {

    }

    private void button3_Click(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(pictureBox1.Image);
        Graphics gr = Graphics.FromImage(bmp);
        Pen p = new Pen(Color.Red);
        p.Width = 5.0f;
        gr.DrawRectangle(p, 1, 2, 30, 40);
        pictureBox1.Image = bmp;
    }
tguclu
You shold usie the "using( ... ) { ... }" pattern. Now, you can have some memory leaks.And if some answer solve your answer, you should accept it ;).
TcKs
A: 

Hi, I am doing the same work with the zoom image, but I am using a scroll bar and it is working fine. I now need to zoom in and zoom out with a menu strip. I am a newbie in C# and any help would be appreciated.

Thanks Regards

Matt
A: 

hi how can i save an image of picturebox in a share folder on net work?

i use this code but it raises error

pictureBox1.Image.Save(@"192.168.1.39\xxxxxx", System.Drawing.Imaging.ImageFormat.Jpeg);

mohsen