Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BinaryFormatter.Serialize( Image ) - ExternalException - A generic error occurred in GDI+

Tags:

c#

gdi+

When I try to Serialize some images using the BinaryFormatter, I'll get a ExternalException - A generic error occurred in GDI+." After scratching my head for awhile, I decided to create a simple test project to narrow down the problem:

    static void Main(string[] args)
    {
        string file = @"C:\temp\delme.jpg";

        //Image i = new Bitmap(file);
        //using(FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))

        byte[] data = File.ReadAllBytes(file);
        using(MemoryStream originalms = new MemoryStream(data))
        {
            using (Image i = Image.FromStream(originalms))
            {
                BinaryFormatter bf = new BinaryFormatter();

                using (MemoryStream ms = new MemoryStream())
                {
                    // Throws ExternalException on Windows 7, not Windows XP
                    bf.Serialize(ms, i);
                }
            }
        }
    }

For specific images, I've tried all sorts of ways of loading the image and I could not get it to work under Windows 7, even when running the program as Administrator.

I've copied the exact same executable and image into my Windows XP VMWare instance and I have no problems.

Anyone have any idea of why for some images it doesn't work under Windows 7, but works under XP?


Here's one of the images: http://www.2shared.com/file/7wAXL88i/SO_testimage.html

delme.jpg md5: 3d7e832db108de35400edc28142a8281

like image 288
Generic Comrade Avatar asked Nov 23 '25 11:11

Generic Comrade


1 Answers

As the OP pointed out, the code provided throws an exception that seems to be occurring only with the image he provided but works fine with other images on my machine.

Option 1

static void Main(string[] args)
{
    string file = @"C:\Users\Public\Pictures\delme.jpg";

    byte[] data = File.ReadAllBytes(file);
    using (MemoryStream originalms = new MemoryStream(data))
    {
        using (Image i = Image.FromStream(originalms))
        {
            BinaryFormatter bf = new BinaryFormatter();

            using (MemoryStream ms = new MemoryStream())
            {
                // Throws ExternalException on Windows 7, not Windows XP                        
                //bf.Serialize(ms, i);

                i.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); // Works
                i.Save(ms, System.Drawing.Imaging.ImageFormat.Png); // Works
                i.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); // Fails
            }    
         }
     }
}

It could be that the image in question was created with a tool that added some additional information that is interfering with the JPEG serialization.

P.S. The image can be saved to memory stream using BMP or PNG format. If changing the format is an option, then you can try out either of these or any other format defined in ImageFormat.

Option 2 If your goal is just to get the contents of the image file into a memory stream, then doing just the following would help

static void Main(string[] args)
{
    string file = @"C:\Users\Public\Pictures\delme.jpg";
    using (FileStream fileStream = File.OpenRead(file))
    {
        MemoryStream memStream = new MemoryStream();
        memStream.SetLength(fileStream.Length);
        fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
    }
}
like image 77
Patrick D'Souza Avatar answered Nov 25 '25 05:11

Patrick D'Souza



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!