• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

水印图片的制作及常用的图像处理(C#)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  水印制作就是简要来说利用GDI+的函数,进行原图和水印图片的合成,或者在原图上配置文字。

  水印制作的关键函数:

  1. DrawString方法绘制文字
  2. DrawImage方法绘制图片

这两个函数有比较多重载,具体请参考MSDN。

说到GDI+,他一般用于Winform对于GUI的绘制,例如桌面上的窗体。其实GDI不仅可以绘制窗体,它可以绘制一切的表面图像。

  其中Graphics绘制图像最核心的类,该对象就像一张画图画的画布,我们可以绘制图像的方法在其上绘图。一个应用程序如果需要绘制和着色,它就必须使用该对象。

此外不得不说还有两个对象:Image类和BitMap类。这两个对象也比较重要。

首先是Image类,该类提供了位图和元文件操作的函数,它是一个抽象类,不能实例化,只能用作基类,我们主要用它来加载一些文件,生成Image对象,以便对位图进一步操作。

其次是BitMap对象,用于由像素数据定义的图像,它也是进行图像处理的主要类。我们可以取得或者设置它的一些属性,还可用它读入和保存一些图像。

下面就进入我们的主题:

一、绘制文字水印

View Code
 private static void drawText(Graphics g, String wmText, Font font, PointF p, StringFormat format)
        {

            SolidBrush brush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
            g.DrawString(wmText, font, brush, p, format);

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置g的呈现质量
            //用指定的笔刷,和字体绘制文字
            SolidBrush brush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
            g.DrawString(wmText, font, brush2, new PointF(p.X + 1, p.Y + 1), format);
  
        }

二、绘制图片水印

  图片水印主要是两个图像的合成,重点是要设置图片的水印的透明度,透明度一般降为原来的30%,这里将使用colorMatrix来做。矩阵在很多地方的被合使用,主要有矩阵进行像素的高速变换(其它方法处理速度相对较慢),特别是变换的地方。仔细观察矩阵对角线数字,1,1,1,0.3,1;这些是倍数运算因子,对应RGBAW的变换倍数。RGB不变,透明度为原来的0.3倍。通过这种方法就将图片的透明度降低了。

设置图像特性,主要是透明度的代码:

View Code
       private static ImageAttributes getImageAttributes()
        {
             ImageAttributes imgAttr = 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 };

             imgAttr.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)(ImageTransparencyNum/100.0), 0.0f},
                                           new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
                                        };

             ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
             imgAttr.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

             return imgAttr;
        }

绘制图片水印核心的代码:

View Code
       public static Bitmap AddWaterMark_IMG(String srcPath, String watermarkPath, WaterMarkPosition wp = WaterMarkPosition.BottomRight)
        {
            using (Image src = Image.FromFile(srcPath))
            {
                using (Bitmap bm = new Bitmap(srcPath))
                {
                    using (Graphics g = Graphics.FromImage(bm))
                    {
                        using (Image wm = Image.FromFile(watermarkPath))
                        {
                           
                            ImageAttributes imgAttr = getImageAttributes();
                             Rectangle rect = getPicRectangle(src, wm, wp);

                            g.DrawImage(wm, rect, 0, 0, wm.Width, wm.Height, GraphicsUnit.Pixel, imgAttr);

                            return new Bitmap((Image)(bm.Clone()));

                        }
                    }
                }
            }

        }

有时灰度化图像也比较常用,这个如果也用像素法处理也是较慢的,我们也采用矩阵处理

View Code
public static Bitmap GrayPicuure(String srcPicturePath)
        {
            using (Bitmap currentBitmap = new Bitmap(srcPicturePath))
            {
                using (Graphics g = Graphics.FromImage(currentBitmap))
                {
                    using (ImageAttributes ia = new ImageAttributes())
                    {       
                        float[][] colorMatrix = {     
                                            new   float[]   {0.299f,   0.299f,   0.299f,   0,   0},
                                            new   float[]   {0.587f,   0.587f,   0.587f,   0,   0},
                                            new   float[]   {0.114f,   0.114f,   0.114f,   0,   0},
                                            new   float[]   {0,   0,   0,   1,   0},
                                            new   float[]   {0,   0,   0,   0,   1}
                                            };
                        //颜色调整矩阵
                        ColorMatrix cm = new ColorMatrix(colorMatrix);

                        ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                        g.DrawImage(currentBitmap, new Rectangle(0, 0, currentBitmap.Width, currentBitmap.Height), 0, 0, currentBitmap.Width, currentBitmap.Height, GraphicsUnit.Pixel, ia);

                        return new Bitmap((Image)(currentBitmap.Clone()));
                    }
                }
            }
        }

此外我们在图像处理时也会翻转旋转图像,这里主要用Image类的RotateFlip方法处理

