C#图片水印代码整理

这一段公司有个项目,客户要求上传的图片要带上自定义的水印。以前也经常和朋友讨论C#图片水印方面的问题,但是从来没有实际操作过。所以,借这次项目的机会也研究了一下C#图片水印的功能!本人参考的是discuz论坛中的代码。这种方法是直接把要上传的图片先转化成System.Drawing.Image,而不用保存到服务器端的磁盘上,然后加上水印重新绘制,再保存到服务器端的磁盘上,下面就拿出来晒晒:

1/// <summary>
2 /// 加图片水印
3 /// </summary>
4 /// <param name="img">要加水印的原图(System.Drawing)</param>
5 /// <param name="filename">文件名</param>
6 /// <param name="watermarkFilename">水印文件名</param>
7 /// <param name="watermarkStatus">图片水印位置1=左上 2=中上 3=右上 4=左中 5=中中 6=右中 7=左下 8=右中 9=右下</param>
8 /// <param name="quality">加水印后的质量0~100,数字越大质量越高</param>
9 /// <param name="watermarkTransparency">水印图片的透明度1~10,数字越小越透明,10为不透明</param>
10  public static void ImageWaterMarkPic(Image img, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
11 {
12 Graphics g = Graphics.FromImage(img);
13 //设置高质量插值法
14 //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
15 //设置高质量,低速度呈现平滑程度
16 //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
17 Image watermark = new Bitmap(watermarkFilename);
18
19 if (watermark.Height >= img.Height || watermark.Width >= img.Width)
20 return;
21
22 ImageAttributes imageAttributes = new ImageAttributes();
23 ColorMap colorMap = new ColorMap();
24
25 colorMap.OldColor = Color.FromArgb(25502550);
26 colorMap.NewColor = Color.FromArgb(0000);
27 ColorMap[] remapTable = { colorMap };
28
29 imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
30
31 float transparency = 0.5F;
32 if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
33 transparency = (watermarkTransparency / 10.0F);
34
35
36 float[][] colorMatrixElements = {
37 new float[] {1.0f0.0f0.0f0.0f0.0f},
38 new float[] {0.0f1.0f0.0f0.0f0.0f},
39 new float[] {0.0f0.0f1.0f0.0f0.0f},
40 new float[] {0.0f0.0f0.0f, transparency, 0.0f},
41 new float[] {0.0f0.0f0.0f0.0f1.0f}
42 };
43
44 ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
45
46 imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
47
48 int xpos = 0;
49 int ypos = 0;
50
51 switch (watermarkStatus)
52 {
53 case 1:
54 xpos = (int)(img.Width * (float).01);
55 ypos = (int)(img.Height * (float).01);
56 break;
57 case 2:
58 xpos = (int)((img.Width * (float).50- (watermark.Width / 2));
59 ypos = (int)(img.Height * (float).01);
60 break;
61 case 3:
62 xpos = (int)((img.Width * (float).99- (watermark.Width));
63 ypos = (int)(img.Height * (float).01);
64 break;
65 case 4:
66 xpos = (int)(img.Width * (float).01);
67 ypos = (int)((img.Height * (float).50- (watermark.Height / 2));
68 break;
69 case 5:
70 xpos = (int)((img.Width * (float).50- (watermark.Width / 2));
71 ypos = (int)((img.Height * (float).50- (watermark.Height / 2));
72 break;
73 case 6:
74 xpos = (int)((img.Width * (float).99- (watermark.Width));
75 ypos = (int)((img.Height * (float).50- (watermark.Height / 2));
76 break;
77 case 7:
78 xpos = (int)(img.Width * (float).01);
79 ypos = (int)((img.Height * (float).99- watermark.Height);
80 break;
81 case 8:
82 xpos = (int)((img.Width * (float).50- (watermark.Width / 2));
83 ypos = (int)((img.Height * (float).99- watermark.Height);
84 break;
85 case 9:
86 xpos = (int)((img.Width * (float).99- (watermark.Width));
87 ypos = (int)((img.Height * (float).99- watermark.Height);
88 break;
89 }
90
91 g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 00, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
92
93 ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
94 ImageCodecInfo ici = null;
95 foreach (ImageCodecInfo codec in codecs)
96 {
97 if (codec.MimeType.IndexOf("jpeg"> -1)
98 ici = codec;
99 }
100 EncoderParameters encoderParams = new EncoderParameters();
101 long[] qualityParam = new long[1];
102 if (quality < 0 || quality > 100)
103 quality = 80;
104
105 qualityParam[0= quality;
106
107 EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
108 encoderParams.Param[0= encoderParam;
109
110 if (ici != null)
111 img.Save(filename, ici, encoderParams);
112 else
113 img.Save(filename);
114
115 g.Dispose();
116 img.Dispose();
117 watermark.Dispose();
118 imageAttributes.Dispose();
119 }
120
121 /// <summary>
122 /// 增加图片文字水印
123 /// </summary>
124 /// <param name="img">要加水印的原图(System.Drawing)</param>
125 /// <param name="filename">文件名</param>
126 /// <param name="watermarkText">水印文字</param>
127 /// <param name="watermarkStatus">图片水印位置1=左上 2=中上 3=右上 4=左中 5=中中 6=右中 7=左下 8=右中 9=右下</param>
128 /// <param name="quality">加水印后的质量0~100,数字越大质量越高</param>
129 /// <param name="fontname">水印的字体</param>
130 /// <param name="fontsize">水印的字号</param>
131 public static void ImageWaterMarkText(Image img, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize)
132 {
133 Graphics g = Graphics.FromImage(img);
134 Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
135 SizeF crSize;
136 crSize = g.MeasureString(watermarkText, drawFont);
137
138 float xpos = 0;
139 float ypos = 0;
140
141 switch (watermarkStatus)
142 {
143 case 1:
144 xpos = (float)img.Width * (float).01;
145 ypos = (float)img.Height * (float).01;
146 break;
147 case 2:
148 xpos = ((float)img.Width * (float).50- (crSize.Width / 2);
149 ypos = (float)img.Height * (float).01;
150 break;
151 case 3:
152 xpos = ((float)img.Width * (float).99- crSize.Width;
153 ypos = (float)img.Height * (float).01;
154 break;
155 case 4:
156 xpos = (float)img.Width * (float).01;
157 ypos = ((float)img.Height * (float).50- (crSize.Height / 2);
158 break;
159 case 5:
160 xpos = ((float)img.Width * (float).50- (crSize.Width / 2);
161 ypos = ((float)img.Height * (float).50- (crSize.Height / 2);
162 break;
163 case 6:
164 xpos = ((float)img.Width * (float).99- crSize.Width;
165 ypos = ((float)img.Height * (float).50- (crSize.Height / 2);
166 break;
167 case 7:
168 xpos = (float)img.Width * (float).01;
169 ypos = ((float)img.Height * (float).99- crSize.Height;
170 break;
171 case 8:
172 xpos = ((float)img.Width * (float).50- (crSize.Width / 2);
173 ypos = ((float)img.Height * (float).99- crSize.Height;
174 break;
175 case 9:
176 xpos = ((float)img.Width * (float).99- crSize.Width;
177 ypos = ((float)img.Height * (float).99- crSize.Height;
178 break;
179 }
180
181 //g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);文字阴影
182 g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);
183
184 ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
185 ImageCodecInfo ici = null;
186 foreach (ImageCodecInfo codec in codecs)
187 {
188 if (codec.MimeType.IndexOf("jpeg"> -1)
189 ici = codec;
190 }
191 EncoderParameters encoderParams = new EncoderParameters();
192 long[] qualityParam = new long[1];
193 if (quality < 0 || quality > 100)
194 quality = 80;
195
196 qualityParam[0= quality;
197
198 EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
199 encoderParams.Param[0= encoderParam;
200
201 if (ici != null)
202 img.Save(filename, ici, encoderParams);
203 else
204 img.Save(filename);
205
206 g.Dispose();
207 img.Dispose();
208 }
209
原文地址:https://www.cnblogs.com/wwwzzg168/p/3572021.html