1.方法
/// <summary>
/// 在圖片上添加水印文字
/// </summary>
/// <param name="sourcePicture">源圖片文件</param>
/// <param name="waterWords">需要添加到圖片上的文字</param>
/// <param name="fontSize">字體大小</param>
/// <param name="paddingLeft">文字離左邊的距離</param>
/// <param name="paddingTop">文字離上邊的距離</param>
/// <param name="alpha">透明度</param>
/// <param name="color">文字顏色</param>
/// <param name="PicturePath">文件路徑</param>
/// <param name="bSave">是否保存</param>
/// <param name="fileName">保存文件名稱</param>
/// <param name="img">圖片文件對象</param>
/// <returns>返回文件保存路徑,如果不保存,則返回爲string.Empty</returns>
public string DrawWords(string sourcePicture, string waterWords, float fontSize, float paddingLeft, float paddingTop, float alpha,Color color, string PicturePath,bool bSave,string fileName, ref Image img)
{
//創建一個圖片對象用來裝載要被添加水印的圖片
Image imgPhoto = null;
string targetImage = string.Empty;
if (img != null)
{
imgPhoto = img;
}
if (img == null || bSave)
{
// 判斷參數是否有效
if (sourcePicture == string.Empty || waterWords == string.Empty || alpha == 0.0 ||
(bSave && PicturePath == string.Empty))
{
return targetImage;
}
// 源圖片全路徑
string sourcePictureName = PicturePath + sourcePicture;
targetImage = sourcePictureName.Replace(System.IO.Path.GetExtension(sourcePictureName), "") + fileName;
if (img == null)
{
imgPhoto = Image.FromFile(sourcePictureName);
}
}
//獲取圖片的寬和高
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
//建立一個bitmap,和我們需要加水印的圖片一樣大小
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
//SetResolution:設置此 Bitmap 的分辨率
//這裏直接將我們需要添加水印的圖片的分辨率賦給了bitmap
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//Graphics:封裝一個 GDI+ 繪圖圖面。
Graphics grPhoto = Graphics.FromImage(bmPhoto);
//設置圖形的品質
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
//將我們要添加水印的圖片按照原始大小描繪(複製)到圖形中
grPhoto.DrawImage(
imgPhoto, // 要添加水印的圖片
new Rectangle(0, 0, phWidth, phHeight), // 根據要添加的水印圖片的寬和高
0, // X方向從0點開始描繪
0, // Y方向
phWidth, // X方向描繪長度
phHeight, // Y方向描繪長度
GraphicsUnit.Pixel); // 描繪的單位,這裏用的是像素
//字體
Font crFont = null;
//矩形的寬度和高度,SizeF有三個屬性,分別爲Height高,width寬,IsEmpty是否爲空
SizeF crSize = new SizeF();
//直到它的長度比圖片的寬度小
crFont = new Font("arial", fontSize, FontStyle.Regular);
//測量用指定的 Font 對象繪製並用指定的 StringFormat 對象格式化的指定字符串。
crSize = grPhoto.MeasureString(waterWords, crFont);
//截邊5%的距離,定義文字顯示(由於不同的圖片顯示的高和寬不同,所以按百分比截取)
int yPixlesFromBottom = (int)(phHeight * .05);
//定義在圖片上文字的大小
float wmHeight = crSize.Height;
float wmWidth = crSize.Width;
//定義在圖片上文字的位置
float xPosOfWm;
float yPosOfWm;
if (paddingLeft > 0)
{
xPosOfWm = paddingLeft;
}
else
{
xPosOfWm = phWidth / 2;
}
if (paddingTop > 0)
{
yPosOfWm = paddingTop;
}
else
{
yPosOfWm = phHeight / 2;
}
//封裝文本佈局信息(如對齊、文字方向和 Tab 停靠位),顯示操作(如省略號插入和國家標準 (National) 數字替換)和 OpenType 功能。
StringFormat StrFormat = new StringFormat();
//定義需要印的文字居中對齊
StrFormat.Alignment = StringAlignment.Center;
//SolidBrush:定義單色畫筆。畫筆用於填充圖形形狀,如矩形、橢圓、扇形、多邊形和封閉路徑。
//這個畫筆爲描繪陰影的畫筆,呈灰色
int m_alpha = Convert.ToInt32(256 * alpha);
//從四個 ARGB 分量(alpha、紅色、綠色和藍色)值創建 Color 結構,這裏設置透明度爲153
//這個畫筆爲描繪正式文字的筆刷,呈白色
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(m_alpha, color.R, color.G, color.B));
//第二次繪製這個圖形,建立在第一次描繪的基礎上
grPhoto.DrawString(waterWords, //string of text
crFont, //font
semiTransBrush, //Brush
new PointF(xPosOfWm, yPosOfWm), //Position
StrFormat);
//imgPhoto是我們建立的用來裝載最終圖形的Image對象
//bmPhoto是我們用來製作圖形的容器,爲Bitmap對象
imgPhoto = bmPhoto;
//釋放資源,將定義的Graphics實例grPhoto釋放,grPhoto功德圓滿
grPhoto.Dispose();
if (bSave)
{
//將grPhoto保存
imgPhoto.Save(targetImage, ImageFormat.Jpeg);
}
img = imgPhoto;
return bSave ? targetImage.Replace(PicturePath, "") : string.Empty;
}
2.調用:
WaterImageManage water = new WaterImageManage();
Image img = null;
water.DrawWords("123.jpg", "姓名:張三", 26, 0, 200, 0.8f, Color.Red, MapPath("/Images/"), false, string.Empty, ref img);
water.DrawWords("", "手機:18607552350", 26, 0, 200, 0.8f, Color.Red, MapPath("/Images/"), false, string.Empty, ref img);
water.DrawWords("", "QQ:8474939612", 20, 0, 240, 0.8f, Color.Black, MapPath("/Images/"), true, "123456.jpg", ref img);