Saving image to file

Victor picture Victor · Oct 16, 2012 · Viewed 186.2k times · Source

I am working on a basic drawing application. I want the user to be able to save the contents of the image.

enter image description here

I thought I should use

System.Drawing.Drawing2D.GraphicsState img = drawRegion.CreateGraphics().Save();

but this does not help me for saving to file.

Answer

Steve picture Steve · Oct 16, 2012

You could try to save the image using this approach

SaveFileDialog dialog = new SaveFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
   int width = Convert.ToInt32(drawImage.Width); 
   int height = Convert.ToInt32(drawImage.Height); 
   Bitmap bmp = new Bitmap(width,height);        
   drawImage.DrawToBitmap(bmp, new Rectangle(0, 0, width, height);
   bmp.Save(dialog.FileName, ImageFormat.Jpeg);
}