View Code
public static Bitmap RotateFlipPicture(String srcPicturePath, RotateFlipType RotateFlipType)
        {
            using (Image tmp = Image.FromFile(srcPicturePath))
            {
                tmp.RotateFlip(RotateFlipType);
                return new Bitmap(tmp);
            }   
        }

图像处理内容比较多,在此不一列举,这里只给出常用的,下面给出全部源代码

View Code
  1 /*
  2  * Copyright 2012 www.vision.net.cn
  3  * 
  4  * Licensed under the Apache License, Version 2.0 (the "License");
  5  * you may not use this file except in compliance with the License.
  6  * You may obtain a copy of the License at
  7  * 
  8  *      http://www.apache.org/licenses/LICENSE-2.0
  9  * 
 10  * Unless required by applicable law or agreed to in writing, software
 11  * distributed under the License is distributed on an "AS IS" BASIS,
 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  * See the License for the specific language governing permissions and
 14  * limitations under the License.
 15  */
 16 
 17 using System;
 18 using System.IO;
 19 using System.Drawing;
 20 using System.Drawing.Imaging;
 21 using System.Windows.Forms;
 22 
 23 namespace myDrawing
 24 {
 25     /// <summary>
 26     /// 水印的位置
 27     /// </summary>
 28     public enum WaterMarkPosition
 29     {
 30         /// <summary>
 31         /// 左上角
 32         /// </summary>
 33         TopLeft,
 34         /// <summary>
 35         /// 左下角
 36         /// </summary>
 37         BottomLeft,
 38         /// <summary>
 39         /// 右上角
 40         /// </summary>
 41         TopRight,
 42         /// <summary>
 43         /// 右下角
 44         /// </summary>
 45         BottomRight,
 46         /// <summary>
 47         /// 顶部中间
 48         /// </summary>
 49         TopCenter,
 50         /// <summary>
 51         /// 底部中间
 52         /// </summary>
 53         BottomCenter
 54     };
 55     /// <summary>
 56     /// 字体
 57     /// </summary>
 58     public enum myFont
 59     { 
 60         /// <summary>
 61         /// 字体:中文宋体
 62         /// </summary>
 63         宋体,
 64         /// <summary>
 65         /// 字体:中文楷体
 66         /// </summary>
 67         楷体,
 68         /// <summary>
 69         /// 字体:中文仿宋
 70         /// </summary>
 71         仿宋,
 72         /// <summary>
 73         /// 字体:中文黑体
 74         /// </summary>
 75         黑体,
 76         /// <summary>
 77         /// 字体:Arial
 78         /// </summary>
 79         Arial,
 80         /// <summary>
 81         /// 字体:Georgia
 82         /// </summary>
 83         Georgia
 84     }
 85     /// <summary>
 86     /// 水印工具
 87     /// </summary>
 88     public class Watermark
 89     {
 90         private static int imageTransparency=0;
 91         /// <summary>
 92         /// 水印图像的透明度,默认30%透明
 93         /// </summary>
 94         public static int ImageTransparencyNum
 95         {
 96             get { if (imageTransparency == 0)imageTransparency = 30; return imageTransparency; }
 97             set
 98             {
 99                 if (imageTransparency >=0 && imageTransparency <= 100) imageTransparency = value;
100             }
101         }
102         /// <summary>
103         /// 取得设置的图像特性,主要是透明度
104         /// </summary>
105         /// <returns></returns>
106         private static ImageAttributes getImageAttributes()
107         {
108              ImageAttributes imgAttr = new ImageAttributes();
109 
110              ColorMap colorMap = new ColorMap();
111              colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
112              colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
113              ColorMap[] remapTable = { colorMap };
114 
115              imgAttr.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
116 
117              float[][] colorMatrixElements = { 
118                                            new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
119                                            new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
120                                            new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
121                                            new float[] {0.0f,  0.0f,  0.0f,  (float)(ImageTransparencyNum/100.0), 0.0f},
122                                            new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
123                                         };
124 
125              ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
126              imgAttr.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
127 
128              return imgAttr;
129         }
130         /// <summary>
131         /// 添加图片水印:将原始图片添加图片水印之后存储
132         /// </summary>
133         /// <param name="srcPath">原图片绝对地址</param>
134         /// <param name="watermarkPath">水印图片路径</param>
135         /// <param name="wp">水印位置的枚举值</param>
136         public static Bitmap AddWaterMark_IMG(String srcPath, String watermarkPath, WaterMarkPosition wp = WaterMarkPosition.BottomRight)
137         {
138             using (Image src = Image.FromFile(srcPath))
139             {
140                 using (Bitmap bm = new Bitmap(srcPath))
141                 {
142                     using (Graphics g = Graphics.FromImage(bm))
143                     {
144                         using (Image wm = Image.FromFile(watermarkPath))
145                         {
146                            
147                             ImageAttributes imgAttr = getImageAttributes();
148                              Rectangle rect = getPicRectangle(src, wm, wp);
149 
150                             g.DrawImage(wm, rect, 0, 0, wm.Width, wm.Height, GraphicsUnit.Pixel, imgAttr);
151 
152                             return new Bitmap((Image)(bm.Clone()));
153 
154                         }
155                     }
156                 }
157             }
158 
159         }
160       
161         /// <summary>
162         /// 取得绘制水印的矩形
163         /// </summary>
164         /// <param name="src">原图像映像</param>
165         /// <param name="wm">水印图像映像</param>
166         /// <param name="wp">指定的水印位置枚举值WaterMarkPosition</param>
167         /// <returns></returns>
168         private static Rectangle getPicRectangle(Image src, Image wm, WaterMarkPosition wp)
169         {
170 
171             int xpos = 10;//水印图像X坐标
172             int ypos = 10;//水印图像y坐标
173 
174             switch (wp)
175             {
176                 case WaterMarkPosition.TopLeft:
177                     xpos = 10;
178                     ypos = 10;
179                     break;
180                 case WaterMarkPosition.TopCenter:
181                     xpos = src.Width / 2 - wm.Width / 2;
182                     ypos = 10;
183                     break;
184                 case WaterMarkPosition.TopRight:
185                     xpos = ((src.Width - wm.Width) - 10);
186                     ypos = 10;
187                     break;
188                 case WaterMarkPosition.BottomLeft:
189                     xpos = 10;
190                     ypos = src.Height - wm.Height - 10;
191                     break;
192                 case WaterMarkPosition.BottomRight:
193                     xpos = ((src.Width - wm.Width) - 10); 
194                     ypos = src.Height - wm.Height - 10;
195                     break;
196                 case WaterMarkPosition.BottomCenter:
197                     xpos = src.Width / 2 - wm.Width / 2;
198                     ypos = src.Height - wm.Height - 10;
199                     break;
200             }
201             return new Rectangle(xpos, ypos, wm.Width, wm.Height);
202         }
203 
204         /// <summary>
205         /// 添加文字水印:将原始图片添加文字水印之后存储
206         /// </summary>
207         /// <param name="srcPath">原图片绝对地址</param>
208         /// <param name="words">要添加的水印文本</param>
209         /// <param name="wp">水印位置WaterMarkPosition,参数可数</param>
210         /// <param name="fontSize">字体大小,可选</param>
211         /// <param name="myfont">字体myFont,可选</param>
212         public static Bitmap AddWaterMark_WORD(String srcPath, String words, WaterMarkPosition wp = WaterMarkPosition.BottomRight, int fontSize = 40, myFont myfont = myFont.楷体)
213         {
214 
215             using (Image src = Image.FromFile(srcPath))
216             {
217                 using (Bitmap bm = new Bitmap(srcPath))
218                 {
219                     using (Graphics g = Graphics.FromImage(bm))
220                     {
221                         //font定义特定的文字格式,包括文本字体,字号大小,和字形属性。
222                         Font crFont = new Font(myfont.ToString(), fontSize, FontStyle.Regular);
223                         //Sizef存储有序的浮点数对。通常为矩形的宽度和高度。(Drawing.SizeF)
224                         SizeF crSize = g.MeasureString(words, crFont);
225                         //FontAndSize fs = FontAndSize.GetValue(g, words, "arial", fontSize, src.Width);
226                         PointF p = getTextPoint(src, crSize, wp);
227                         StringFormat sf = getStringFormat();
228 
229                         drawText(g, words, crFont, p, sf);
230 
231                         return new Bitmap((Image)(bm.Clone()));
232                     }
233                 }
234             }
235 
236         }
237         /// <summary>
238         /// 绘制文本对象
239         /// </summary>
240         /// <param name="g">Graphics画面 对象</param>
241         /// <param name="wmText">水印文字的内容</param>
242         /// <param name="font">水印文字的字体</param>
243         /// <param name="p">水印文字的位置坐标有序对</param>
244         /// <param name="format">水印广本的格式化格式</param>
245         private static void drawText(Graphics g, String wmText, Font font, PointF p, StringFormat format)
246         {
247 
248             SolidBrush brush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
249             g.DrawString(wmText, font, brush, p, format);
250 
251             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置g的呈现质量
252             //用指定的笔刷,和字体绘制文字
253             SolidBrush brush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
254             g.DrawString(wmText, font, brush2, new PointF(p.X + 1, p.Y + 1), format);
255   
256         }
257         /// <summary>
258         /// 取得水印的格式格式
259         /// </summary>
260         /// <returns>StringFormat格式</returns>
261         private static StringFormat getStringFormat()
262         {
263             StringFormat StrFormat = new StringFormat();
264             StrFormat.Alignment = StringAlignment.Center;
265             return StrFormat;
266         }
 
                       
                    
                    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#计算时间差发布时间:2022-07-18
下一篇:
Java和C#访问同一个数据库遇到的一些问题发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap