A Generic Error Occurred in Gdi+, Jpeg Image to Memorystream

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

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);

//TEST
img.Save("C:\\test.jpg");
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.

EDIT

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

A generic error occurred in GDI+ while saving image to MemoryStream

As mentioned by Klaus Gütter in the comments, you are not allowed to dispose the stream before the bitmap.

One option to fix this would be to return a clone of the bitmap, that should ensure all the pixel-data is copied to a separate buffer, allowing the original stream to be freed.

using var bmp = new Bitmap(memoryStream);
images.Add(bmp.Clone(new Rectangle(0,0, bmp.Width, bmp.Height), PixelFormat.Format32bppArgb);

Another option could be to just not dispose the memory stream. Since MemoryStream just represents a chunk of managed memory it is safe to rely on the garbage collector for clean up. A good rule of thumb is to always dispose disposable objects, but this is less important when you know that the object only owns managed resources, like memory.

A Generic error occurred in GDI+ in Bitmap.Save method


When either a Bitmap object or an Image object is constructed from a
file, the file remains locked for the lifetime of the object. As a
result, you cannot change an image and save it back to the same file
where it originated.
http://support.microsoft.com/?id=814675

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

Image.Save(..) throws a GDI+ exception because the memory stream is closed

http://alperguc.blogspot.in/2008/11/c-generic-error-occurred-in-gdi.html

EDIT:

just writing from memory...

save to an 'intermediary' memory stream, that should work

e.g. try this one - replace

    Bitmap newBitmap = new Bitmap(thumbBMP);
thumbBMP.Dispose();
thumbBMP = null;
newBitmap.Save("~/image/thumbs/" + "t" + objPropBannerImage.ImageId, ImageFormat.Jpeg);

with something like:

string outputFileName = "...";
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
{
thumbBMP.Save(memory, ImageFormat.Jpeg);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}

A generic error occurred in GDI+ when saving Image

Try

stitchedImage.Save(@"D:\Naved\BasicDotNet\Images\stitchedImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

instead of

stitchedImage.Save(@"D:\Naved\BasicDotNet\Images", System.Drawing.Imaging.ImageFormat.Jpeg);

The first argument to Image.Save (String, ImageFormat) is the file name to which to save, not the directory to which to save. Previously you had done

string[] files = Directory.GetFiles(@"D:\Naved\BasicDotNet\Images");

So @"D:\Naved\BasicDotNet\Images" must be a directory, not a file.

(Incidentally, I suggest saving the stitched image to a different directory; otherwise, every time you run your code you'll stitch the previous stitch together with the previous contents.)

A generic error occurred in GDI+ when attempting to use Image.Save

Thank you to Simon Whitehead for answering this in the comments. He said, "3) Make sure the file is not in use by anything else (including your code)."

So the problem was that my own code was using the item.Image object, and was preventing GDI+ to call the dispose() method on it. The solution was to copy the object into a new object, then use that object to "Write." The resulting code is as follows:

try
{
using (Bitmap tempImage = new Bitmap(item.Image))
{
tempImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
}
}
catch (Exception e)
{
Debug.WriteLine("DEBUG::LoadImages()::Error attempting to create image::" + e.Message);
}

Reloading Img to memorystream causes a [ A generic error occurred in GDI+ ]

Thanks to Mr. @Jimi , I've managed to solve the issue.

as he so kindly pointed out, i need to dispose of the Memory stream after saving the object.

so my code was edited like this :

Image img = PictureBox.Image;
byte[] photo_aray = null;
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg);
photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
//
//
//SQL UPDATE HERE
//
//
ms.Close();
ms.Dispose();

Thanks!



Related Topics



Leave a reply



Submit