上传图片并生成缩略图的ASP.NET2.0代码!

 1  <% @ Page Language = " C# "  ResponseEncoding = " gb2312 "   %>
 2  <% @ import Namespace = " System "   %>
 3  <% @ import Namespace = " System.IO "   %>
 4  <% @ import Namespace = " System.Drawing "   %>
 5  <% @ import Namespace = " System.Drawing.Imaging "   %>
 6  < script runat = " server " >
 7 
 8  void  Page_Load(Object sender, EventArgs e)
 9  {
10  if ( ! Page.IsPostBack)
11  {
12  ImgPreview.Visible = false ;
13  }
14  }
15  void  GetThumbnailImage( int  width, int  height, string  strInfo, int  left, int  right)
16  {
17  string  file = " Uploads/ " + uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf( ' \\ ' ) + 1 );
18  string  newfile = " Uploads/ " + uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf( ' \\ ' ) + 1 ) + " .jpg " ;
19  string  strAdd = strInfo;
20  System.Drawing.Image oldimage  =  System.Drawing.Image.FromFile(Server.MapPath(file));
21  System.Drawing.Image thumbnailImage  =
22  oldimage.GetThumbnailImage(width, height, new  System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
23  Response.Clear();
24  Bitmap output = new  Bitmap(thumbnailImage);
25  Graphics g = Graphics.FromImage(output);
26  g.DrawString(strAdd, new  Font( " Courier New " 14 ), new  SolidBrush(Color.Red),left,right);
27  output.Save(Server.MapPath(newfile),System.Drawing.Imaging.ImageFormat.Jpeg);
28  Response.ContentType  =   " image/gif " ;
29  ImgPreview.Visible = true ;
30  ImgPreview.ImageUrl = newfile;
31  }
32  bool  ThumbnailCallback()
33  {
34  return   true ;
35  }
36 
37  void  Button_Click( object  sender, EventArgs e)
38  {
39  int  width,height,left,right;
40  string  strAddInfo = txtAddInfo.Text;
41  width = Int32.Parse(txtWidth.Text);
42  height = Int32.Parse(txtHeight.Text);
43  left = Int32.Parse(txtLeft.Text);
44  right = Int32.Parse(txtRight.Text);
45  if ( ! (uploadFile.PostedFile.ContentLength > 0 ))
46  {
47  lblErrInfo.Text = " 没有选择文件 " ;
48  }
49  else
50  {
51 
52  string  path  =  Server.MapPath( " ./Uploads/ " + uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf( ' \\ ' ) + 1 ));
53  if (File.Exists(path))
54  {
55  lblErrInfo.Text = " 已经有同名文件 " ;
56  }
57  else
58  {
59  uploadFile.PostedFile.SaveAs(path); 
60  GetThumbnailImage(width,height,strAddInfo,left,right);
61 
62 
63  }
64  </ script >
65  < html >
66  < head >
67  </ head >
68  < body >
69  < form method = " post "  enctype = " multipart/form-data "  runat = " server " >
70  < p >
71  < input id = " uploadFile "  type = " file "  runat = " server "   />
72  < asp:Label id = " lblErrInfo "  runat = " server "  forecolor = " Red " ></ asp:Label >
73  </ p >
74  < p >
75  width: < asp:TextBox id = " txtWidth "  runat = " server "  Width = " 40px " > 100 </ asp:TextBox >
76   height: < asp:TextBox id = " txtHeight "  runat = " server "  Width = " 40px " > 150 </ asp:TextBox >
77   
78  </ p >
79  < p >
80  添加信息: < asp:TextBox id = " txtAddInfo "  runat = " server " > AspxBoy.Com </ asp:TextBox >
81  </ p >
82  < p >
83  信息位置:left: < asp:TextBox id = " txtLeft "  runat = " server "  Width = " 40px " > 10 </ asp:TextBox >
84   right: < asp:TextBox id = " txtRight "  runat = " server "  Width = " 40px " > 135 </ asp:TextBox >
85  </ p >
86  < p >
87   
88  < input id = " button "  type = " button "  value = " 上传生成缩略图 "  onServerClick = " Button_Click "  runat = " server "   />
89  </ p >
90  < p >< asp:Image id = " ImgPreview "  runat = " server " ></ asp:Image >
91  </ p >
92  <!--  Insert content here  -->
93  </ form >
94  </ body >
95  </ html >

你可能感兴趣的:(asp.net)