博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#给图片加文字和图片的水印
阅读量:5119 次
发布时间:2019-06-13

本文共 9144 字,大约阅读时间需要 30 分钟。

///      /// WaterMark 的摘要说明     ///      /// 图片加水印    /// 要加入的文字     /// 水印图片路径     /// 要加水印的图片路径     /// 处理后的图片路径     /// 水印在修改图片中距右边的宽度     /// 水印在修改图片中距底部的高度     /// 水印图片的透明度     /// 文字     /// 文字     /// 文字     /// 是否显示文字     /// 是否显示水印图片     public class WaterMark    {        #region  param        private string strCopyright, strMarkPath, strPhotoPath, strSavePath;        private int iMarkRightSpace, iMarkButtomSpace, iDiaphaneity;        private int iFontRightSpace = 0, iFontButtomSpace = 0, iFontDiaphaneity = 80;        private int iFontSize = 10;        private bool bShowCopyright = true, bShowMarkImage = true;        #endregion        #region WaterMark        public WaterMark()        {            this.strCopyright = "";            this.strMarkPath = null;            this.strPhotoPath = null;            this.strSavePath = null;            this.iDiaphaneity = 70;            this.iMarkRightSpace = 0;            this.iMarkButtomSpace = 0;        }        ///          /// 主要用两样都加的        ///          public WaterMark(string copyright, string markPath, string photoPath, string savePath)        {            this.strCopyright = copyright;            this.strMarkPath = markPath;            this.strPhotoPath = photoPath;            this.strSavePath = savePath;            this.iDiaphaneity = 70;            this.iMarkRightSpace = 0;            this.iMarkButtomSpace = 0;        }        #endregion        #region property        ///          /// 设置是否显示水印文字        ///          public bool ShowCopyright        {            set { this.bShowCopyright = value; }        }        ///          /// 设置是否显示水印图片        ///          public bool ShowMarkImage        {            set { this.bShowMarkImage = value; }        }        ///          /// 获取或设置要加入的文字        ///          public string Copyright        {            set { this.strCopyright = value; }        }        ///          /// 获取或设置加水印后的图片路径        ///          public string SavePath        {            get { return this.strSavePath; }            set { this.strSavePath = value; }        }        ///          /// 获取或设置水印图片路径        ///          public string MarkPath        {            get { return this.strMarkPath; }            set { this.strMarkPath = value; }        }        ///          /// 获取或设置要加水印图片的路径        ///          public string PhotoPath        {            get { return this.strPhotoPath; }            set { this.strPhotoPath = value; }        }        ///          /// 设置水印图片的透明度        ///          public int Diaphaneity        {            set            {                if (value > 0 && value <= 100)                    this.iDiaphaneity = value;            }        }        ///          /// 设置水印字体的透明度0-255        ///          public int FontDiaphaneity        {            set            {                if (value >= 0 && value <= 255)                    this.iFontDiaphaneity = value;            }        }        ///          /// 设置水印图片在修改图片中距左边的高度        ///          public int MarkRightSpace        {            set { this.iMarkRightSpace = value; }        }        ///          /// 设置水印图片在修改图片中距底部的高度        ///          public int MarkButtomSpace        {            set { this.iMarkButtomSpace = value; }        }        ///          /// 设置水印字体在修改图片中距左边的距离        ///          public int FontRightSpace        {            set { iFontRightSpace = value; }        }        ///          /// 设置水印字体在修改图片中距底部的高度        ///          public int FontButtomSpace        {            set { iFontButtomSpace = value; }        }        #endregion        ///          /// 生成水印图片        ///          /// 
public void createMarkPhoto() { Bitmap bmWatermark = null; FileStream fileStream = new FileStream(this.strPhotoPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); Image gPhoto = Image.FromStream(fileStream); int PhotoWidth = gPhoto.Width; int PhotoHeight = gPhoto.Height; //Bitmap bitPhoto = new Bitmap(PhotoWidth, PhotoHeight, PixelFormat.Format24bppRgb); Bitmap bitPhoto = new Bitmap(PhotoWidth, PhotoHeight); bitPhoto.SetResolution(gPhoto.HorizontalResolution, gPhoto.VerticalResolution); try { if (bShowCopyright) { Graphics grPhoto = Graphics.FromImage(bitPhoto); //grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//这句代码的作用是让合成模式为自动反锯齿,也就是所谓的"模糊" grPhoto.DrawImage(gPhoto, new Rectangle(0, 0, PhotoWidth, PhotoHeight), 0, 0, PhotoWidth, PhotoHeight, GraphicsUnit.Pixel); Font crFont = new Font("楷体", iFontSize, FontStyle.Bold); SizeF crSize = grPhoto.MeasureString(strCopyright, crFont); //设置字体在图片中的位置 float yPosFromBottom = PhotoHeight - iFontButtomSpace - (crSize.Height); //float xCenterOfImg = (phWidth/2); float xCenterOfImg = PhotoWidth - iFontRightSpace - (crSize.Width / 2); //设置字体居中 StringFormat StrFormat = new StringFormat(); StrFormat.Alignment = StringAlignment.Center; //设置绘制文本的颜色和纹理 (Alpha=153) SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(this.iFontDiaphaneity, 0, 0, 0)); //将版权信息绘制到图象上 grPhoto.DrawString(strCopyright, crFont, semiTransBrush2, new PointF(xCenterOfImg, yPosFromBottom), StrFormat); gPhoto = bitPhoto; grPhoto.Dispose(); } if (bShowMarkImage) { //创建一个需要填充水银的Image对象 Image imgWatermark = new Bitmap(strMarkPath); int iMarkWidth = imgWatermark.Width; int iMarkmHeight = imgWatermark.Height; Graphics grWatermark = null; if (bShowCopyright) { //在原来修改过的bmPhoto上创建一个水银位图 bmWatermark = new Bitmap(bitPhoto); bmWatermark.SetResolution(gPhoto.HorizontalResolution, gPhoto.VerticalResolution); } else { bmWatermark = new Bitmap(gPhoto); } //将位图bmWatermark加载到Graphics对象 grWatermark = Graphics.FromImage(bmWatermark); ImageAttributes imageAttributes = new ImageAttributes(); 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}, new float[] {
0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {
0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {
0.0f, 0.0f, 0.0f, (float)iDiaphaneity/100f, 0.0f}, new float[] {
0.0f, 0.0f, 0.0f, 0.0f, 1.0f}}; ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); grWatermark.DrawImage(imgWatermark, new Rectangle((PhotoWidth - iMarkRightSpace - (iMarkWidth / 2)), (PhotoHeight - iMarkButtomSpace - (iMarkmHeight / 2)), iMarkWidth, iMarkmHeight), 0, 0, iMarkWidth, iMarkmHeight, GraphicsUnit.Pixel, imageAttributes); gPhoto = bmWatermark; grWatermark.Dispose(); imgWatermark.Dispose(); } //设置输出图片质量,默认输出是60%的清晰度 EncoderParameter ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); EncoderParameters eps = new EncoderParameters(1); eps.Param[0] = ep; ImageCodecInfo ic = GetCodecInfo("image/jpeg"); fileStream.Close(); gPhoto.Save(strSavePath, ic, eps); bmWatermark.Dispose(); bitPhoto.Dispose(); gPhoto.Dispose(); ep.Dispose(); eps.Dispose(); } finally { if (bitPhoto != null) bitPhoto.Dispose(); if (bmWatermark != null) bmWatermark.Dispose(); fileStream.Close(); gPhoto.Dispose(); } } private ImageCodecInfo GetCodecInfo(String mimeType) { ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo ici in CodecInfo) { if (ici.MimeType == mimeType) return ici; } return null; } }

 

转载于:https://www.cnblogs.com/blazeZzz/p/9558046.html

你可能感兴趣的文章
Sql Server 中由数字转换为指定长度的字符串
查看>>
Java 多态 虚方法
查看>>
万能的SQLHelper帮助类
查看>>
tmux的简单快捷键
查看>>
[Swift]LeetCode922.按奇偶排序数组 II | Sort Array By Parity II
查看>>
Html5 离线页面缓存
查看>>
《绿色·精简·性感·迷你版》易语言,小到不可想象
查看>>
Android打包key密码丢失找回
查看>>
VC6.0调试技巧(一)(转)
查看>>
类库与框架,强类型与弱类型的闲聊
查看>>
webView添加头视图
查看>>
php match_model的简单使用
查看>>
在NT中直接访问物理内存
查看>>
Intel HEX 文件格式
查看>>
SIP服务器性能测试工具SIPp使用指导(转)
查看>>
回调没用,加上iframe提交表单
查看>>
(安卓)一般安卓开始界面 Loding 跳转 实例 ---亲测!
查看>>
Mysql 索引优化 - 1
查看>>
LeetCode(3) || Median of Two Sorted Arrays
查看>>
大话文本检测经典模型:EAST
查看>>