Navigation

Fix GDI: A generic error occurred in GDI in MVC Application

A generic error occurred in GDI+, GDI+ Generic Error ASP.NET MVC, A generic error occurred in GDI+ saving image,

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+

  1. This error can also comes out while upload folder don't have permissions to save image.
  2. If we are creating image from bytes and bytes are locked then this also throws relevant exception.
  3. Check for the same file/image exists in the same folder.
  4. Always use dispose method to deallocate memory while image/file generating from bytes array.
    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.

Suggested Reading

Share

Anjan Kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

0 comments: