BinaryFormatter.Serialize(Image)-ExternalException-GDI +中发生一般错误 [英] BinaryFormatter.Serialize( Image ) - ExternalException - A generic error occurred in GDI+

查看:93
本文介绍了BinaryFormatter.Serialize(Image)-ExternalException-GDI +中发生一般错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用BinaryFormatter序列化一些图像时,我将收到一个ExternalException-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);
                }
            }
        }
    }

对于特定的图像,我尝试了各种加载图像的方法,即使以管理员身份运行程序,我也无法使其在Windows 7下正常工作.

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.

我已经将完全相同的可执行文件和映像复制到Windows XP VMWare实例中,并且没有问题.

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

任何人都知道为什么某些图像在Windows 7下不能运行,而在XP下可以运行吗?

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

以下是图像之一: http://www.2shared.com/file/7wAXL88i/SO_testimage.html

delme.jpg md5:3d7e832db108de35400edc28142a8281

delme.jpg md5: 3d7e832db108de35400edc28142a8281

推荐答案

正如OP所指出的那样,提供的代码引发了一个异常,该异常似乎仅发生在他提供的图像上,但可以与我的计算机上的其他图像很好地工作.

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.

选项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
            }    
         }
     }
}

问题的图像可能是使用添加了一些干扰JPEG序列化的其他信息的工具创建的.

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.图像 可以使用 BMP PNG 格式保存到内存流中.如果可以更改格式,则可以尝试使用这些格式或 ImageFormat 中定义的任何其他格式.

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.

选项2 如果您的目标只是将图像文件的内容放入内存流中,那么只需执行以下操作即可

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

这篇关于BinaryFormatter.Serialize(Image)-ExternalException-GDI +中发生一般错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