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

后端 未结 3 1215
清酒与你
清酒与你 2021-01-14 10:35

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 awhi

3条回答
  •  忘掉有多难
    2021-01-14 10:43

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

提交回复
热议问题