1
using
System;
2
using
System.Data;
3
using
System.Configuration;
4
using
System.Collections;
5
using
System.Web;
6
using
System.Web.Security;
7
using
System.Web.UI;
8
using
System.Web.UI.WebControls;
9
using
System.Web.UI.WebControls.WebParts;
10
using
System.Web.UI.HtmlControls;
11
using
System.IO;
12
13
public
partial
class
upfile_upfile : System.Web.UI.Page
14
{
15
protected void Page_Load(object sender, EventArgs e)
16
{ }
17
18
protected void Button1_Click(object sender, EventArgs e)
19
{
20
if (FileUpload1.HasFile)
21
{
22
string fileContentType = FileUpload1.PostedFile.ContentType;
23
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
24
{
25
string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
26
27
FileInfo file = new FileInfo(name);
28
string fileName = file.Name; // 文件名称
29
string fileName_s = "s_" + file.Name; // 缩略图文件名称
30
string fileName_sy = "sy_" + file.Name; // 水印图文件名称(文字)
31
string fileName_syp = "syp_" + file.Name; // 水印图文件名称(图片)
32
string webFilePath = Server.MapPath("file/" + fileName); // 服务器端文件路径
33
string webFilePath_s = Server.MapPath("file/" + fileName_s); // 服务器端缩略图路径
34
string webFilePath_sy = Server.MapPath("file/" + fileName_sy); // 服务器端带水印图路径(文字)
35
string webFilePath_syp = Server.MapPath("file/" + fileName_syp); // 服务器端带水印图路径(图片)
36
string webFilePath_sypf = Server.MapPath("file/shuiyin.jpg"); // 服务器端水印图路径(图片)
37
38
if (!File.Exists(webFilePath))
39
{
40
try
41
{
42
FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
43
AddShuiYinWord(webFilePath, webFilePath_sy);
44
AddShuiYinPic(webFilePath, webFilePath_syp, webFilePath_sypf);
45
MakeThumbnail(webFilePath, webFilePath_s, 130, 130, "Cut"); // 生成缩略图方法
46
Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" +
47
FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
48
}
49
catch (Exception ex)
50
{
51
Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
52
}
53
}
54
else
55
{
56
Label1.Text = "提示:文件已经存在,请重命名后上传";
57
}
58
}
59
else
60
{
61
Label1.Text = "提示:文件类型不符";
62
}
63
}
64
}
65
/**//**/
66
/**//// <summary>
67
/// 生成缩略图
68
/// </summary>
69
/// <param name="originalImagePath">源图路径(物理路径)</param>
70
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
71
/// <param name="width">缩略图宽度</param>
72
/// <param name="height">缩略图高度</param>
73
/// <param name="mode">生成缩略图的方式</param>
74
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
75
{
76
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
77
78
int towidth = width;
79
int toheight = height;
80
81
int x = 0;
82
int y = 0;
83
int ow = originalImage.Width;
84
int oh = originalImage.Height;
85
86
switch (mode)
87
{
88
case "HW"://指定高宽缩放(可能变形)
89
break;
90
case "W"://指定宽,高按比例
91
toheight = originalImage.Height * width / originalImage.Width;
92
break;
93
case "H"://指定高,宽按比例
94
towidth = originalImage.Width * height / originalImage.Height;
95
break;
96
case "Cut"://指定高宽裁减(不变形)
97
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
98
{
99
oh = originalImage.Height;
100
ow = originalImage.Height * towidth / toheight;
101
y = 0;
102
x = (originalImage.Width - ow) / 2;
103
}
104
else
105
{
106
ow = originalImage.Width;
107
oh = originalImage.Width * height / towidth;
108
x = 0;
109
y = (originalImage.Height - oh) / 2;
110
}
111
break;
112
default:
113
break;
114
}
115
116
//新建一个bmp图片
117
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
118
119
//新建一个画板
120
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
121
122
//设置高质量插值法
123
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
124
125
//设置高质量,低速度呈现平滑程度
126
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
127
128
//清空画布并以透明背景色填充
129
g.Clear(System.Drawing.Color.Transparent);
130
131
//在指定位置并且按指定大小绘制原图片的指定部分
132
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
133
new System.Drawing.Rectangle(x, y, ow, oh),
134
System.Drawing.GraphicsUnit.Pixel);
135
136
try
137
{
138
//以jpg格式保存缩略图
139
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
140
}
141
catch (System.Exception e)
142
{
143
throw e;
144
}
145
finally
146
{
147
originalImage.Dispose();
148
bitmap.Dispose();
149
g.Dispose();
150
}
151
}
152
153
/**//**/
154
/**//// <summary>
155
/// 在图片上增加文字水印
156
/// </summary>
157
/// <param name="Path">原服务器图片路径</param>
158
/// <param name="Path_sy">生成的带文字水印的图片路径</param>
159
protected void AddShuiYinWord(string Path, string Path_sy)
160
{
161
string addText = "测试水印";
162
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
163
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
164
g.DrawImage(image, 0, 0, image.Width, image.Height);
165
System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);
166
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
167
168
g.DrawString(addText, f, b, 15, 15);
169
g.Dispose();
170
171
image.Save(Path_sy);
172
image.Dispose();
173
}
174
175
/**//**/
176
/**//// <summary>
177
/// 在图片上生成图片水印
178
/// </summary>
179
/// <param name="Path">原服务器图片路径</param>
180
/// <param name="Path_syp">生成的带图片水印的图片路径</param>
181
/// <param name="Path_sypf">水印图片路径</param>
182
protected void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)
183
{
184
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
185
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
186
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
187
g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
188
g.Dispose();
189
190
image.Save(Path_syp);
191
image.Dispose();
192
}
193
}
194

2

3

4

5

6

7

8

9

10

11

12

13

14



15

16



17

18

19



20

21



22

23

24



25

26

27

28

29

30

31

32

33

34

35

36

37

38

39



40

41



42

43

44

45

46

47

48

49

50



51

52

53

54

55



56

57

58

59

60



61

62

63

64

65


66


67

68

69

70

71

72

73

74

75



76

77

78

79

80

81

82

83

84

85

86

87



88

89

90

91

92

93

94

95

96

97

98



99

100

101

102

103

104

105



106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137



138

139

140

141

142



143

144

145

146



147

148

149

150

151

152

153


154


155

156

157

158

159

160



161

162

163

164

165

166

167

168

169

170

171

172

173

174

175


176


177

178

179

180

181

182

183



184

185

186

187

188

189

190

191

192

193

194
