Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Lets say that I have fetched an Image called img from my Database and Now I want to save it in my Local.

I am getting below Error for same.

Solving: A generic error occurred in GDI+. in C#


Is there anyone who had faced this kind of stuff and was able to save the image finally in local ?

Below is the code

public static void SavePic(Image img)
        {
            Image result = null;
            ImageFormat format = ImageFormat.Png;
            result = img;

            using (Image imageToExport = result)
            {
                string filePath = string.Format(@"C:\Vaibhav Personal.{0}", format.ToString());

                imageToExport.Save(filePath, format);

            }

        }


What I have tried:

Tried Googling, Found many answers but none of them served good in my case
Posted
Updated 27-Jan-19 3:14am
Comments
F-ES Sitecore 27-Jan-19 10:20am    
Try saving to a folder rather than the root of the c drive.

1 solution

Start here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] and check exactly what it does. Then check your save code, and the DB content.

If that all looks good, use the debugger to look at exactly what is happening in your method - you use of using there is ... um ... different and is likely to cause other problems later, since it will force a Dispose on the Image that both imageFormat and img (and by extension the outside world that calls this method) are referring to. using blocks should be used for new instances, not existing ones.
 
Share this answer
 
Comments
vaibhav1800 27-Jan-19 12:57pm    
The Probelem was Lazy Loading. So We were saving an image before it was created.
To solve this, I made another Image Type variable called copy and assigned my original img image to copy and then save the copy image file.

Sharing the code. Do let me know for any more clarifications. Thanks a lot Griff !

public static void SavePic(Image img,string Filename)
{
string fileName = Filename;
string saveDirectory = @"C:\Personal";
if (!Directory.Exists(fileName))
{
Directory.CreateDirectory(fileName);
}
string fileSavePath = Path.Combine(saveDirectory, fileName);
Image copy = img;
copy.Save(fileSavePath, ImageFormat.Jpeg);

}
OriginalGriff 27-Jan-19 13:36pm    
You're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900