WinXP Only Bitmap.Save ArgumentException参数无效

时间:2012-11-21 06:48:32

标签: c#-4.0 gdi+ bit-manipulation

基本上,代码尝试获取源图像,在其上绘制一些自定义文本并将新图像保存到文件系统。

当我在Windows 7中运行代码时,它运行正常,但是当我在WinXP中运行它时,它会在第一个DrawString之后的任何时候在imgCopy.Save行中创建一个异常。

异常是ArgumentException(参数无效)。这就像DrawString破坏了WinXP下的图像......?

构建适用于x86 / .NET 4.0运行时。有什么想法为什么XP下的例外?

// imgSrc is actually passed into the method with the rec object
// this is just for repro
using (var imgSrc = new System.Drawing.Bitmap(rec.SrcFile))
using (var imgCopy = imgSrc.Clone() as Bitmap)
using (var gImg = Graphics.FromImage(imgCopy)) //shorten var names for this post
{
    imgCopy.Save(rec.DstFile, ImageFormat.Jpeg); //Happy here
    gImg.SmoothingMode = SmoothingMode.AntiAlias;
    imgCopy.Save(rec.DstFile, ImageFormat.Jpeg); //Also no problem
    gImg.DrawString(rec.Name, fntArial16, Brushes.Black, new Rectangle(170, 105, 650, 50), sfCenter);
    imgCopy.Save(rec.DstFile, ImageFormat.Jpeg); //<-- Fails here
}

编辑:参数代码:

private static Font fntArial16 = new Font("Arial", 16, FontStyle.Bold);

private static StringFormat _sfCenter;
private static StringFormat sfCenter {
    get {
        if (_sfCenter == null) {
            _sfCenter = new StringFormat();
            sfCenter.Alignment = StringAlignment.Center;
            sfCenter.LineAlignment = StringAlignment.Center;
        }
        return _sfCenter;
    }
}

1 个答案:

答案 0 :(得分:1)

我们将问题缩小到包含XMP(可扩展元数据平台)数据的.jpg文件。一旦我们从文件中删除它,它就能正确地在WinXP上运行。不幸的是,生成该文件的工具没有选择将其排除,所以我们改为使用.png文件,这也很好。

相关问题