/// <summary>生成带文字水印的缩略图 </summary>
/// <param name="originalImgPath">源图路径(物理路径)</param>
/// <param name="newImgPath">缩略图路径(物理路径)</param>
/// <param name="waterMarkText">水印文字</param>
public static void markThumbanil(string originalImgPath, string newImgPath, string position, string waterMarkText, float transparent)
{
createThumbnail.alpha = transparent;
System.Drawing.Image originaImg = System.Drawing.Image.FromFile(originalImgPath);//需要加水印的图片
int x = 0;
int y = 0;
int ow = originaImg.Width;//原始图片宽
int oh = originaImg.Height;//原始图片高
//新建一个bmp图片
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(ow, oh, PixelFormat.Format24bppRgb);
bitmap.SetResolution(originaImg.HorizontalResolution, originaImg.VerticalResolution);
//新建一个画板
Graphics grPhoto = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
grPhoto.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
grPhoto.DrawImage(originaImg, new Rectangle(0, 0, ow, oh),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
//设置要添加的文字
//根据图片的大小我们来确定添加上去的文字的大小
//在这里我们定义一个数组来确定
int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };
//字体
Font crFont = null;
//矩形的宽度和高度,SizeF有三个属性,分别为Height高,width宽,IsEmpty是否为空
SizeF crSize = new SizeF();
//利用一个循环语句来选择我们要添加文字的型号
//直到它的长度比图片的宽度小
for (int i = 0; i < 7; i++)
{
crFont = new Font("arial", sizes[i], FontStyle.Bold);
//测量用指定的 Font 对象绘制并用指定的 StringFormat 对象格式化的指定字符串。
crSize = grPhoto.MeasureString(waterMarkText, crFont);
// ushort 关键字表示一种整数数据类型
if ((ushort)crSize.Width < (ushort)(ow))
{
break;
}
}
//截边5%的距离,定义文字显示(由于不同的图片显示的高和宽不同,所以按百分比截取)
// int yPixlesFromBottom = (int)(oh * .05);
//定义在图片上文字的位置
//float wmHeight = crSize.Height;
//float wmWidth = crSize.Width;
float xPosOfWm = 0;
float yPosOfWm = 0;
//位置
switch (position)
{
case "BottomMiddle":
xPosOfWm = originaImg.Width / 2;
yPosOfWm = originaImg.Height - crSize.Height - 10;
break;
case "Center":
xPosOfWm = originaImg.Width / 2;
yPosOfWm = originaImg.Height / 2;
break;
case "LeftBottom":
xPosOfWm = crSize.Width / 2;
yPosOfWm = originaImg.Height - crSize.Height - 10;
break;
case "LeftTop":
xPosOfWm = crSize.Width / 2;
yPosOfWm = crSize.Height / 2;
break;
case "RightTop":
xPosOfWm = originaImg.Width - crSize.Width / 2;
yPosOfWm = crSize.Height / 2;
break;
case "RightBottom":
xPosOfWm = originaImg.Width - crSize.Width / 2;
yPosOfWm = originaImg.Height - crSize.Height - 10;
break;
case "TopMiddle":
xPosOfWm = originaImg.Width / 2;
yPosOfWm = 10;
break;
default:
xPosOfWm = crSize.Width;
yPosOfWm = originaImg.Height - crSize.Height - 10;
break;
}
//封装文本布局信息(如对齐、文字方向和 Tab 停靠位),显示操作(如省略号插入和国家标准 (National) 数字替换)和 OpenType 功能。
StringFormat StrFormat = new StringFormat();
//定义需要印的文字居中对齐
StrFormat.Alignment = StringAlignment.Center;
//SolidBrush:定义单色画笔。画笔用于填充图形形状,如矩形、椭圆、扇形、多边形和封闭路径。
//这个画笔为描绘阴影的画笔,呈灰色
int m_alpha = Convert.ToInt32(256 * alpha);
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(m_alpha, 0, 0, 0));
//描绘文字信息,这个图层向右和向下偏移一个像素,表示阴影效果
//DrawString 在指定矩形并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。
grPhoto.DrawString(waterMarkText, //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(waterMarkText, //string of text
crFont, //font
semiTransBrush, //Brush
new PointF(xPosOfWm, yPosOfWm), //Position
StrFormat);
//originaImg = bitmap;
try
{
//以jpg格式保存缩略图
bitmap.Save(newImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
originaImg.Dispose();
bitmap.Dispose();
grPhoto.Dispose();
StrFormat.Dispose();
if (File.Exists(originalImgPath))
{ File.Delete(originalImgPath); }
}
catch (System.Exception e)
{
throw e;
}
finally
{
originaImg.Dispose();
bitmap.Dispose();
grPhoto.Dispose();
StrFormat.Dispose();
}
}
/// <summary>生成带图片水印的缩略图 </summary>
/// <param name="originalImgPath">源图路径(物理路径)</param>
/// <param name="newImgPath">缩略图路径(物理路径)</param>
/// <param name="waterMarkPath">水印图片路径(物理路径)</param>
/// <param name="transparent">透明度(0.1-1.0数值越小透明度越高)</param>
public static void markThumbanilImg(string originalImgPath, string newImgPath, string waterMarkPath, string position, float transparent)
{
createThumbnail.alpha = transparent;
System.Drawing.Image originaImg = System.Drawing.Image.FromFile(originalImgPath);//将需要加上水印的图片装载到Image对象中
int x = 0;
int y = 0;
int ow = originaImg.Width;//原始图片宽
int oh = originaImg.Height;//原始图片高
//新建一个bmp图片
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(ow, oh, PixelFormat.Format24bppRgb);
bitmap.SetResolution(originaImg.HorizontalResolution, originaImg.VerticalResolution);
//新建一个画板
Graphics grPhoto = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
grPhoto.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
System.Drawing.Image copyImg = new Bitmap(waterMarkPath);//由于水印是图片,我们也需要定义一个Image来装载它
//画底图
grPhoto.DrawImage(originaImg, new Rectangle(0, 0, ow, oh), 0, 0, ow, oh, GraphicsUnit.Pixel);
//Bitmap bmWatermark = new Bitmap(bitmap);
//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 = 0;
int yPosOfWm = 0;
switch (position)
{
case "BottomMiddle":
xPosOfWm = (originaImg.Width - copyImg.Width) / 2;
yPosOfWm = originaImg.Height - copyImg.Height - 10;
break;
case "Center":
xPosOfWm = (originaImg.Width - copyImg.Width) / 2;
yPosOfWm = (originaImg.Height - copyImg.Height) / 2;
break;
case "LeftBottom":
xPosOfWm = 10;
yPosOfWm = originaImg.Height - copyImg.Height - 10;
break;
case "LeftTop":
xPosOfWm = 10;
yPosOfWm = 10;
break;
case "RightTop":
xPosOfWm = originaImg.Width - copyImg.Width - 10;
yPosOfWm = 10;
break;
case "RightBottom":
xPosOfWm = originaImg.Width - copyImg.Width - 10;
yPosOfWm = originaImg.Height - copyImg.Height - 10;
break;
case "TopMiddle":
xPosOfWm = (originaImg.Width - copyImg.Width) / 2;
yPosOfWm = 10;
break;
default:
xPosOfWm = 10;
yPosOfWm = originaImg.Height - copyImg.Height - 10;
break;
}
//第二画图,把水印加上
grPhoto.DrawImage(copyImg, new Rectangle(xPosOfWm, yPosOfWm, copyImg.Width, copyImg.Height), 0, 0, copyImg.Width, copyImg.Height, GraphicsUnit.Pixel, imageAttributes);
try
{
//以jpg格式保存缩略图
bitmap.Save(newImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
originaImg.Dispose();
bitmap.Dispose();
grPhoto.Dispose();
if (File.Exists(originalImgPath))
{ File.Delete(originalImgPath); }
}
catch (System.Exception e)
{
throw e;
}
finally
{
originaImg.Dispose();
bitmap.Dispose();
grPhoto.Dispose();
}
}
.net图片加水印
最新推荐文章于 2022-11-17 14:06:13 发布