Asked  6 Months ago    Answers:  5   Viewed   48 times

This seems to be a bit of an infamous error all over the web. So much so that I have been unable to find an answer to my problem as my scenario doesn't fit. An exception gets thrown when I save the image to the stream.

Weirdly this works perfectly with a png but gives the above error with jpg and gif which is rather confusing.

Most similar problem out there relate to saving images to files without permissions. Ironically the solution is to use a memory stream as I am doing....

public static byte[] ConvertImageToByteArray(Image imageToConvert)
    using (var ms = new MemoryStream())
        ImageFormat format;
        switch (imageToConvert.MimeType())
            case "image/png":
                format = ImageFormat.Png;
            case "image/gif":
                format = ImageFormat.Gif;
                format = ImageFormat.Jpeg;

        imageToConvert.Save(ms, format);
        return ms.ToArray();

More detail to the exception. The reason this causes so many issues is the lack of explanation :(

System.Runtime.InteropServices.ExternalException was unhandled by user code
Message="A generic error occurred in GDI+."
   at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters    encoderParams)
   at System.Drawing.Image.Save(Stream stream, ImageFormat format)
   at Caldoo.Infrastructure.PhotoEditor.ConvertImageToByteArray(Image imageToConvert) in C:UsersIanSVNCaldooCaldoo.CoordinatorPhotoEditor.cs:line 139
   at Caldoo.Web.Controllers.PictureController.Croppable() in C:UsersIanSVNCaldooCaldoo.WebControllersPictureController.cs:line 132
   at lambda_method(ExecutionScope , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)

OK things I have tried so far.

  1. Cloning the image and working on that.
  2. Retrieving the encoder for that MIME passing that with jpeg quality setting.



OK I seem to have found the cause just by sheer luck and its nothing wrong with that particular method, it's further back up the call stack.

Earlier I resize the image and as part of that method I return the resized object as follows. I have inserted two calls to the above method and a direct save to a file.

// At this point the new bitmap has no MimeType
// Need to output to memory stream
using (var m = new MemoryStream())
       dst.Save(m, format);

       var img = Image.FromStream(m);

       var bytes = PhotoEditor.ConvertImageToByteArray(img);

       return img;

It appears that the memory stream that the object was created on has to be open at the time the object is saved. I am not sure why this is. Is anyone able to enlighten me and how I can get around this.

I only return from a stream because after using the resize code similar to this the destination file has an unknown mime type (img.RawFormat.Guid) and Id like the Mime type to be correct on all image objects as it makes it hard write generic handling code otherwise.


This didn't come up in my initial search but here's the answer from Jon Skeet

Tuesday, June 1, 2021
answered 6 Months ago

Yes, use a ColorMatrix. It ought to look like this:

  0  0  0  0  0
  0  0  0  0  0
  0  0  0  0  0 
  0  0  0  1  0 
  R  G  B  0  1

Where R, G and B are the scaled color values of the replacement color (divide by 255.0f)

Saturday, August 14, 2021
answered 4 Months ago

To draw on a bitmap you don't want to create a Graphics object for an UI control. You create a Graphics object for the bitmap using the FromImage method:

Graphics g = Graphics.FromImage(theImage);

A Graphics object doesn't contain the graphics that you draw to it, instead it's just a tool to draw on another canvas, which is usually the screen, but it can also be a Bitmap object.

So, you don't draw first and then extract the bitmap, you create the bitmap first, then create the Graphics object to draw on it:

Bitmap destination = new Bitmap(200, 200);
using (Graphics g = Graphics.FromImage(destination)) {
   Matrix rotation = new Matrix();
   g.Transform = rotation;
   g.DrawImage(source, 0, 0, 200, 200);
Thursday, August 19, 2021
Double H
answered 4 Months ago

I had what I believe was the same problem recently. You need to skip the using statement around the creation of your MemoryStream. Creating a bitmap keeps a reference to the stream that created it. You can read about it on MSDN.

private static Bitmap LoadImageFromBytes(byte[] bytes)
    var imageStream = new MemoryStream(bytes))
    var image = (Bitmap)Bitmap.FromStream(imageStream);
    return image;
Thursday, August 19, 2021
Anass Kartit
answered 4 Months ago

As long as the image object exists that was created by loading the image from the file, the file is in use. You can't save an image with the same name while the file is in use.

Instead of using Image.FromFile to load the image, open a file stream and use Image.FromStream to create the image, then close the file stream. That way the file is no longer in use, and you can replace it.

Wednesday, August 25, 2021
RajaReddy PolamReddy
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :