c# - A generic error occurred in GDI+, JPEG Image to MemoryStream -


this seems bit of infamous error on web. so have been unable find answer problem scenario doesn't fit. exception gets thrown when save image stream.

weirdly works png gives above error jpg , gif rather confusing.

most similar problem out there relate saving images files without permissions. ironically solution use memory stream doing....

public static byte[] convertimagetobytearray(image imagetoconvert) {     using (var ms = new memorystream())     {         imageformat format;         switch (imagetoconvert.mimetype())         {             case "image/png":                 format = imageformat.png;                 break;             case "image/gif":                 format = imageformat.gif;                 break;             default:                 format = imageformat.jpeg;                 break;         }          imagetoconvert.save(ms, format);         return ms.toarray();     } } 

more detail exception. reason causes many issues lack of explanation :(

system.runtime.interopservices.externalexception unhandled user code message="a generic error occurred in gdi+." source="system.drawing" errorcode=-2147467259 stacktrace:    @ system.drawing.image.save(stream stream, imagecodecinfo encoder, encoderparameters    encoderparams)    @ system.drawing.image.save(stream stream, imageformat format)    @ caldoo.infrastructure.photoeditor.convertimagetobytearray(image imagetoconvert) in c:\users\ian\svn\caldoo\caldoo.coordinator\photoeditor.cs:line 139    @ caldoo.web.controllers.picturecontroller.croppable() in c:\users\ian\svn\caldoo\caldoo.web\controllers\picturecontroller.cs:line 132    @ lambda_method(executionscope , controllerbase , object[] )    @ system.web.mvc.actionmethoddispatcher.execute(controllerbase controller, object[] parameters)    @ system.web.mvc.reflectedactiondescriptor.execute(controllercontext controllercontext, idictionary`2 parameters)    @ system.web.mvc.controlleractioninvoker.invokeactionmethod(controllercontext controllercontext, actiondescriptor actiondescriptor, idictionary`2 parameters)    @ system.web.mvc.controlleractioninvoker.<>c__displayclassa.<invokeactionmethodwithfilters>b__7()    @ system.web.mvc.controlleractioninvoker.invokeactionmethodfilter(iactionfilter filter, actionexecutingcontext precontext, func`1 continuation)  innerexception:  

ok things have tried far.

  1. cloning image , working on that.
  2. retrieving encoder mime passing jpeg quality setting.

ok seem have found cause sheer luck , nothing wrong particular method, it's further call stack.

earlier resize image , part of method return resized object follows. have inserted 2 calls above method , direct save file.

// @ point new bitmap has no mimetype // need output memory stream using (var m = new memorystream()) {        dst.save(m, format);         var img = image.fromstream(m);         //test        img.save("c:\\test.jpg");        var bytes = photoeditor.convertimagetobytearray(img);          return img;  } 

it appears memory stream object created on has open @ time object saved. not sure why is. able enlighten me , how can around this.

i return stream because after using resize code similar this destination file has unknown mime type (img.rawformat.guid) , id mime type correct on image objects makes hard write generic handling code otherwise.

edit

this didn't come in initial search here's answer jon skeet


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -