这是页面代码

 1 <% @ Page language = " c# "  Codebehind = " pic.aspx.cs "  AutoEventWireup = " false "  Inherits = " test.pic "   %>
 2 < HTML >
 3      < HEAD >
 4          < title > pic </ title >
 5          < meta name = " GENERATOR "  Content = " Microsoft Visual Studio .NET 7.1 " >
 6          < meta name = " CODE_LANGUAGE "  Content = " C# " >
 7          < meta name = " vs_defaultClientScript "  content = " JavaScript " >
 8          < meta name = " vs_targetSchema "  content = " http://schemas.microsoft.com/intellisense/ie5 " >
 9      </ HEAD >
10      < body >
11          < form id = " Form1 "  method = " post "  runat = " server " >
12              < INPUT id = " File1 "  type = " file "  name = " File1 "  runat = " server " >
13              < asp:button id = " Button1 "  runat = " server "  Text = " Button " ></ asp:button >< BR >
14              < table border = " 0 "  width = " 100% " >
15                  < tr >
16                      < td >
17                          < asp:Image id = " Image1 "  runat = " server "  Width = " 200px " ></ asp:Image >
18                      </ td >
19                      < td >
20                          < asp:Image id = " Image2 "  runat = " server "  Width = " 200px " ></ asp:Image >
21                      </ td >
22                      < td >
23                          < asp:Image id = " Image3 "  runat = " server "  Width = " 100px " ></ asp:Image >
24                      </ td >
25                  </ tr >
26              </ table >
27          </ form >
28      </ body >
29 </ HTML >
30

