using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using Gynova.LogManager;
namespace Gynova.Function
{
public class WaterImageManager
{
//加图片
public static bool DrawImage(string sourcePicturePath, string waterPicturePath, string destImagePath, int x, int y, int w, int h, ImageFormat picFormat, float alpha = 1.0f)
{
//
// 将需要加上水印的图片装载到Image对象中
//
Image imgPhoto = Image.FromFile(sourcePicturePath);
//
// 确定其长宽
//
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(waterPicturePath);
//
// 获取水印图片的高度和宽度
//
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);
//
//上面设置完颜色,下面开始设置位置
//
imgPhoto.Dispose();//释放底图,解决图片保存时 “GDI+ 中发生一般性错误。”
// 第二次绘图,把水印印上去
//
grWatermark.DrawImage(imgWatermark,
new Rectangle(x,
y,
w,
h),
0,
0,
wmWidth,
wmHeight,
GraphicsUnit.Pixel,
imageAttributes);
imgPhoto = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose();
try
{
imgPhoto.Save(destImagePath, picFormat);
}
catch (Exception e)
{
LogManager.LogManager.Instance.Write("DrawImage:" + e.Message, MsgType.Error);
return false;
}
imgPhoto.Dispose();
imgWatermark.Dispose();
return true;
}
//加文字
public static bool DrawWords(string sourcePicturePath, string waterWords, string destImagePath, int fontSize,Color color,float x, float y, ImageFormat picFormat)
{
//创建一个图片对象用来装载要被添加水印的图片
Image imgPhoto = Image.FromFile(sourcePicturePath);
//获取图片的宽和高
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); // 描绘的单位,这里用的是像素
//字体
Font crFont = new Font("微软雅黑",fontSize,FontStyle.Regular);
imgPhoto.Dispose();//释放底图,解决图片保存时 “GDI+ 中发生一般性错误。”
//封装文本布局信息(如对齐、文字方向和 Tab 停靠位),显示操作(如省略号插入和国家标准 (National) 数字替换)和 OpenType 功能。
StringFormat StrFormat = new StringFormat();
//定义需要印的文字居中对齐
StrFormat.Alignment = StringAlignment.Center;
SolidBrush semiTransBrush = new SolidBrush(color);
//第二次绘制这个图形,建立在第一次描绘的基础上
grPhoto.DrawString(waterWords, //string of text
crFont, //font
semiTransBrush, //Brush
new PointF(x, y), //Position
StrFormat);
//imgPhoto是我们建立的用来装载最终图形的Image对象
//bmPhoto是我们用来制作图形的容器,为Bitmap对象
imgPhoto = bmPhoto;
//释放资源,将定义的Graphics实例grPhoto释放,grPhoto功德圆满
grPhoto.Dispose();
try
{
imgPhoto.Save(destImagePath, picFormat);
}
catch (Exception e)
{
LogManager.LogManager.Instance.Write("DrawWords:" + e.Message, MsgType.Error);
return false;
}
imgPhoto.Dispose();
return true;
}
}
}