Introduction
Providing solution to very common issue "
A generic error occurred in GDI+." while working with ASP.Net MVC C# application. In this application, I was writing code to save image in Png format. Sometimes, we don't have any exact idea about issue, then we can wastage our precious development time, to overcome this issue, I come up here with to resolve this issue.
Problem Where Encountered
img1.Save(Server.MapPath("/Upload/test.png", System.Drawing.Imaging.ImageFormat.Png);
Few other more reasons to arise issue A generic error occurred in GDI+
- This error can also comes out while upload folder don't have permissions to save image.
- If we are creating image from bytes and bytes are locked then this also throws relevant exception.
- Check for the same file/image exists in the same folder.
- Always use dispose method to deallocate memory while image/file generating from bytes array.
![A generic error occurred in GDI A generic error occurred in GDI]() |
Fig: A generic error occurred in GDI |
Conclusion
I was saving png image under
Upload folder, unfortunately Upload folder was not available in MVC app location, so I needed to create a new
Upload folder to upload png image to that specific folder, this is the way, how we can remove error
A generic error occurred in GDI.