28

I have this code

    private void saveImage()
    {
        Bitmap bmp1 = new Bitmap(pictureBox.Image);
        bmp1.Save("c:\\t.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        // Dispose of the image files.
        bmp1.Dispose();
    }

i already have an image t.jpg at my drive "c:\".
i wanted to replace it with a new image every time my program runs. but a GDI+ error shows up
how could i fix it?

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
Ozarraga_AB
  • 939
  • 5
  • 15
  • 24

2 Answers2

42

You must remove your image if that is already exists.

private void saveImage()
    {
        Bitmap bmp1 = new Bitmap(pictureBox.Image);

       if(System.IO.File.Exists("c:\\t.jpg"))
              System.IO.File.Delete("c:\\t.jpg");

        bmp1.Save("c:\\t.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        // Dispose of the image files.
        bmp1.Dispose();
    }
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
5

I presume you earlier loaded the c:\t.jpg image using the Image.Load method. If so, the Image object is holding an open file handle on the image file, which means that the file can't be overwritten.

Instead of using Image.Load to get the original image, load it from a FileStream that you create and dispose of.

So, instead of

Image image = Image.Load(@"c:\\t.jpg");

do this:

using(FileStream fs = new FileStream(@"c:\\t.jpg", FileMode.Open))
{
    pictureBox.Image = Image.FromStream(fs);
    fs.Close();
}

The file handle has been released so overwriting the file with Bitmap.Save can succeed. The code you gave in your question should therefore work. There is no need to delete the original file or dispose of the image before saving.

Additional: If you close the FileStream as above,then calls to Image.Save will throw an exception. See here: A Generic error occurred in GDI+ in Bitmap.Save method

Adrian Bhagat
  • 85
  • 1
  • 7