1
以下是引用片段:
2
private void btnUploadPicture_Click(object sender, System.EventArgs e)
3

{
4
//检查上传文件的格式是否有效
5
if(this.UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
6
{
7
Response.Write("上传图片格式无效!");
8
return;
9
}
10
11
//生成原图
12
Byte[] oFileByte = new byte[this.UploadFile.PostedFile.ContentLength];
13
System.IO.Stream oStream = this.UploadFile.PostedFile.InputStream;
14
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
15
16
int oWidth = oImage.Width; //原图宽度
17
int oHeight = oImage.Height; //原图高度
18
int tWidth = 100; //设置缩略图初始宽度
19
int tHeight = 100; //设置缩略图初始高度
20
21
//按比例计算出缩略图的宽度和高度
22
if(oWidth >= oHeight)
23
{
24
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
25
}
26
else
27
{
28
tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
29
}
30
31
//生成缩略原图
32
Bitmap tImage = new Bitmap(tWidth,tHeight);
33
Graphics g = Graphics.FromImage(tImage);
34
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
35
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
36
g.Clear(Color.Transparent); //清空画布并以透明背景色填充
37
g.DrawImage(oImage,new Rectangle(0,0,tWidth,tHeight),new Rectangle(0,0,oWidth,oHeight),GraphicsUnit.Pixel);
38
39
string oFullName = Server.MapPath(".") + "/" + "o" + DateTime.Now.ToShortDateString().Replace("-","") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存原图的物理路径
40
string tFullName = Server.MapPath(".") + "/" + "t" + DateTime.Now.ToShortDateString().Replace("-","") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存缩略图的物理路径
41
42
try
43
{
44
//以JPG格式保存图片
45
oImage.Save(oFullName,System.Drawing.Imaging.ImageFormat.Jpeg);
46
tImage.Save(tFullName,System.Drawing.Imaging.ImageFormat.Jpeg);
47
}
48
catch(Exception ex)
49
{
50
throw ex;
51
}
52
finally
53
{
54
//释放资源
55
oImage.Dispose();
56
g.Dispose();
57
tImage.Dispose();
58
}
59
}
60

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
