“A generic error occurred in GDI+” when attempting to use Image.Save

后端 未结 6 1031
挽巷
挽巷 2020-12-11 06:21

I am developing an Outlook 2010 Add-In, and am loading an image from a serialized XML file. The image loads fine, and am able to assign it to a pictureBox object on a Winfo

相关标签:
6条回答
  • 2020-12-11 07:03


    1. Make Sure That your destination folder have read/write permission (check it twice!).
    2. Using Server.MapPath is better
    3. Make Sure you have free space on your destination drive or folder. 4. Most of the times we cant user Memory Streamers On Shared Servers, So we should be make sure that provider allow us to use it.

    Hope Microsoft Give Detailed Exception Errors instead of "Generic GDI+ Errror" !!!

    0 讨论(0)
  • 2020-12-11 07:06

    I had the same generic exception, but then I gave write permission to the IIS on parallel plesk file manager. if you are on windows server, make sure to give write permission to IIS user which was IIS_IUSR in my server

    also make sure that the folder you are trying to save is correct as mentioned in above comments

    0 讨论(0)
  • 2020-12-11 07:07

    Had this issue myself, needed to check that the folder existed, GDI didn't tell me what went wrong, would have been nice.

    0 讨论(0)
  • 2020-12-11 07:07

    In my case it was a spelling mistake in the path to the directory where I was saving the image:

    if (Directory.Exists(directory_path))
    {
     image.SaveAs(directory_path + filename);
    }
    

    As gaffleck said it would be nice if GDI+ had thrown more informative exception.

    0 讨论(0)
  • 2020-12-11 07:08

    Thank you to Simon Whitehead for answering this in the comments. He said, "3) Make sure the file is not in use by anything else (including your code)."

    So the problem was that my own code was using the item.Image object, and was preventing GDI+ to call the dispose() method on it. The solution was to copy the object into a new object, then use that object to "Write." The resulting code is as follows:

    try
    {
       using (Bitmap tempImage = new Bitmap(item.Image)) 
       {
          tempImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
       }    
    }
    catch (Exception e)
    {
        Debug.WriteLine("DEBUG::LoadImages()::Error attempting to create image::" + e.Message);
    }
    
    0 讨论(0)
  • 2020-12-11 07:08

    I too faced same error for code line:

    wmImg.Save(BrandImgPath,ImageFormat.Png);
    
    BrandImgPath = "D:/XYZ/fileName;
    

    Found cause:

    XYZ folder didn't exist in D: drive. So my code was creating this folder later. One should ensure if that path exist or not.

    if (Directory.Exists(@"D:/XYZ")) return;
    

    Hope it will help someone to solve his code mistakes.

    0 讨论(0)
提交回复
热议问题