using (Image img = Image.FromFile(savePath))
{
//如果原图片是索引像素格式之列的,则需要转换
if (img.PixelFormat!=null)
{
Bitmap bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmp))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(img, 0, 0, bmp.Width, bmp.Height);
Font f = new Font("微软雅黑", 32);
Brush b = new SolidBrush(Color.FromArgb(150, 255, 0, 0));
string addText = "仅供预览,
此件打印无效";
StringFormat Format = new StringFormat();
Format.Alignment = StringAlignment.Center; //居中
Rectangle rec = new Rectangle(0, 0, img.Width, img.Height);
Format.LineAlignment = StringAlignment.Center;
g.DrawString(addText, f, b, rec, Format);
g.Dispose();
bmp.Save(savePath + "_");
}
}
else
{
System.Drawing.Image image = System.Drawing.Image.FromFile(savePath);
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f = new Font("微软雅黑", 32);
Brush b = new SolidBrush(Color.FromArgb(150, 255, 0, 0));
string addText = "仅供预览,
此件打印无效";
StringFormat Format = new StringFormat();
Format.Alignment = StringAlignment.Center; //居中
Format.LineAlignment = StringAlignment.Center; //居中
Rectangle rec = new Rectangle(0, 0, image.Width, image.Height);
g.DrawString(addText, f, b, rec, Format);
g.Dispose();
image.Save(savePath + "_");
}
}
效果如下