1
以下是引用片段:
2
private
void
btnUploadPicture_Click(
object
sender, System.EventArgs e)
3![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
4
//检查上传文件的格式是否有效
5
if(this.UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
6![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
Response.Write("上传图片格式无效!");
8
return;
9
}
10![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
int oWidth = oImage.Width; //原图宽度
17
int oHeight = oImage.Height; //原图高度
18
int tWidth = 100; //设置缩略图初始宽度
19
int tHeight = 100; //设置缩略图初始高度
20![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
//按比例计算出缩略图的宽度和高度
22
if(oWidth >= oHeight)
23![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
25
}
26
else
27![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
29
}
30![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
try
43![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
throw ex;
51
}
52
finally
53![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
//释放资源
55
oImage.Dispose();
56
g.Dispose();
57
tImage.Dispose();
58
}
59
}
60
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
4
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
7
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
10
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
24
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
28
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
44
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
48
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
50
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
52
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
54
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
59
![ExpandedBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
60
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)