1
using System; 2
using System.Collections.Generic; 3
using System.Text; 4
5
using System.IO; 6
using System.Drawing; 7
using System.Drawing.Imaging; 8
using System.Drawing.Drawing2D; 9
10
namespace DocMIS.AppConfig 11


{ 12

/**//**//**//// < summary> 13
/// 水印位置 14
/// < /summary> 15
public enum ImagePosition 16

{ 17

/**//**//**//// < summary> 18
/// 左上 19
/// < /summary> 20
LeftTop, 21

/**//**//**//// < summary> 22
/// 左下 23
/// < /summary> 24
LeftBottom, 25

/**//**//**//// < summary> 26
/// 右上 27
/// < /summary> 28
RightTop, 29

/**//**//**//// < summary> 30
/// 右下 31
/// < /summary> 32
RigthBottom, 33

/**//**//**//// < summary> 34
/// 顶部居中 35
/// < /summary> 36
TopMiddle, 37

/**//**//**//// < summary> 38
/// 底部居中 39
/// < /summary> 40
BottomMiddle, 41

/**//**//**//// < summary> 42
/// 中心 43
/// < /summary> 44
Center 45
} 46
47

/**//**//**//// < summary> 48
/// 图像操作类(主要用于给图片加上透明文字水印) 49
/// < /summary> 50
class ImageWater_Word 51

{ 52
private string _ErrMsg; 53

出错信息#region 出错信息 54

/**//**//**//// < summary> 55
/// 出错信息 56
/// < /summary> 57
public string ErrMsg 58

{ 59

get
{ return _ErrMsg; } 60

set
{ _ErrMsg = value; } 61
} 62
#endregion 63
64
65

将文件转换成流#region 将文件转换成流 66
//public byte[] SetImageToByteArray(string fileName, ref string fileSize) 67

/**//**//**//// < summary> 68
/// 将文件转换成流 69
/// < /summary> 70
/// < param name="fileName">文件全路径< /param> 71
/// < returns>< /returns> 72
private byte[] SetImageToByteArray(string fileName) 73

{ 74
byte[] image = null; 75
try 76

{ 77
FileStream fs = new FileStream(fileName, FileMode.Open); 78
FileInfo fileInfo = new FileInfo(fileName); 79
//fileSize = Convert.ToDecimal(fileInfo.Length / 1024).ToString("f2") + " K"; 80
int streamLength = (int)fs.Length; 81
image = new byte[streamLength]; 82
fs.Read(image, 0, streamLength); 83
fs.Close(); 84
return image; 85
} 86
catch 87

{ 88
return image; 89
} 90
} 91
#endregion 92
93

将byte转换成MemoryStream类型#region 将byte转换成MemoryStream类型 94

/**//**//**//// < summary> 95
/// ASP.NET图片加水印:将byte转换成MemoryStream类型 96
/// < /summary> 97
/// < param name="mybyte">byte[]变量< /param> 98
/// < returns>< /returns> 99
private MemoryStream ByteToStream(byte[] mybyte) 100

{ 101
MemoryStream mymemorystream = new MemoryStream(mybyte, 0, mybyte.Length); 102
return mymemorystream; 103
} 104
#endregion 105
106

将byte转换成Image文件#region 将byte转换成Image文件 107

/**//**//**//// < summary> 108
/// ASP.NET图片加水印:将byte转换成Image文件 109
/// < /summary> 110
/// < param name="mybyte">byte[]变量< /param> 111
/// < returns>< /returns> 112
private System.Drawing.Image SetByteToImage(byte[] mybyte) 113

{ 114
System.Drawing.Image image; 115
MemoryStream mymemorystream = new MemoryStream(mybyte, 0, mybyte.Length); 116
image = System.Drawing.Image.FromStream(mymemorystream); 117
return image; 118
} 119
#endregion 120
121
122

批量在图片上添加透明水印文字#region 批量在图片上添加透明水印文字 123

/**//**//**//// < summary> 124
/// ASP.NET图片加水印:批量在图片上添加透明水印文字 125
/// < /summary> 126
/// < param name="arrsourcePicture">原来图片地址(路径+文件名)< /param> 127
/// < param name="waterWords">需要添加到图片上的文字< /param> 128
/// < param name="alpha">透明度(0.1~1.0之间)< /param> 129
/// < param name="position">文字显示的位置< /param> 130
/// < param name="fRewrite">是否覆盖原图片(如果不覆盖,那么将在同目录下生成一个文件名带0607的文件)< /param> 131
/// < returns>< /returns> 132
public bool DrawWords(string[] arrsourcePicture, string waterWords, float alpha, ImagePosition position, bool fRewrite) 133

{ 134
foreach (string imgPath in arrsourcePicture) 135

{ 136
if (!DrawWords(imgPath, waterWords, alpha, position, fRewrite)) 137

{ 138
_ErrMsg += "——处理文件:" + imgPath + " 时出错。"; 139
return false; 140
} 141
} 142
return true; 143
} 144
#endregion 145
146

在图片上添加透明水印文字#region 在图片上添加透明水印文字 147

/**//**//**//// < summary> 148
/// ASP.NET图片加水印:在图片上添加透明水印文字 149
/// < /summary> 150
/// < param name="sourcePicture">原来图片地址(路径+文件名)< /param> 151
/// < param name="waterWords">需要添加到图片上的文字< /param> 152
/// < param name="alpha">透明度(0.1~1.0之间)< /param> 153
/// < param name="position">文字显示的位置< /param> 154
/// < param name="fRewrite">是否覆盖原图片(如果不覆盖,那么将在同目录下生成一个文件名带0607的文件)< /param> 155
/// < returns>< /returns> 156
public bool DrawWords(string sourcePicture, string waterWords, float alpha, ImagePosition position, bool fRewrite) 157

{ 158
if (!System.IO.File.Exists(sourcePicture)) 159

{ 160
_ErrMsg = "文件不存在!"; 161
return false; 162
} 163
string fileExtension = System.IO.Path.GetExtension(sourcePicture).ToLower(); 164
if (fileExtension != ".gif" && fileExtension != ".jpg" && fileExtension != ".png" && fileExtension != ".bmp") 165

{ 166
_ErrMsg = "不是图片文件!"; 167
return false; 168
} 169
170
Image imgPhoto = null; 171
Bitmap bmPhoto = null; 172
Graphics grPhoto = null; 173
try 174

{ 175
//创建一个图片对象用来装载要被添加水印的图片 176
imgPhoto = Image.FromStream(ByteToStream(SetImageToByteArray(sourcePicture))); 177
178
//获取图片的宽和高 179
int phWidth = imgPhoto.Width; 180
int phHeight = imgPhoto.Height; 181
182
//建立一个bitmap,和我们需要加水印的图片一样大小 183
bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb); 184
185
//SetResolution:设置此 Bitmap 的分辨率 186
//这里直接将我们需要添加水印的图片的分辨率赋给了bitmap 187
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); 188
189
//Graphics:封装一个 GDI+ 绘图图面。 190
grPhoto = Graphics.FromImage(bmPhoto); 191
192
//设置图形的品质 193
grPhoto.SmoothingMode = SmoothingMode.AntiAlias; 194
195
//将我们要添加水印的图片按照原始大小描绘(复制)到图形中 196
grPhoto.DrawImage( 197
imgPhoto, // 要添加水印的图片 198
new Rectangle(0, 0, phWidth, phHeight), // 根据要添加的水印图片的宽和高 199
0, // X方向从0点开始描绘 200
0, // Y方向 201
phWidth, // X方向描绘长度 202
phHeight, // Y方向描绘长度 203
GraphicsUnit.Pixel); // 描绘的单位,这里用的是像素 204
205
//根据图片的大小我们来确定添加上去的文字的大小 206
//在这里我们定义一个数组来确定 207

int[] sizes = new int[]
{ 48, 36, 28, 24, 16, 14, 12, 10 }; 208
209
//字体 210
Font crFont = null; 211
//矩形的宽度和高度,SizeF有三个属性,分别为Height高,width宽,IsEmpty是否为空 212
SizeF crSize = new SizeF(); 213
214
//利用一个循环语句来选择我们要添加文字的型号 215
//直到它的长度比图片的宽度小 216
for (int i = 0; i < sizes.Length; i++) 217

{ 218
crFont = new Font("arial", sizes[i], FontStyle.Bold); 219
220
//测量用指定的 Font 对象绘制并用指定的 StringFormat 对象格式化的指定字符串。 221
crSize = grPhoto.MeasureString(waterWords, crFont); 222
223
// ushort 关键字表示一种整数数据类型 224
if ((ushort)crSize.Width < (ushort)phWidth) 225
break; 226
} 227
228
//截边5%的距离,定义文字显示(由于不同的图片显示的高和宽不同,所以按百分比截取) 229
int yPixlesFromBottom = (int)(phHeight * .05); 230
231
//定义在图片上文字的位置 232
float wmHeight = crSize.Height; 233
float wmWidth = crSize.Width; 234
235
float xPosOfWm; 236
float yPosOfWm; 237
238
//设置水印的位置 239
switch (position) 240

{ 241
case ImagePosition.BottomMiddle: 242
xPosOfWm = phWidth / 2; 243
yPosOfWm = phHeight - wmHeight - 10; 244
break; 245
case ImagePosition.Center: 246
xPosOfWm = phWidth / 2; 247
yPosOfWm = phHeight / 2; 248
break; 249
case ImagePosition.LeftBottom: 250
xPosOfWm = wmWidth; 251
yPosOfWm = phHeight - wmHeight - 10; 252
break; 253
case ImagePosition.LeftTop: 254
xPosOfWm = wmWidth / 2; 255
yPosOfWm = wmHeight / 2; 256
break; 257
case ImagePosition.RightTop: 258
xPosOfWm = phWidth - wmWidth - 10; 259
yPosOfWm = wmHeight; 260
break; 261
case ImagePosition.RigthBottom: 262
xPosOfWm = phWidth - wmWidth - 10; 263
yPosOfWm = phHeight - wmHeight - 10; 264
break; 265
case ImagePosition.TopMiddle: 266
xPosOfWm = phWidth / 2; 267
yPosOfWm = wmWidth; 268
break; 269
default: 270
xPosOfWm = wmWidth; 271
yPosOfWm = phHeight - wmHeight - 10; 272
break; 273
} 274
//封装文本布局信息(如对齐、文字方向和 Tab 停靠位),显示操作(如省略号插入和国家标准 (National) 数字替换)和 OpenType 功能。 275
StringFormat StrFormat = new StringFormat(); 276
277
//定义需要印的文字居中对齐 278
StrFormat.Alignment = StringAlignment.Center; 279
280
//SolidBrush:定义单色画笔。画笔用于填充图形形状,如矩形、椭圆、扇形、多边形和封闭路径。 281
//这个画笔为描绘阴影的画笔,呈灰色 282
int m_alpha = Convert.ToInt32(256 * alpha); 283
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(m_alpha, 0, 0, 0)); 284
285
//描绘文字信息,这个图层向右和向下偏移一个像素,表示阴影效果 286
//DrawString 在指定矩形并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。 287
grPhoto.DrawString(waterWords, //string of text 288
crFont, //font 289
semiTransBrush2, //Brush 290
new PointF(xPosOfWm + 1, yPosOfWm + 1), //Position 291
StrFormat); 292
293
//从四个 ARGB 分量(alpha、红色、绿色和蓝色)值创建 Color 结构,这里设置透明度为153 294
//这个画笔为描绘正式文字的笔刷,呈白色 295
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255)); 296
297
//第二次绘制这个图形,建立在第一次描绘的基础上 298
grPhoto.DrawString(waterWords, //string of text 299
crFont, //font 300
semiTransBrush, //Brush 301
new PointF(xPosOfWm, yPosOfWm), //Position 302
StrFormat); 303
304
//imgPhoto是我们建立的用来装载最终图形的Image对象 305
//bmPhoto是我们用来制作图形的容器,为Bitmap对象 306
imgPhoto = bmPhoto; 307
//释放资源,将定义的Graphics实例grPhoto释放,grPhoto功德圆满 308
//grPhoto.Dispose(); 309
310
//将grPhoto保存 311
if (fRewrite) 312

