1.图片添加水印字符串
/// <summary>
/// 文字水印
/// </summary>
/// <param name="imgPath">服务器图片相对路径</param>
/// <param name="filename">保存文件名</param>
/// <param name="watermarkText">水印文字</param>
/// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param>
/// <param name="quality">附加水印图片质量,0-100</param>
/// <param name="fontsize">字体大小</param>
/// <param name="fontname">字体</param>
public void ImageAddWaterMarkStr(string imgPath, string filename, string watermarkText,
int watermarkStatus, int quality, int fontsize, string fontname = "微软雅黑")
{
byte[] _ImageBytes = File.ReadAllBytes(imgPath);
Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));
Graphics g = Graphics.FromImage(img);
Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
SizeF crSize;
crSize = g.MeasureString(watermarkText, drawFont);
float xpos = 0;
float ypos = 0;
switch (watermarkStatus)
{
case 1:
xpos = (float)img.Width * (float).01;
ypos = (float)img.Height * (float).01;
break;
case 2:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = (float)img.Height * (float).01;
break;
case 3:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = (float)img.Height * (float).01;
break;
case 4:
xpos = (float)img.Width * (float).01;
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 5:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 6:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 7:
xpos = (float)img.Width * (float).01;
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
case 8:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
case 9:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
}
g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType.IndexOf("jpeg") > -1) ici = codec;
}
EncoderParameters encoderParams = new EncoderParameters();
long[] qualityParam = new long[1];
if (quality < 0 || quality > 100) quality = 80;
qualityParam[0] = quality;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
encoderParams.Param[0] = encoderParam;
if (ici != null) img.Save(filename, ici, encoderParams);
else img.Save(filename);
g.Dispose();
img.Dispose();
}
//测试代码
private void 添加水印_Click(object sender, EventArgs e)
{
string img = Application.StartupPath + "\\app.ico";
string waterImg = Application.StartupPath + "\\waterApp.ico";
ImageAddWaterMarkStr(img, waterImg, "app", 2, 50, 10);
this.pbWater.Image = Image.FromFile(waterImg);
}
2.二进制流添加水印字符串
/// <summary>
/// 给二进制流添加水印文字并返回
/// </summary>
/// <param name="photo">二进制流</param>
/// <param name="font">字体</param>
/// <param name="fontSize">字体大小</param>
/// <param name="wntType">水印位置</param>
/// <param name="Text">水印文字</param>
/// <returns></returns>
public byte[] BytesAddWaterMarkStr(Byte[] photo, string font, int fontSize, string wntType, string Text)
{
MemoryStream stmBLOB = new MemoryStream(photo);
Image pic = Image.FromStream(stmBLOB);
Graphics grap = Graphics.FromImage(pic);
Brush brush = new SolidBrush(Color.Blue);//创建一把刷子
int xpos = 10;
int ypos = 10;
switch (wntType)
{
case "WMP_Left_Top":
xpos = 10;
ypos = 10;
break;
case "WMP_Right_Top":
xpos = pic.Width - 10;
ypos = 10;
break;
case "WMP_Right_Bottom":
xpos = pic.Width - 60;
ypos = pic.Height - 60;
break;
case "WMP_Left_Bottom":
xpos = 10;
ypos = pic.Height - 10;
break;
case "WM_ZJ":
xpos = pic.Width / 2;
ypos = pic.Height / 2;
break;
}
grap.DrawString(Text, new Font(font, fontSize), brush, xpos, ypos);//给图片(pic)加水印
byte[] bt = null;
using (MemoryStream mostream = new MemoryStream())
{
Bitmap bmp = new Bitmap(pic);
bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Png);//将图像以指定的格式存入缓存内存流
bt = new byte[mostream.Length];
mostream.Position = 0;//设置留的初始位置
mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
}
return bt;
}
//给二进制流加水印文字
byte[] imgData =BytesAddWaterMarkStr(data.SignetName, "宋体", 3, "WMP_Right_Bottom", "水印字符串");
3.二进制流添加水印字符串
/// <summary>
/// 文字水印
/// </summary>
/// <param name="photo">二进制流</param>
/// <param name="watermarkText">水印文字</param>
/// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param>
/// <param name="quality">附加水印图片质量,0-100</param>
/// <param name="fontsize">字体大小</param>
/// <param name="fontname">字体</param>
public byte[] BytesAddWaterMarkStr2(Byte[] photo, string watermarkText,
int watermarkStatus, int quality, int fontsize, string fontname = "微软雅黑")
{
Image img = Image.FromStream(new System.IO.MemoryStream(photo));
Graphics grap = Graphics.FromImage(img);
Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
SizeF crSize = grap.MeasureString(watermarkText, drawFont);
float xpos = 0;
float ypos = 0;
switch (watermarkStatus)
{
case 1:
xpos = (float)img.Width * (float).01;
ypos = (float)img.Height * (float).01;
break;
case 2:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = (float)img.Height * (float).01;
break;
case 3:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = (float)img.Height * (float).01;
break;
case 4:
xpos = (float)img.Width * (float).01;
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 5:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 6:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 7:
xpos = (float)img.Width * (float).01;
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
case 8:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
case 9:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
}
grap.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
grap.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);
//grap.Dispose();
// img.Dispose();
byte[] bt = null;
using (MemoryStream mostream = new MemoryStream())
{
Bitmap bmp = new Bitmap(img);
bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Png);//将图像以指定的格式存入缓存内存流
bt = new byte[mostream.Length];
mostream.Position = 0;//设置留的初始位置
mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
}
return bt;
}
调用方式:
byte[] bytes = ImageToByte(filePath);
byte[] bytes2 = BytesAddWaterMarkStr2(bytes, "添加水印", 5, 50, 15);
this.pbWater.Image = ByteArrayToImage(bytes2);
运行效果如下:
注:如果需要添加的水印字符串 倾斜 等,设置如下——仅供参考
Font drawFont = new Font(fontname, fontsize, FontStyle.Italic | FontStyle.Strikeout, GraphicsUnit.Pixel);