locked
save image to PC directory RRS feed

  • Question

  • i don't know why when i  save the same picture to the same directory,it causes the error message: "A generic error occred in GDI+"??????
    but if i change another picute to save , it works properly

    my code is as belows:
     Image img = Image.FromFile(this.picBox2.ImageLocation.ToString());
                        
    img.Save(@"D:\Temp\111.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);

    how can i prevent this error when i save the same picture?

    Monday, September 20, 2010 9:56 AM

Answers

  • to set an image-variable to null, sets the reference to that image to null, it doesn't do a cleanup-process. I use it for (absence) testing only:

    if (someObject == null) ...

    Dispose should be used with any unmanaged resources like streams Images Brushes and so on.

    http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx

    http://en.wikipedia.org/wiki/Dispose_pattern

    regards,

      Thorsten

    • Marked as answer by Helen Zhou Monday, September 27, 2010 3:09 AM
    Monday, September 20, 2010 6:06 PM
  • Hi,

    you'll have to release the File first - means the Image you load from FileSystem, in your case it's the image in the picBox2:

          Image img = Image.FromFile(@"D:\Temp\111.jpeg");
          Bitmap bmp = new Bitmap(img);
          img.Dispose();
          img = null;
    
          bmp.Save(@"D:\Temp\111.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
          if (picBox2.Image != null)
          {
            picBox2.Image.Dispose();
            picBox2.Image = null;
          }
          this.picBox2.Image = bmp;
    

    regards,

      Thorsten

    • Proposed as answer by NagarjunaDilip Monday, September 20, 2010 1:21 PM
    • Marked as answer by Helen Zhou Monday, September 27, 2010 3:09 AM
    Monday, September 20, 2010 12:39 PM

All replies

  • Hi,

    you'll have to release the File first - means the Image you load from FileSystem, in your case it's the image in the picBox2:

          Image img = Image.FromFile(@"D:\Temp\111.jpeg");
          Bitmap bmp = new Bitmap(img);
          img.Dispose();
          img = null;
    
          bmp.Save(@"D:\Temp\111.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
          if (picBox2.Image != null)
          {
            picBox2.Image.Dispose();
            picBox2.Image = null;
          }
          this.picBox2.Image = bmp;
    

    regards,

      Thorsten

    • Proposed as answer by NagarjunaDilip Monday, September 20, 2010 1:21 PM
    • Marked as answer by Helen Zhou Monday, September 27, 2010 3:09 AM
    Monday, September 20, 2010 12:39 PM
  • Hi,

    you'll have to release the File first - means the Image you load from FileSystem, in your case it's the image in the picBox2:

       Image img = Image.FromFile(@"D:\Temp\111.jpeg");
    
       Bitmap bmp = new Bitmap(img);
    
       img.Dispose();
    
       img = null;
    
    
    
       bmp.Save(@"D:\Temp\111.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
    
       if (picBox2.Image != null)
    
       {
    
        picBox2.Image.Dispose();
    
        picBox2.Image = null;
    
       }
    
       this.picBox2.Image = bmp;
    
    

    regards,

      Thorsten


    you mean i need to dispose the image

    what is different dispose image and set image to null

    Monday, September 20, 2010 3:51 PM
  • to set an image-variable to null, sets the reference to that image to null, it doesn't do a cleanup-process. I use it for (absence) testing only:

    if (someObject == null) ...

    Dispose should be used with any unmanaged resources like streams Images Brushes and so on.

    http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx

    http://en.wikipedia.org/wiki/Dispose_pattern

    regards,

      Thorsten

    • Marked as answer by Helen Zhou Monday, September 27, 2010 3:09 AM
    Monday, September 20, 2010 6:06 PM