在使用BinaryFormatter序列化一些图片时,我遇到了ExternalException - A generic error occurred in GDI+"的问题。经过一番琢磨后,我决定创建一个简单的测试项目来缩小问题的范围:
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下无法运行,即使以管理员身份运行程序也无效。然而,当我将完全相同的可执行文件和图片复制到Windows XP的VMWare实例中时,却没有任何问题。
有人知道为什么在Windows 7下有些图片不能正常工作,但在XP下可以吗?
以下是其中一张图片: http://www.2shared.com/file/7wAXL88i/SO_testimage.html delme.jpg md5: 3d7e832db108de35400edc28142a8281