基本上,代码正在尝试获取源图像,在其上绘制一些自定义文本并将新图像保存到文件系统。
当我在 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;
}
}