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
}
/// <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
else26
{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
else35
{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
else74
return System.Drawing.Imaging.ImageFormat.Jpeg;75
}


浙公网安备 33010602011771号