Saving image to file

219,181

Solution 1

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);
}

Solution 2

You can try with this code

Image.Save("myfile.png", ImageFormat.Png)

Link : http://msdn.microsoft.com/en-us/library/ms142147.aspx

Solution 3

If you are drawing on the Graphics of the Control than you should do something draw on the Bitmap everything you are drawing on the canvas, but have in mind that Bitmap needs to be the exact size of the control you are drawing on:

  Bitmap bmp = new Bitmap(myControl.ClientRectangle.Width,myControl.ClientRectangle.Height);
  Graphics gBmp = Graphics.FromImage(bmp);
  gBmp.DrawEverything(); //this is your code for drawing
  gBmp.Dispose();
  bmp.Save("image.png", ImageFormat.Png);

Or you can use a DrawToBitmap method of the Control. Something like this:

Bitmap bmp = new Bitmap(myControl.ClientRectangle.Width, myControl.ClientRectangle.Height);
myControl.DrawToBitmap(bmp,new Rectangle(0,0,bmp.Width,bmp.Height));
bmp.Save("image.png", ImageFormat.Png);

Solution 4

You can save image , save the file in your current directory application and move the file to any directory .

 Bitmap btm = new Bitmap(image.width,image.height);
    Image img = btm;
                        img.Save(@"img_" + x + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                        FileInfo img__ = new FileInfo(@"img_" + x + ".jpg");
                        img__.MoveTo("myVideo\\img_" + x + ".jpg");
Share:
219,181
Victor
Author by

Victor

Thrilled to learn new things. Love programming, coffee and mountain bikes.

Updated on July 09, 2022

Comments

  • Victor
    Victor almost 2 years

    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.

  • Steve
    Steve almost 10 years
    @Joel Right, you don't appreciate Intellisense enough until you write code like this by hand
  • Paul Zahra
    Paul Zahra about 8 years
    Why haven't you disposed of bmp object? Couldn't this leave the file in an 'open' state?
  • Lexy Feito
    Lexy Feito over 7 years
    where would this image be saved?
  • Steve
    Steve over 7 years
    Any place indicated by the property dialog.Filename
  • Aaron Franke
    Aaron Franke over 5 years
    Note: Not static, you have to use objectName.Save()