C#實現的添加水印圖片/文字
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
/*
*
* 使用說明:
* 建議先定義一個WaterImage實例
* 然後利用實例的屬性,去匹配需要進行操作的參數
* 然後定義一個WaterImageManager實例
* 利用WaterImageManager實例進行DrawImage()印圖片水印
* DrawWords()印文字水印
*
*/
namespace Common.Utils
{
/// <summary>
/// 圖片位置
/// </summary>
public enum ImagePosition
{
LeftTop, //左上
LeftBottom, //左下
RightTop, //右上
RigthBottom, //右下
TopMiddle, //頂部居中
BottomMiddle, //底部居中
Center //中心
}
/// <summary>
/// 字體集
/// </summary>
public enum FontFamilys
{
CUSTOM,
Arial,
Batang,
BatangChe,
Calibri,
Cambria,
Candara,
Consolas,
Ebrima,
Footlight_MT_Light,
Kalinga,
Kokila,
Mangal,
Symbol,
Times_New_Roman,
Webdings,
仿宋,
華文中宋,
華文仿宋,
華文宋體,
華文彩雲,
華文新魏,
華文楷體,
華文琥珀,
華文細黑,
華文行楷,
華文隸書,
宋體,
幼圓,
微軟雅黑,
新宋體,
方正姚體,
方正舒體,
楷體,
隸書,
黑體
}
/// <summary>
/// 水印圖片的操作管理
/// </summary>
public class WaterImageManager
{
private int padding = 0;//內容間隔
private string targetPicName = "_mixture_pic";//默認生成圖片的文件名字
private string targetPicPath = "";//默認生成圖片的目錄
private ImageFormat picFormat = ImageFormat.Png;//默認生成圖片的格式
public int Padding { get; set; }
public string TargetPicName { get; set; }
public string TargetPicPath { get; set; }
public ImageFormat PicFormat { get; set; }
/// <summary>
/// 生成一個新的水印圖片製作實例(默認)
/// </summary>
public WaterImageManager()
{
Padding = padding;
TargetPicName = targetPicName;
TargetPicPath = targetPicPath;
PicFormat = picFormat;
}
/// <summary>
/// 生成一個新的水印圖片製作實例(有參)
/// </summary>
/// <param name="tragetPicName">生成合成圖片的文件名稱</param>
/// <param name="tragetPicPath">生成合成圖片的文件路徑</param>
/// <param name="padding">指定水印距離父容器邊距</param>
/// <param name="picFormat">指定生成合成圖片的圖片格式</param>
public WaterImageManager(string targetPicName, string targetPicPath, int padding, ImageFormat picFormat)
{
this.Padding = padding;
this.TargetPicName = targetPicName;
this.TargetPicPath = targetPicPath.EndsWith(@"\") ? targetPicPath : targetPicPath + @"\";
this.PicFormat = picFormat;
}
/// <summary>
/// 合成圖片
/// </summary>
/// <param name="sourcePictureName">源文件名(包括後綴)</param>
/// <param name="sourcePicturePath">源文件路徑</param>
/// <param name="waterPictureName">水印文件名(包括後綴)</param>
/// <param name="waterPicturePath">水印文件路徑</param>
/// <param name="alpha">透明度(0.1-1.0數值越小透明度越高)</param>
/// <param name="position">位置</param>
/// <returns>合成圖片的完整路徑</returns>
public string DrawImage(string sourcePictureName,
string sourcePicturePath,
string waterPictureName,
string waterPicturePath,
float alpha,
ImagePosition position)
{
//
// 判斷參數是否有效
//
if (sourcePictureName == string.Empty || waterPictureName == string.Empty || alpha == 0.0 || sourcePicturePath == string.Empty || waterPicturePath == string.Empty)
{
return sourcePicturePath + sourcePictureName + "." + PicFormat.ToString().ToLower();
}
if (!sourcePicturePath.EndsWith(@"\"))
sourcePicturePath = sourcePicturePath + @"\";
if (!waterPicturePath.EndsWith(@"\"))
waterPicturePath = waterPicturePath + @"\";
//
// 源圖片,水印圖片全路徑
//
string _sourcePictureName = sourcePicturePath + sourcePictureName;
string _waterPictureName = waterPicturePath + waterPictureName;
if (!File.Exists(_sourcePictureName))
throw new FileNotFoundException(_sourcePictureName + " file not found!");
if (!File.Exists(_waterPictureName))
throw new FileNotFoundException(_waterPictureName + " file not found!");
string fileSourceExtension = System.IO.Path.GetExtension(_sourcePictureName).ToLower();
string fileWaterExtension = System.IO.Path.GetExtension(_waterPictureName).ToLower();
//
// 判斷文件是否存在,以及類型是否正確
//
if (System.IO.File.Exists(_sourcePictureName) == false ||
System.IO.File.Exists(_waterPictureName) == false || (
fileSourceExtension != ".gif" &&
fileSourceExtension != ".jpg" &&
fileSourceExtension != ".png") || (
fileWaterExtension != ".gif" &&
fileWaterExtension != ".jpg" &&
fileWaterExtension != ".png")
)
{
return sourcePicturePath + sourcePictureName + "." + PicFormat.ToString().ToLower();
}
//
// 目標圖片名稱及全路徑
//
TargetPicPath = TargetPicPath.EndsWith(@"\") ? TargetPicPath : TargetPicPath + @"\";
string targetImage = TargetPicPath == string.Empty ?
_sourcePictureName.Replace(System.IO.Path.GetExtension(_sourcePictureName), "") + TargetPicName + "." + PicFormat.ToString().ToLower() :
TargetPicPath + TargetPicName + "." + PicFormat.ToString().ToLower();
//
// 將需要加上水印的圖片裝載到Image對象中
//
Image imgPhoto = Image.FromFile(_sourcePictureName);
//
// 確定其長寬
//
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
//
// 封裝 GDI+ 位圖,此位圖由圖形圖像及其屬性的像素數據組成。
//
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, imgPhoto.PixelFormat);//phWidth, phHeight, PixelFormat.Format24bppRgb imgPhoto
//
// 設定分辨率
//
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//
// 定義一個繪圖畫面用來裝載位圖
//
Graphics grPhoto = Graphics.FromImage(bmPhoto);
//
//同樣,由於水印是圖片,我們也需要定義一個Image來裝載它
//
Image imgWatermark = new Bitmap(_waterPictureName);
//
// 獲取水印圖片的高度和寬度
//
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;
//SmoothingMode:指定是否將平滑處理(消除鋸齒)應用於直線、曲線和已填充區域的邊緣。
// 成員名稱 說明
// AntiAlias 指定消除鋸齒的呈現。
// Default 指定不消除鋸齒。
// HighQuality 指定高質量、低速度呈現。
// HighSpeed 指定高速度、低質量呈現。
// Invalid 指定一個無效模式。
// None 指定不消除鋸齒。
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
//
// 第一次描繪,將我們的底圖描繪在繪圖畫面上
//
grPhoto.DrawImage(imgPhoto,
new Rectangle(0, 0, phWidth, phHeight),
0,
0,
phWidth,
phHeight,
GraphicsUnit.Pixel);
//
// 與底圖一樣,我們需要一個位圖來裝載水印圖片。並設定其分辨率
//
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//
// 繼續,將水印圖片裝載到一個繪圖畫面grWatermark
//
Graphics grWatermark = Graphics.FromImage(bmWatermark);
//
//ImageAttributes 對象包含有關在呈現時如何操作位圖和圖元文件顏色的信息。
//
ImageAttributes imageAttributes = new ImageAttributes();
//
//Colormap: 定義轉換顏色的映射
//
ColorMap colorMap = new ColorMap();
//
//我的水印圖被定義成擁有綠色背景色的圖片被替換成透明
//
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, //red紅色
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, //green綠色
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, //blue藍色
new float[] {0.0f, 0.0f, 0.0f, alpha, 0.0f},//透明度
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};//
// ColorMatrix:定義包含 RGBA 空間座標的 5 x 5 矩陣。
// ImageAttributes 類的若干方法通過使用顏色矩陣調整圖像顏色。
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
//
//上面設置完顏色,下面開始設置位置
//
int xPosOfWm;
int yPosOfWm;
switch (position)
{
case ImagePosition.BottomMiddle:
xPosOfWm = (phWidth - wmWidth) / 2;
yPosOfWm = phHeight - wmHeight - Padding;
break;
case ImagePosition.Center:
xPosOfWm = (phWidth - wmWidth) / 2;
yPosOfWm = (phHeight - wmHeight) / 2;
break;
case ImagePosition.LeftBottom:
xPosOfWm = Padding;
yPosOfWm = phHeight - wmHeight - Padding;
break;
case ImagePosition.LeftTop:
xPosOfWm = Padding;
yPosOfWm = Padding;
break;
case ImagePosition.RightTop:
xPosOfWm = phWidth - wmWidth - Padding;
yPosOfWm = Padding;
break;
case ImagePosition.RigthBottom:
xPosOfWm = phWidth - wmWidth - Padding;
yPosOfWm = phHeight - wmHeight - Padding;
break;
case ImagePosition.TopMiddle:
xPosOfWm = (phWidth - wmWidth) / 2;
yPosOfWm = Padding;
break;
default:
xPosOfWm = Padding;
yPosOfWm = phHeight - wmHeight - Padding;
break;
}
imgPhoto.Dispose();//釋放底圖,解決圖片保存時 “GDI+ 中發生一般性錯誤。”
// 第二次繪圖,把水印印上去
//
grWatermark.DrawImage(imgWatermark,
new Rectangle(xPosOfWm,
yPosOfWm,
wmWidth,
wmHeight),
0,
0,
wmWidth,
wmHeight,
GraphicsUnit.Pixel,
imageAttributes);
imgPhoto = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose();
//
// 保存文件到服務器的文件夾裏面
//
if (!System.IO.Directory.Exists(TargetPicPath))
System.IO.Directory.CreateDirectory(TargetPicPath);
try
{
imgPhoto.Save(targetImage, PicFormat);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
imgPhoto.Dispose();
imgWatermark.Dispose();
return TargetPicPath + TargetPicName + "." + PicFormat.ToString().ToLower();
}
/// <summary>
/// 在圖片上添加水印文字
/// </summary>
/// <param name="sourcePicture">源圖片文件名(包含後綴)</param>
/// <param name="waterWords">需要添加到圖片上的文字</param>
/// <param name="alpha">透明度(取值區間(0.0,1.0])</param>
/// <param name="position">位置</param>
/// <param name="PicturePath">文件路徑</param>
/// <returns></returns>
public string DrawWords(string sourcePictureName,
string sourcePicturePath,
string waterWords,
float alpha,
FontFamilys fontFamily,
FontStyle style,
ImagePosition position)
{
//
// 判斷參數是否有效
//
if (sourcePictureName == string.Empty || waterWords == string.Empty || alpha == 0.0 || sourcePicturePath == string.Empty)
{
return sourcePicturePath + sourcePictureName;
}
if (!sourcePicturePath.EndsWith(@"\"))
sourcePicturePath = sourcePicturePath + @"\";
//
// 源圖片全路徑
//
string _sourcePictureName = sourcePicturePath + sourcePictureName;
if (!File.Exists(_sourcePictureName))
throw new FileNotFoundException(_sourcePictureName + " file not found!");
string fileExtension = System.IO.Path.GetExtension(_sourcePictureName).ToLower();
//
// 判斷文件是否存在,以及文件名是否正確
//
if (System.IO.File.Exists(_sourcePictureName) == false || (
fileExtension != ".gif" &&
fileExtension != ".jpg" &&
fileExtension != ".png"))
{
return sourcePicturePath + sourcePictureName;
}
//
// 目標圖片名稱及全路徑
//
string targetImage = TargetPicPath == string.Empty ?
_sourcePictureName.Replace(System.IO.Path.GetExtension(_sourcePictureName), "") + TargetPicName + "." + PicFormat.ToString().ToLower() :
TargetPicPath + TargetPicName + "." + PicFormat.ToString().ToLower();
//創建一個圖片對象用來裝載要被添加水印的圖片
Image imgPhoto = Image.FromFile(_sourcePictureName);
//獲取圖片的寬和高
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
//
//建立一個bitmap,和我們需要加水印的圖片一樣大小
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, imgPhoto.PixelFormat);
//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); // 描繪的單位,這裏用的是像素
//根據圖片的大小我們來確定添加上去的文字的大小
//在這裏我們定義一個數組來確定
int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4, 2 };
//字體
Font crFont = null;
//矩形的寬度和高度,SizeF有三個屬性,分別爲Height高,width寬,IsEmpty是否爲空
SizeF crSize = new SizeF();
//利用一個循環語句來選擇我們要添加文字的型號
//直到它的長度比圖片的寬度小
for (int i = 0; i < 8; i++)
{
crFont = new Font(fontFamily.ToString(), sizes[i], style);
//測量用指定的 Font 對象繪製並用指定的 StringFormat 對象格式化的指定字符串。
crSize = grPhoto.MeasureString(waterWords, crFont);
// ushort 關鍵字表示一種整數數據類型
if ((ushort)crSize.Width < (ushort)phWidth)
break;
}
//截邊5%的距離,定義文字顯示(由於不同的圖片顯示的高和寬不同,所以按百分比截取)
int yPixlesFromBottom = (int)(phHeight * .05);
//定義在圖片上文字的位置
float wmHeight = crSize.Height;
float wmWidth = crSize.Width;
float xPosOfWm;
float yPosOfWm;
switch (position)
{
case ImagePosition.BottomMiddle:
xPosOfWm = phWidth / 2;
yPosOfWm = phHeight - wmHeight - Padding;
break;
case ImagePosition.Center:
xPosOfWm = phWidth / 2;
yPosOfWm = phHeight / 2;
break;
case ImagePosition.RigthBottom:
xPosOfWm = phWidth / 2 + wmWidth / 2;
yPosOfWm = phHeight - wmHeight - Padding;
break;
case ImagePosition.RightTop:
xPosOfWm = phWidth / 2 + wmWidth / 2;
yPosOfWm = wmHeight / 2 + Padding;
break;
case ImagePosition.LeftTop:
xPosOfWm = wmWidth / 2 + Padding;
yPosOfWm = wmHeight / 2 + Padding;
break;
case ImagePosition.LeftBottom:
xPosOfWm = wmWidth / 2 + Padding;
yPosOfWm = phHeight - wmHeight - Padding;
break;
case ImagePosition.TopMiddle:
xPosOfWm = phWidth / 2;
yPosOfWm = wmHeight / 2 + Padding;
break;
default:
xPosOfWm = wmWidth;
yPosOfWm = phHeight - wmHeight - Padding;
break;
}
imgPhoto.Dispose();//釋放底圖,解決圖片保存時 “GDI+ 中發生一般性錯誤。”
//封裝文本佈局信息(如對齊、文字方向和 Tab 停靠位),顯示操作(如省略號插入和國家標準 (National) 數字替換)和 OpenType 功能。
StringFormat StrFormat = new StringFormat();
//定義需要印的文字居中對齊
StrFormat.Alignment = StringAlignment.Center;
//SolidBrush:定義單色畫筆。畫筆用於填充圖形形狀,如矩形、橢圓、扇形、多邊形和封閉路徑。
//這個畫筆爲描繪陰影的畫筆,呈灰色
int m_alpha = Convert.ToInt32(255 * alpha);
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(m_alpha, 0, 0, 0));
//描繪文字信息,這個圖層向右和向下偏移一個像素,表示陰影效果
//DrawString 在指定矩形並且用指定的 Brush 和 Font 對象繪製指定的文本字符串。
grPhoto.DrawString(waterWords, //string of text
crFont, //font
semiTransBrush2, //Brush
new PointF(xPosOfWm + 1, yPosOfWm + 1), //Position
StrFormat);
//從四個 ARGB 分量(alpha、紅色、綠色和藍色)值創建 Color 結構,這裏設置透明度爲153
//這個畫筆爲描繪正式文字的筆刷,呈白色
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
//第二次繪製這個圖形,建立在第一次描繪的基礎上
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();
//將grPhoto保存
if (!System.IO.Directory.Exists(TargetPicPath))
System.IO.Directory.CreateDirectory(TargetPicPath);
try
{
imgPhoto.Save(targetImage, PicFormat);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
imgPhoto.Dispose();
return TargetPicPath + TargetPicName + "." + PicFormat.ToString().ToLower();
}
/// 無損壓縮圖片
/// <param name="sFile">原圖片</param>
/// <param name="dFile">壓縮後保存位置</param>
/// <param name="dHeight">高度</param>
/// <param name="dWidth">寬度</param>
/// <param name="flag">壓縮質量(數字越小壓縮率越高) 1-100</param>
/// <returns></returns>
public bool GetPicThumbnail(string sFile, string dFile, int dHeight, int dWidth, int flag)
{
System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
ImageFormat tFormat = iSource.RawFormat;
int sW = 0, sH = 0;
//按比例縮放
Size tem_size = new Size(iSource.Width, iSource.Height);
if (tem_size.Width > dHeight || tem_size.Width > dWidth)
{
if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
{
sW = dWidth;
sH = (dWidth * tem_size.Height) / tem_size.Width;
}
else
{
sH = dHeight;
sW = (tem_size.Width * dHeight) / tem_size.Height;
}
}
else
{
sW = tem_size.Width;
sH = tem_size.Height;
}
Bitmap ob = new Bitmap(dWidth, dHeight);
Graphics g = Graphics.FromImage(ob);
g.Clear(Color.WhiteSmoke);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
g.Dispose();
//以下代碼爲保存圖片時,設置壓縮質量
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//設置壓縮的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
if (jpegICIinfo != null)
{
ob.Save(dFile, jpegICIinfo, ep);//dFile是壓縮後的新路徑
}
else
{
ob.Save(dFile, tFormat);
}
return true;
}
catch(Exception e)
{
Console.WriteLine(e.Message);
return false;
}
finally
{
iSource.Dispose();
ob.Dispose();
}
}
}
/// <summary>
/// 裝載水印圖片的相關信息
/// </summary>
public class WaterImage
{
public WaterImage()
{
}
private string m_sourcePicture;
/// <summary>
/// 源圖片地址名字(帶後綴)
/// </summary>
public string SourcePicture
{
get { return m_sourcePicture; }
set { m_sourcePicture = value; }
}
private string m_waterImager;
/// <summary>
/// 水印圖片名字(帶後綴)
/// </summary>
public string WaterPicture
{
get { return m_waterImager; }
set { m_waterImager = value; }
}
private float m_alpha;
/// <summary>
/// 水印圖片文字的透明度
/// </summary>
public float Alpha
{
get { return m_alpha; }
set { m_alpha = value; }
}
private ImagePosition m_postition;
/// <summary>
/// 水印圖片或文字在圖片中的位置
/// </summary>
public ImagePosition Position
{
get { return m_postition; }
set { m_postition = value; }
}
private string m_words;
/// <summary>
/// 水印文字的內容
/// </summary>
public string Words
{
get { return m_words; }
set { m_words = value; }
}
}
}
使用方式參考:
var parntImg = @"icon.png";
var waterImg = @"sup_icon.png";
var saveImg = @"C:\Users\luzhenyu\Desktop\test\";
WaterImageManage manage = new WaterImageManage("", "", 0, ImageFormat.Png);
var temp = manage.DrawImage(parntImg, saveImg, waterImg, saveImg, 1.0f, ImagePosition.TopMiddle);//水印圖片
var temp = manage.DrawWords(parntImg, saveImg, "我愛你", 0.5f, FontFamilys.黑體, FontStyle.Regular, ImagePosition.TopMiddle);//水印文字
manage.GetPicThumbnail(saveImg + "icon.png", saveImg + "hahahaha.png", 48, 48, 100);//無損壓縮圖片
【歡迎上碼】
【微信公衆號搜索 h2o2s2】