以下是后台代码
  1 using  System;
  2 using  System.Collections;
  3 using  System.ComponentModel;
  4 using  System.Data;
  5 using  System.Drawing;
  6 using  System.Web;
  7 using  System.Web.SessionState;
  8 using  System.Web.UI;
  9 using  System.Web.UI.WebControls;
 10 using  System.Web.UI.HtmlControls;
 11
 12 namespace  test
 13 {
 14    /// <summary>
 15    /// pic 的摘要说明。
 16    /// 功能:图片上传/保存至硬盘/生成缩略图/在图片上写字
 17    /// </summary>

 18    public class pic : System.Web.UI.Page
 19    {
 20        protected System.Web.UI.WebControls.Button Button1;
 21        protected System.Web.UI.WebControls.Image Image1;
 22        protected System.Web.UI.WebControls.Image Image2;
 23        protected System.Web.UI.WebControls.Image Image3;
 24        protected System.Web.UI.HtmlControls.HtmlInputFile File1;
 25    
 26        private void Page_Load(object sender, System.EventArgs e)
 27        {
 28            if(!this.IsPostBack)
 29            {
 30                //接收参数
 31                string filename = this.Request.QueryString["filename"];
 32                //如果参数不为空
 33                //此处这样写,是一个比较安全全的写法,因为接收到的参数值也许是NULL
 34                if(filename + "a" != "a")
 35                {
 36                    //求取后缀名
 37                    string suffix = filename.Substring(filename.LastIndexOf("."));
 38                    //显示图片
 39                    //分别为原图片/写字的图片(多一个w)/缩略图(多一个x)
 40                    this.Image1.ImageUrl = this.Server.MapPath("uploadfile/" + filename);
 41                    this.Image2.ImageUrl = this.Server.MapPath("uploadfile/" + filename.Replace(suffix,"w" + suffix));
 42                    this.Image3.ImageUrl = this.Server.MapPath("uploadfile/" + filename.Replace(suffix,"x" + suffix));
 43
 44                    //显示图像控件
 45                    this.Image1.Visible = true;
 46                    this.Image2.Visible = true;
 47                    this.Image3.Visible = true;
 48                }

 49                else
 50                {
 51                    //如果没有接收到参数,则隐藏页面中的图像控件
 52                    this.Image1.Visible = false;
 53                    this.Image2.Visible = false;
 54                    this.Image3.Visible = false;
 55                }

 56            }

 57        }

 58
 59
 60        /// <summary>
 61        /// 上传图片
 62        /// </summary>
 63        /// <param name="sender"></param>
 64        /// <param name="e"></param>

 65        private void Button1_Click(object sender, System.EventArgs e)
 66        {
 67            //接收上传图片 
 68            System.Web.HttpPostedFile myPost = this.Request.Files[0];
 69            if(myPost.ContentLength !=0)
 70            {
 71                try
 72                {
 73                    //定义上传路径(在当前目录下的uploadfile文件病例
 74                    string uploadpath = this.Server.MapPath("uploadfile");
 75                    //取得文件名
 76                    string tmpfilename = myPost.FileName;
 77                    //文件名
 78                    string filename = tmpfilename.Substring(tmpfilename.LastIndexOf("//"+ 1);
 79
 80                    //原文件的保存路径
 81                    string fileSavePath = uploadpath + "//" + filename;
 82
 83                    //保存原图片            
 84                    myPost.SaveAs(fileSavePath);
 85                
 86                    //调用生成缩略图程序,生成缩略图及生成写字的图片
 87                    this.toImage(myPost.InputStream,uploadpath,filename);
 88
 89                    //完成之后,重定向本页面,并带参数显示当前上传的图片 
 90                    this.Response.Redirect("pic.aspx?filename=" + filename);
 91                    this.Response.End();
 92                }

 93                catch(Exception ex)
 94                {
 95                    throw ex;
 96                }

 97                finally
 98                {
 99                    
100                }

101            }

102        }

103
104
105        /// <summary>
106        /// 生成缩略图程序
107        /// </summary>
108        /// <param name="myStream">取到的流文件对象</param>
109        /// <param name="uploadPath">要保存的路径</param>
110        /// <param name="picName">上传的图片原文件名</param>

111        private void toImage(System.IO.Stream myStream,string uploadPath,string picName)
112        {
113            //取得后缀名
114            string suffix = picName.Substring(picName.LastIndexOf("."));
115
116            //缩略图的保存路径
117            string fileXltPath = uploadPath + "//" + picName.Replace(suffix,"x" + suffix);
118            //写字图的保存路径
119            string fileXztPath = uploadPath + "//" + picName.Replace(suffix,"w" + suffix);
120
121
122            //创建一个图像对象取得上传图片对象
123            System.Drawing.Image myImage = System.Drawing.Image.FromStream(myStream,false);
124            //对绘制前的图片产生一个缩略图(原图片一半大小)
125            System.Drawing.Image thumbImage = myImage.GetThumbnailImage(myImage.Size.Width/2,myImage.Size.Height/2,null,System.IntPtr.Zero);
126            //保存缩略图
127            thumbImage.Save(fileXltPath,this.getImageFormat(suffix));
128            //关闭缩略图对象
129            thumbImage.Dispose();
130
131            //创建绘制对象
132            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myImage);
133            g.DrawImage(myImage,0,0,myImage.Size.Width,myImage.Size.Height);
134            //选择字体及字体大小
135            System.Drawing.Font f = new Font("隶书",140);
136            //定义字体颜色
137            System.Drawing.Brush b = new SolidBrush(System.Drawing.Color.Red);
138            //开始绘制,根据上述两种设定,添加绘制的上左位置
139            g.DrawString("好好学习,天天向上",f,b,10,10);
140            //关闭绘制对象
141            g.Dispose();
142
143            //保存绘制后上传图片
144            myImage.Save(fileXztPath,myImage.RawFormat);
145            //关闭图片对象
146            myImage.Dispose();
147        }

148
149        /// <summary>
150        /// 根据图片的后缀名,返回要保存的图片格式
151        /// </summary>
152        /// <param name="suffix">带.号的后缀名</param>
153        /// <returns>返回System.Drawing.Imaging.ImageForma对象</returns>

154        private System.Drawing.Imaging.ImageFormat getImageFormat(string suffix)
155        {
156          System.Drawing.Imaging.ImageFormat myFormat;
157          switch(suffix.ToLower())
158          {
159            case ".bmp":
160              myFormat = System.Drawing.Imaging.ImageFormat.Bmp;
161              break;
162            case ".emf":
163              myFormat = System.Drawing.Imaging.ImageFormat.Emf;
164              break;
165            case ".exif":
166              myFormat = System.Drawing.Imaging.ImageFormat.Exif;
167              break;
168            case ".gif":
169              myFormat = System.Drawing.Imaging.ImageFormat.Gif;
170              break;
171            case ".icon":
172              myFormat = System.Drawing.Imaging.ImageFormat.Icon;
173              break;
174            case ".jpeg":
175            case ".jpg":
176              myFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
177              break;
178            case ".png":
179              myFormat = System.Drawing.Imaging.ImageFormat.Png;
180              break;
181            case ".tiff":
182              myFormat = System.Drawing.Imaging.ImageFormat.Tiff;
183              break;
184            case ".wmf":
185              myFormat = System.Drawing.Imaging.ImageFormat.Wmf;
186              break;
187            default:
188              myFormat = System.Drawing.Imaging.ImageFormat.MemoryBmp;
189              break;
190          }

191          return(myFormat);
192        }

193
194
195        #region Web 窗体设计器生成的代码
196        override protected void OnInit(EventArgs e)
197        {
198            //
199            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
200            //
201            InitializeComponent();
202            base.OnInit(e);
203        }

204        
205        /// <summary>
206        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
207        /// 此方法的内容。
208        /// </summary>

209        private void InitializeComponent()
210        {    
211            this.Button1.Click += new System.EventHandler(this.Button1_Click);
212            this.Load += new System.EventHandler(this.Page_Load);
213
214        }

215        #endregion

216    }

217}

218