1
/// <summary>
2
/// 上传图片
3
/// </summary>
4
/// <param name="sender"></param>
5
/// <param name="e"></param>
6
/// <returns>操作结果</returns>
7
private bool ImageUpload(int nWidth,int nHeight)
8
{
9
System.Web.HttpFileCollection files = Request.Files;
10
System.Web.HttpPostedFile pf = files[0];
11
string sOldPath = pf.FileName.ToString();
12
int i = sOldPath.LastIndexOf("\\");
13
string sOldName = sOldPath.Substring(i+1,sOldPath.Length-i-1);
14
//"L"代表大图 && "S"代表缩略图
15
string sTimeNo = System.DateTime.Now.ToString("yyMMddHHmmss");
16
string sNewNameL = "L"+sTimeNo+"_"+sOldName;
17
string sNewNameS = sNewNameL.Replace("L"+sTimeNo,"S"+sTimeNo);
18
string sNewPathL = Server.MapPath("../images/uploadfiles/")+sNewNameL;
19
string sNewPathS = Server.MapPath("../images/uploadfiles/")+sNewNameS;
20
if(System.IO.File.Exists(sNewPathL)||System.IO.File.Exists(sNewPathS))
21
{
22
Page.RegisterStartupScript("FailToUpload","<script>alert('文件名已存在!');</script>");
23
return false;
24
}
25
else
26
{
27
pf.SaveAs(sNewPathL);//保存原图
28
string strContentType = pf.ContentType.ToString();
29
if(strContentType.IndexOf("image/")<0)
30
{
31
Page.RegisterStartupScript("KeyEro","<script>alert('无效的图片格式!');</script>");
32
return false;
33
}
34
else
35
{
36
this.GetThumbNail(sOldPath,strContentType,sNewPathS,nWidth, nHeight);
37
this.Image1.ImageUrl = sNewPathS;
38
return true;
39
}
40
}
41
}
42
/// <summary>
43
/// 生成缩略图
44
/// </summary>
45
/// <param name="FileName">待上传文件的完全限定名</param>
46
/// <param name="strContentType">待上传文件的内容类型</param>
47
/// <param name="path">路径</param>
48
/// <param name="nWidth">宽</param>
49
/// <param name="nHeight">高</param>
50
private void GetThumbNail(string FileName,string strContentType,string path,int nWidth,int nHeight)
51
{
52
System.Drawing.Image oImage;
53
oImage = System.Drawing.Image.FromFile(FileName);
54
oImage = oImage.GetThumbnailImage(nWidth,nHeight,null,IntPtr.Zero);
55
// MemoryStream ms = new MemoryStream();
56
// Response.ContentType = strContentType;
57
// oImage.Save(ms,strContentType);
58
oImage.Save(path,this.GetContenType(strContentType));
59
// ms.WriteTo(Response.OutputStream);
60
}
61
/// <summary>
62
/// 获取保存文件的格式
63
/// </summary>
64
/// <param name="strContentType">待上传文件的内容类型</param>
65
/// <returns>文件格式</returns>
66
private System.Drawing.Imaging.ImageFormat GetContenType(string strContentType)
67
{
68
//只写少数几种格式
69
if(strContentType.ToString().ToLower()== "image/bmp")
70
return System.Drawing.Imaging.ImageFormat.Bmp;
71
else if(strContentType.ToString().ToLower()== "image/gif")
72
return System.Drawing.Imaging.ImageFormat.Gif;
73
else
74
return System.Drawing.Imaging.ImageFormat.Jpeg;
75
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)