Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a generic error occurred in gdi+. while saving an image

Tags:

asp.net

Dear Expert i am getting an error while saving an image the code is as follows

  ClsImageManager objImgManager = new ClsImageManager();
  Bitmap ImageBitmap = objImgManager.GetBitmapFromBytes(ImageData);
  Response.ContentType = "image/tiff";
  ImageBitmap.Save(Response.OutputStream, ImageFormat.Tiff);
  ImageBitmap.Dispose();
  Response.End();

when i used Image.format.jpeg the code is working good but when i changes it to ImageFormat.Tiff then i am getting an error a generic error occurred in gdi+.

like image 848
naval Avatar asked Dec 27 '25 16:12

naval


2 Answers

You should note that GDI/GDI+ (System.Drawing namespace) is not officially supported in ASP.NET - see "Caution" in http://msdn.microsoft.com/en-us/library/system.drawing.aspx.
WIC is supposed to be used instead GDI+ (see http://weblogs.asp.net/bleroy/archive/2009/12/10/resizing-images-from-the-server-using-wpf-wic-instead-of-gdi.aspx)

Said that, many had successfully use GDI+ in ASP.NET. Most probably, you should attempt saving image into memory stream (or on file) and then writing saved image into the response. See this link for details: http://www.west-wind.com/weblog/posts/2006/Oct/19/Common-Problems-with-rendering-Bitmaps-into-ASPNET-OutputStream

Another work-around can be related to user account. Apparently, GDI/GDI+ is bound to device context (screen, printer etc) and they may not be available under service accounts. So you may try running your ASP.NET code on some normal user account if that helps or not.

like image 64
VinayC Avatar answered Dec 31 '25 17:12

VinayC


You may need to try explicitly encoding the image save.
Have a look at the code example at the bottom of this MSDN documentation on Image.Save
Image.Save Method (String, ImageCodecInfo, EncoderParameters)
The same actions can be applied to your save.

However, it could also possibly be that your objImgManager is disposing of the buffer where the image is stored before you can save it.

Bitmap ImageBitmap = objImgManager.GetBitmapFromBytes(ImageData);

You can get around this by creating a copy of the image by doing this:

Bitmap ImageBitmap = new Bitmap(objImgManager.GetBitmapFromBytes(ImageData));
like image 24
Nanhydrin Avatar answered Dec 31 '25 17:12

Nanhydrin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!