c# - Intermittent System.Drawing/GDI+ "Generic" Error -


i've got aspx page renders whatever in querystring vertical text , returns png. works great.

i have 1 customer has run issue. every few days page stops working , throws dreaded gdi+ "generic" error.

error: system.runtime.interopservices.externalexception (0x80004005): generic error occurred in gdi+. @ system.drawing.image.save(stream stream, imagecodecinfo encoder, encoderparameters encoderparams) @ system.drawing.image.save(stream stream, imageformat format) ... 

i don't know why error comes up, or why goes away. able throw testing aspx file installation ran similar code few variations see if pinpoint problem. discovered if changed imageformat png jpeg error went away.

i conceivably change product render jpegs instead of pngs. have no way of knowing if start causing errors intermittently now.

anybody know might cause problem this? thanks! code below.

update: customer server windows server 2008 r2 box running iis 7.5 , application runs on .net 4.0.

    protected void page_load(object sender, eventargs e)     {         byte[] image = getimagebytes(this.text);         if (image != null)         {             response.clearcontent();             response.clearheaders();             response.contenttype = "image/png";             response.outputstream.write(image, 0, image.length);         }     }      private byte[] getimagebytes(string text)     {         var font = new font("tahoma", 11, fontstyle.bold, graphicsunit.pixel);          // create image size of text writing         bitmap img = new bitmap(1,1);         var graphics = graphics.fromimage(img);         int width = (int)graphics.measurestring(text, font).width;         int height = (int)graphics.measurestring(text, font).height;         img = new bitmap(img, new size(width, height));          // draw text onto image         graphics = graphics.fromimage(img);         graphics.clear(color.transparent);         graphics.smoothingmode = system.drawing.drawing2d.smoothingmode.antialias;         graphics.textrenderinghint = system.drawing.text.textrenderinghint.antialias;         graphics.compositingquality = system.drawing.drawing2d.compositingquality.highquality;         graphics.drawstring(text, font, new solidbrush(color.black), 0, 0);         graphics.flush();          // rotate image vertical         img.rotateflip(rotatefliptype.rotate270flipnone);          var stream = new system.io.memorystream();         img.save(stream, imageformat.png);         stream.position = 0;         return stream.toarray();     } 

i flush issue

forces execution of pending graphics operations , returns without waiting operations finish.

this member overloaded. complete information member, including syntax, usage, , examples, click name in overload list.

can try see if still have issue?

    private byte[] getimagebytes(string text)     {         using (var font = new font("tahoma", 20, fontstyle.bold, graphicsunit.pixel))         using (var img = new bitmap(1, 1))         {             int width;             int height;             using (var graphics = graphics.fromimage(img))             {                 width = (int)graphics.measurestring(text, font).width;                 height = (int)graphics.measurestring(text, font).height;             }             using (var realimg = new bitmap(img, new size(width, height)))             {                 using (var graphics = graphics.fromimage(realimg))                 {                     graphics.clear(color.transparent);                     graphics.smoothingmode = system.drawing.drawing2d.smoothingmode.antialias;                     graphics.textrenderinghint = system.drawing.text.textrenderinghint.antialias;                     graphics.compositingquality = system.drawing.drawing2d.compositingquality.highquality;                     graphics.drawstring(text, font, new solidbrush(color.black), 0, 0);                 }                  realimg.rotateflip(rotatefliptype.rotate270flipnone);                  using (var stream = new system.io.memorystream())                 {                     realimg.save(stream, system.drawing.imaging.imageformat.png);                     stream.position = 0;                     return stream.toarray();                 }             }         }     } 

Comments