1
if (FileUpload1.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
2
{
3
FunctionUtility.JavaScriptHelper.Alert("上传图片格式不正确!");
4
return;
5
}
6![]()
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![]()
12
//原图宽度和高度
13
int oWidth = oImage.Width;
14
int oHeight = oImage.Height;
15![]()
16
//设置缩略图的初始宽度和高度
17
int tWidth = 200;
18
int tHeight = 200;
19![]()
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![]()
30
//生成缩略图
31
Bitmap tImage = new Bitmap(tWidth, tHeight);
32
Graphics g = Graphics.FromImage(tImage);
33![]()
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![]()
41
g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
42![]()
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![]()
50
//以JPG格式保存图片并释放占用的资源
51
try
52
{
53
oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
54
tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
55![]()
56
Image1.Visible = true;
57
Image2.Visible = true;
58
Image1.ImageUrl = "/DesignImages/Images/YT/" + setFileName;
59
Image2.ImageUrl = "/DesignImages/Images/SLT/" + setFileName;
60![]()
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
}
if (FileUpload1.PostedFile.ContentType.ToLower().IndexOf("image") < 0)2
{3
FunctionUtility.JavaScriptHelper.Alert("上传图片格式不正确!");4
return;5
}6

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

12
//原图宽度和高度13
int oWidth = oImage.Width;14
int oHeight = oImage.Height;15

16
//设置缩略图的初始宽度和高度17
int tWidth = 200;18
int tHeight = 200;19

20
//按比例计算出缩略图的宽度和高度21
if (oWidth >= oHeight)22
{23
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));24
}25
else26
{27
tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));28
}29

30
//生成缩略图31
Bitmap tImage = new Bitmap(tWidth, tHeight);32
Graphics g = Graphics.FromImage(tImage);33

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

41
g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);42

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

50
//以JPG格式保存图片并释放占用的资源51
try52
{53
oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg);54
tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);55

56
Image1.Visible = true;57
Image2.Visible = true;58
Image1.ImageUrl = "/DesignImages/Images/YT/" + setFileName;59
Image2.ImageUrl = "/DesignImages/Images/SLT/" + setFileName;60

61
FunctionUtility.JavaScriptHelper.Alert("缩略图生成成功!");62
}63
catch (Exception oe)64
{65
throw oe;66
}67
finally68
{69
oImage.Dispose();70
g.Dispose();71
tImage.Dispose();72
}

浙公网安备 33010602011771号