{ 313
imgPhoto.Save(sourcePicture); 314
} 315
else 316

{ 317
// 目标图片名称及全路径 318
string targetImage = sourcePicture.Replace(System.IO.Path.GetExtension(sourcePicture), "") + "_0607" + fileExtension; 319
imgPhoto.Save(targetImage); 320
} 321
//imgPhoto.Dispose(); 322
return true; 323
} 324
catch (Exception ex) 325

{ 326
_ErrMsg = ex.Message; 327
return false; 328
} 329
finally 330

{ 331
if (imgPhoto != null) 332
imgPhoto.Dispose(); 333
if (bmPhoto != null) 334
bmPhoto.Dispose(); 335
if (grPhoto != null) 336
grPhoto.Dispose(); 337
} 338
339
340
} 341
#endregion 342
343
} 344
} 345

1
using System.IO;2
using System.Drawing.Imaging;3

4
private void Button1_ServerClick(object sender, System.EventArgs e)5


{6
Graphics g=null;7
System.Drawing.Image upimage=null;8
System.Drawing.Image thumimg=null;9
System.Drawing.Image simage=null;10
Bitmap outputfile=null;11
try12


{13
string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();14
string filename = DateTime.Now.ToString("yyyyMMddhhmmss");15
string smallpath = Server.MapPath(".")+"/smallimg/";16
string bigpath = Server.MapPath(".")+"/bigimg/";17
int width,height,newwidth,newheight;18

19
System.Drawing.Image.GetThumbnailImageAbort callb =new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);20
if(!Directory.Exists(smallpath))21
Directory.CreateDirectory(smallpath);22
if(!Directory.Exists(bigpath))23
Directory.CreateDirectory(bigpath);24

25
Stream upimgfile = File1.PostedFile.InputStream;26
string simagefile = Server.MapPath("a8logo.jpg"); //要加水印的文件 27
simage=System.Drawing.Image.FromFile(simagefile);28
upimage= System.Drawing.Image.FromStream(upimgfile); //上传的图片29

30
width = upimage.Width;31
height = upimage.Height;32
if(width>height)33


{34
newwidth=200;35
newheight =(int)((double)height/(double)width * (double)newwidth);36
}37
else38


{39
newheight=200;40
newwidth=(int)((double)width/(double)height * (double)newheight);41
}42
thumimg = upimage.GetThumbnailImage(newwidth,newheight,callb,IntPtr.Zero);43
outputfile=new Bitmap(upimage);44
g=Graphics.FromImage(outputfile);45
g.DrawImage(simage,new Rectangle(upimage.Width-simage.Width,upimage.Height-simage.Height,upimage.Width,upimage.Height),0,0,upimage.Width,upimage.Height,GraphicsUnit.Pixel);46

47
string newpath = bigpath + filename + extension; //原始图路径48
string thumpath = smallpath + filename + extension; //缩略图路径49
outputfile.Save(newpath);50
thumimg.Save(thumpath);51
outputfile.Dispose();52

53
}54
catch(Exception ex) 55

56

57


{58
throw ex;59
}60
finally61


{62
if(g!=null)63
g.Dispose();64
if(thumimg!=null)65
thumimg.Dispose();66
if(upimage!=null)67
upimage.Dispose();68
if(simage!=null)69
simage.Dispose();70
}71
}72
public bool ThumbnailCallback()73


{74
return false;75
}76

浙公网安备 33010602011771号