分享一段,图片加水印的代码。本人修改过多次
/// <summary> /// 图片加水印文字 /// </summary> /// <param name="imgPath">原图片路径</param> /// <param name="newImgPath">新图片路径</param> /// <param name="text">水印文字</param> /// <param name="Alpha">透明度</param> /// <param name="fontsize">字体大小</param> public static bool PicAddWaterText(string imgPath,string newImgPath, string text, int Alpha = 1, int fontsize = 30) { try { //text = text + "仅限xx使用"; //System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(oldstream);//文件流 System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(imgPath); int imgPhotoWidth = imgPhoto.Width; int imgPhotoHeight = imgPhoto.Height; Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72, 72); Graphics gbmPhoto = Graphics.FromImage(bmPhoto); //gif背景色 gbmPhoto.Clear(Color.FromName("White")); gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight), 0, 0, imgPhotoWidth, imgPhotoHeight, GraphicsUnit.Pixel); imgPhoto.Dispose(); System.Drawing.Font font = null; System.Drawing.SizeF crSize = new SizeF(); font = new Font("宋体", fontsize, FontStyle.Regular); //测量指定区域 crSize = gbmPhoto.MeasureString(text, font); float y = (imgPhotoHeight - crSize.Height)/2; float x = (imgPhotoWidth - crSize.Width)/2; System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat(); StrFormat.Alignment = System.Drawing.StringAlignment.Center; //画两次制造透明效果 var writeColor = Color.FromArgb(Alpha, System.Drawing.ColorTranslator.FromHtml("#999")); //var writeColor = Color.FromArgb(255, Color.Red); //System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(Alpha, Color.Red)); System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(writeColor); //float _x = -20, _y = y + 10; //if (imgPhotoHeight > imgPhotoWidth) //{ // // gbmPhoto.RotateTransform(-60); // _y = 0; _x = 0; //} //else { gbmPhoto.RotateTransform(320); } gbmPhoto.TranslateTransform(x,y); var Var_trans = gbmPhoto.Transform; Var_trans.Shear(0.4F,0.00F); gbmPhoto.Transform = Var_trans; //gbmPhoto.DrawString(text, font, semiTransBrush2, _x, _y); // gbmPhoto.DrawString(text, font, semiTransBrush2, imgPhotoWidth, imgPhotoHeight / 2); gbmPhoto.DrawString(text, font, semiTransBrush2, 10, 10); //gbmPhoto.DrawString(text, font, semiTransBrush2, 0, y); //System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(writeColor); // gbmPhoto.DrawString(text, font, semiTransBrush, _x, _y); //gbmPhoto.DrawString(text, font, semiTransBrush, imgPhotoWidth / 2, imgPhotoHeight / 2); //gbmPhoto.DrawString(text, font, semiTransBrush, 0, y); //gbmPhoto.DrawString(text, font, semiTransBrush2, 0, y); //Stream newStream = new MemoryStream(); bmPhoto.Save(newImgPath, System.Drawing.Imaging.ImageFormat.Jpeg); gbmPhoto.Dispose(); bmPhoto.Dispose(); //imgPhoto1.Dispose(); return true; } catch (Exception e) { return false; } }
新版,图片像支付宝证件一样,填满整张图片
/// <summary> /// 图片加水印文字 /// </summary> /// <param name="imgPath">原图片路径</param> /// <param name="newImgPath">新图片路径</param> /// <param name="text">水印文字</param> /// <param name="Alpha">透明度</param> /// <param name="fontsize">字体大小</param> public static bool PicAddWaterText(string imgPath,string newImgPath, string text, int Alpha = 1, int fontsize = 30) { try { //text = text + "仅限xx使用"; //System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(oldstream);//文件流 System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(imgPath); int imgPhotoWidth = imgPhoto.Width; int imgPhotoHeight = imgPhoto.Height; Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72, 72); Graphics gbmPhoto = Graphics.FromImage(bmPhoto); //gif背景色 gbmPhoto.Clear(Color.FromName("White")); gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight), 0, 0, imgPhotoWidth, imgPhotoHeight, GraphicsUnit.Pixel); imgPhoto.Dispose(); System.Drawing.Font font = null; System.Drawing.SizeF crSize = new SizeF(); font = new Font("宋体", fontsize, FontStyle.Regular); //测量指定区域 crSize = gbmPhoto.MeasureString(text, font); float y = (imgPhotoHeight - crSize.Height)/2; float x = (imgPhotoWidth - crSize.Width)/2; System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat(); StrFormat.Alignment = System.Drawing.StringAlignment.Center; //画两次制造透明效果 var writeColor = Color.FromArgb(Alpha, System.Drawing.ColorTranslator.FromHtml("#999")); //var writeColor = Color.FromArgb(255, Color.Red); //System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(Alpha, Color.Red)); System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(writeColor); //float _x = -20, _y = y + 10; //if (imgPhotoHeight > imgPhotoWidth) //{ // // gbmPhoto.RotateTransform(-60); // _y = 0; _x = 0; //} //else { gbmPhoto.RotateTransform(320); } //gbmPhoto.RotateTransform(90); var Var_trans = gbmPhoto.Transform; Var_trans.Shear(0.2F, 0.00F); gbmPhoto.Transform = Var_trans; gbmPhoto.TranslateTransform(-(imgPhotoWidth), 0); int hcount = 0; for (int i = -(imgPhotoWidth*2); i < imgPhotoWidth; i++) { hcount = 0; for (int j = 0; j < imgPhotoHeight*1.3f; j++) { //gbmPhoto.DrawString(text, font, semiTransBrush2, _x, _y); // gbmPhoto.DrawString(text, font, semiTransBrush2, imgPhotoWidth, imgPhotoHeight / 2); gbmPhoto.DrawString(text, font, semiTransBrush2, 0, 0); hcount += (int)crSize.Width; gbmPhoto.TranslateTransform(0, crSize.Width); j =j+ Convert.ToInt32(crSize.Width); } gbmPhoto.TranslateTransform(0, -(hcount)); gbmPhoto.TranslateTransform(crSize.Width, 0); i =i+ Convert.ToInt32(crSize.Width); } //gbmPhoto.DrawString(text, font, semiTransBrush2, 0, y); //System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(writeColor); // gbmPhoto.DrawString(text, font, semiTransBrush, _x, _y); //gbmPhoto.DrawString(text, font, semiTransBrush, imgPhotoWidth / 2, imgPhotoHeight / 2); //gbmPhoto.DrawString(text, font, semiTransBrush, 0, y); //gbmPhoto.DrawString(text, font, semiTransBrush2, 0, y); //Stream newStream = new MemoryStream(); bmPhoto.Save(newImgPath, System.Drawing.Imaging.ImageFormat.Jpeg); gbmPhoto.Dispose(); bmPhoto.Dispose(); //imgPhoto1.Dispose(); return true; } catch (Exception e) { return false; } }
优化
/// <summary>
/// 图片加水印文字
/// </summary>
/// <param name="imgPath">原图片路径</param>
/// <param name="newImgPath">新图片路径</param>
/// <param name="text">水印文字</param>
/// <param name="Alpha">透明度</param>
/// <param name="fontsize">字体大小</param>
public static bool PicAddWaterText(string imgPath, string newImgPath, string text, int Alpha = 1, int fontsize = 30)
{
try
{
//text = text + "仅限xx使用";
//System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(oldstream);//文件流
System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(imgPath);
int imgPhotoWidth = imgPhoto.Width;
int imgPhotoHeight = imgPhoto.Height;
var tempWidth = imgPhotoWidth > imgPhotoHeight ? imgPhotoWidth : imgPhotoHeight;
fontsize = tempWidth > 300 ? fontsize : 18;
Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
//gif背景色
gbmPhoto.Clear(Color.FromName("White"));
gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight), 0, 0, imgPhotoWidth, imgPhotoHeight, GraphicsUnit.Pixel);
imgPhoto.Dispose();
System.Drawing.Font font = null;
System.Drawing.SizeF crSize = new SizeF();
font = new Font("宋体", fontsize, FontStyle.Regular);
//测量指定区域
crSize = gbmPhoto.MeasureString(text, font);
float y = (imgPhotoHeight - crSize.Height) / 2;
float x = (imgPhotoWidth - crSize.Width) / 2;
System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
StrFormat.Alignment = System.Drawing.StringAlignment.Center;
//画两次制造透明效果
var writeColor = Color.FromArgb(Alpha, System.Drawing.ColorTranslator.FromHtml("#999"));
//var writeColor = Color.FromArgb(255, Color.Red);
//System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(Alpha, Color.Red));
System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(writeColor);
//float _x = -20, _y = y + 10;
//if (imgPhotoHeight > imgPhotoWidth)
//{
// // gbmPhoto.RotateTransform(-60);
// _y = 0; _x = 0;
//}
//else { gbmPhoto.RotateTransform(320); }
//gbmPhoto.RotateTransform(90);
var Var_trans = gbmPhoto.Transform;
Var_trans.Shear(-0.2F, 0.00F);
gbmPhoto.Transform = Var_trans;
gbmPhoto.TranslateTransform(-tempWidth, 0);
int hcount = 0;
int take = 0;
for (int i = -(tempWidth * 2); i < tempWidth*2; i++)
{
hcount = 0;
for (int j = 0; j < imgPhotoHeight * 1.2f; j++)
{
//gbmPhoto.DrawString(text, font, semiTransBrush2, _x, _y);
// gbmPhoto.DrawString(text, font, semiTransBrush2, imgPhotoWidth, imgPhotoHeight / 2);
if (++take % 2 == 0)
{
gbmPhoto.DrawString(text, font, semiTransBrush2, 0, 0);
}
hcount += (int)crSize.Width;
gbmPhoto.TranslateTransform(0, (int)crSize.Width);
j = j + Convert.ToInt32(crSize.Width);
}
gbmPhoto.TranslateTransform(crSize.Width, -(hcount));
}
//gbmPhoto.DrawString(text, font, semiTransBrush2, 0, y);
//System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(writeColor);
// gbmPhoto.DrawString(text, font, semiTransBrush, _x, _y);
//gbmPhoto.DrawString(text, font, semiTransBrush, imgPhotoWidth / 2, imgPhotoHeight / 2);
//gbmPhoto.DrawString(text, font, semiTransBrush, 0, y);
//gbmPhoto.DrawString(text, font, semiTransBrush2, 0, y);
//Stream newStream = new MemoryStream();
bmPhoto.Save(newImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
gbmPhoto.Dispose();
bmPhoto.Dispose();
//imgPhoto1.Dispose();
return true;
}
catch (Exception e)
{
return false;
}
}
单元测试代码
[TestMethod()] public void WXImgTest() { ImgHelper.PicAddWaterText("D:/pic/1.jpg", "D:/pic1/3.jpg", "xxName", 150,40); //Log4.Write(result); }