1
if (FileUpload1.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
2
{
3
FunctionUtility.JavaScriptHelper.Alert("上传图片格式不正确!");
4
return;
5
}
6![](/Images/OutliningIndicators/None.gif)
7
//生成原图
8
Byte[] oFileByte = new Byte[FileUpload1.PostedFile.ContentLength];
9
Stream oStream = FileUpload1.PostedFile.InputStream;
10
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
11![](/Images/OutliningIndicators/None.gif)
12
//原图宽度和高度
13
int oWidth = oImage.Width;
14
int oHeight = oImage.Height;
15![](/Images/OutliningIndicators/None.gif)
16
//设置缩略图的初始宽度和高度
17
int tWidth = 200;
18
int tHeight = 200;
19![](/Images/OutliningIndicators/None.gif)
20
//按比例计算出缩略图的宽度和高度
21
if (oWidth >= oHeight)
22
{
23
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
24
}
25
else
26
{
27
tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
28
}
29![](/Images/OutliningIndicators/None.gif)
30
//生成缩略图
31
Bitmap tImage = new Bitmap(tWidth, tHeight);
32
Graphics g = Graphics.FromImage(tImage);
33![](/Images/OutliningIndicators/None.gif)
34
//指定高质量插值法
35
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
36
//指定高质量低速度呈现
37
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
38
//清空画布并以透明背景色填充
39
g.Clear(Color.Transparent);
40![](/Images/OutliningIndicators/None.gif)
41
g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
42![](/Images/OutliningIndicators/None.gif)
43
//设置文件名称
44
string setFileName = FunctionUtility.FileHelper.GetDateTimeFileName()+".jpg";
45
//保存原图的物理路径
46
string oFullName = Server.MapPath("/DesignImages/Images/YT/" + setFileName);
47
//保存缩略图物理路径
48
string tFullName = Server.MapPath("/DesignImages/Images/SLT/" + setFileName);
49![](/Images/OutliningIndicators/None.gif)
50
//以JPG格式保存图片并释放占用的资源
51
try
52
{
53
oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
54
tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
55![](/Images/OutliningIndicators/InBlock.gif)
56
Image1.Visible = true;
57
Image2.Visible = true;
58
Image1.ImageUrl = "/DesignImages/Images/YT/" + setFileName;
59
Image2.ImageUrl = "/DesignImages/Images/SLT/" + setFileName;
60![](/Images/OutliningIndicators/InBlock.gif)
61
FunctionUtility.JavaScriptHelper.Alert("缩略图生成成功!");
62
}
63
catch (Exception oe)
64
{
65
throw oe;
66
}
67
finally
68
{
69
oImage.Dispose();
70
g.Dispose();
71
tImage.Dispose();
72
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/None.gif)
17
![](/Images/OutliningIndicators/None.gif)
18
![](/Images/OutliningIndicators/None.gif)
19
![](/Images/OutliningIndicators/None.gif)
20
![](/Images/OutliningIndicators/None.gif)
21
![](/Images/OutliningIndicators/None.gif)
22
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
25
![](/Images/OutliningIndicators/None.gif)
26
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
29
![](/Images/OutliningIndicators/None.gif)
30
![](/Images/OutliningIndicators/None.gif)
31
![](/Images/OutliningIndicators/None.gif)
32
![](/Images/OutliningIndicators/None.gif)
33
![](/Images/OutliningIndicators/None.gif)
34
![](/Images/OutliningIndicators/None.gif)
35
![](/Images/OutliningIndicators/None.gif)
36
![](/Images/OutliningIndicators/None.gif)
37
![](/Images/OutliningIndicators/None.gif)
38
![](/Images/OutliningIndicators/None.gif)
39
![](/Images/OutliningIndicators/None.gif)
40
![](/Images/OutliningIndicators/None.gif)
41
![](/Images/OutliningIndicators/None.gif)
42
![](/Images/OutliningIndicators/None.gif)
43
![](/Images/OutliningIndicators/None.gif)
44
![](/Images/OutliningIndicators/None.gif)
45
![](/Images/OutliningIndicators/None.gif)
46
![](/Images/OutliningIndicators/None.gif)
47
![](/Images/OutliningIndicators/None.gif)
48
![](/Images/OutliningIndicators/None.gif)
49
![](/Images/OutliningIndicators/None.gif)
50
![](/Images/OutliningIndicators/None.gif)
51
![](/Images/OutliningIndicators/None.gif)
52
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
63
![](/Images/OutliningIndicators/None.gif)
64
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
67
![](/Images/OutliningIndicators/None.gif)
68
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)