Create graphic and save it as Bitmap

Hesam Qodsi picture Hesam Qodsi · May 21, 2010 · Viewed 17k times · Source

I have two questions:

1) I have a PictureBox and its Dock is set to Fill. When I resize the Form I cannot create a Graphic on the part of the PictureBox that is extended. What is the problem?

2) I want to convert the Graphic that is created on the PictureBox to Bitmap and save it as *.JPG or *.bmp. How can I do this?

Answer

Alex Pacurar picture Alex Pacurar · May 21, 2010

you can use the handle device to get the bitmap out of the picture box

Graphics g = pictureBox1.CreateGraphics();          
Bitmap bitMap = Bitmap.FromHbitmap(g.GetHdc());
bitMap.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

or even better, if the pictureBox does`nt modify the image, you can directly get the image from the pictureBox control

pictureBox1.Image.Save("path", System.Drawing.Imaging.ImageFormat.Jpeg);