Basically the code is trying to take a source image, draw some custom text on it and save the new image to the file system.
When I run the code in Windows 7, it runs fine, but when I run it in WinXP it creates an exception in the imgCopy.Save line anytime after the first DrawString.
The exception is ArgumentException (parameter is not valid). It’s like the DrawString corrupts the image under WinXP…?
The build is for the x86/.NET 4.0 runtime. Any ideas why the exception under 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
}
Edit: Code for the parameters:
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;
}
}
We narrowed the problem down to the .jpg file containing XMP (Extensible Metadata Platform) data. Once we removed that from the file it worked on WinXP correctly. Unfortunately the tool that generated the file didn’t have the option to exclude this, so we went with the .png file instead and that works fine as well.