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

后端 未结 6 1047
挽巷
挽巷 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: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);
    }
    

提交回复
热议问题