您的位置毕业设计论文网 , 信息管理系统毕业设计,智轩软件源码网>网站制作>asp.net>使用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图>

使用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图

发布时间:2009-1-14浏览:

核心提示:使用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图

CODE: <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="upfile.aspx.cs"Inherits="upfile_upfile"%> 2 3<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5<htmlxmlns="http://www.w3.org/1999/xhtml"> 6<headrunat="server"> 7 <title>无标题页</title> 8</head> 9<body> 10 <formid="form1"runat="server"> 11 <div> 12  <asp:FileUploadID="FileUpload1"runat="server"/> 13  <asp:ButtonID="Button1"runat="server" Text="上传"/><br/> 14  <aspabelID="Label1"runat="server"></aspabel></div> 15 </form> 16</body> 17</html>  1usingSystem;  2usingSystem.Data;  3usingSystem.Configuration;  4usingSystem.Collections;  5usingSystem.Web;  6usingSystem.Web.Security;  7usingSystem.Web.UI;  8usingSystem.Web.UI.WebControls;  9usingSystem.Web.UI.WebControls.WebParts; 10usingSystem.Web.UI.HtmlControls; 11usingSystem.IO; 12 13publicpartialclassupfile_upfile:System.Web.UI.Page 14{ 15  protectedvoidPage_Load(objectsender,EventArgse) 16  {} 17 18  protectedvoidButton1_Click(objectsender,EventArgse) 19  { 20    if(FileUpload1.HasFile) 21    { 22      stringfileContentType=FileUpload1.PostedFile.ContentType; 23      if(fileContentType=="image/bmp"||fileContentType=="image/gif"||fileContentType=="image/pjpeg") 24      { 25        stringname=FileUpload1.PostedFile.FileName;//客户端文件路径 26 27        FileInfofile=newFileInfo(name); 28        stringfileName=file.Name;//文件名称 29        stringfileName_s="s_"+file.Name;//缩略图文件名称 30        stringfileName_sy="sy_"+file.Name;//水印图文件名称(文字) 31        stringfileName_syp="syp_"+file.Name;//水印图文件名称(图片) 32        stringwebFilePath=Server.MapPath("file/"+fileName);//服务器端文件路径 33        stringwebFilePath_s=Server.MapPath("file/"+fileName_s);  //服务器端缩略图路径 34        stringwebFilePath_sy=Server.MapPath("file/"+fileName_sy); //服务器端带水印图路径(文字) 35        stringwebFilePath_syp=Server.MapPath("file/"+fileName_syp); //服务器端带水印图路径(图片) 36        stringwebFilePath_sypf=Server.MapPath("file/shuiyin.jpg"); //服务器端水印图路径(图片) 37 38        if(!File.Exists(webFilePath)) 39        { 40          try 41          { 42            FileUpload1.SaveAs(webFilePath);//使用SaveAs方法保存文件 43            AddShuiYinWord(webFilePath,webFilePath_sy); 44            AddShuiYinPic(webFilePath,webFilePath_syp,webFilePath_sypf); 45            MakeThumbnail(webFilePath,webFilePath_s,130,130,"Cut");//生成缩略图方法 46            Label1.Text="提示:文件“"+fileName+"”成功上传,并生成“"+fileName_s+"”缩略图,文件类型为:"+ 47          FileUpload1.PostedFile.ContentType+",文件大小为:"+FileUpload1.PostedFile.ContentLength+"B"; 48          } 49          catch(Exceptionex) 50          { 51            Label1.Text="提示:文件上传失败,失败原因:"+ex.Message; 52          } 53        } 54        else 55        { 56          Label1.Text="提示:文件已经存在,请重命名后上传"; 57        } 58      } 59      else 60      { 61        Label1.Text="提示:文件类型不符"; 62      } 63    } 64  } 65  /**//**/ 66  /**////<summary> 67  ///生成缩略图 68  ///</summary> 69  ///<paramname="originalImagePath">源图路径(物理路径)</param> 70  ///<paramname="thumbnailPath">缩略图路径(物理路径)</param> 71  ///<paramname="width">缩略图宽度</param> 72  ///<paramname="height">缩略图高度</param> 73  ///<paramname="mode">生成缩略图的方式</param>  74  publicstaticvoidMakeThumbnail(stringoriginalImagePath,stringthumbnailPath,intwidth,intheight,stringmode) 75  { 76    System.Drawing.ImageoriginalImage=System.Drawing.Image.FromFile(originalImagePath); 77 78    inttowidth=width; 79    inttoheight=height; 80 81    intx=0; 82    inty=0; 83    intow=originalImage.Width; 84    intoh=originalImage.Height; 85 86    switch(mode) 87    { 88      case"HW"://指定高宽缩放(可能变形) 89        break; 90      case"W"://指定宽,高按比例 91        toheight=originalImage.Height*width/originalImage.Width; 92        break; 93      case"H"://指定高,宽按比例 94        towidth=originalImage.Width*height/originalImage.Height; 95        break; 96      case"Cut"://指定高宽裁减(不变形) 97        if((double)originalImage.Width/(double)originalImage.Height>(double)towidth/(double)toheight) 98        { 99          oh=originalImage.Height; 100          ow=originalImage.Height*towidth/toheight; 101          y=0; 102          x=(originalImage.Width-ow)/2; 103        } 104        else 105        { 106          ow=originalImage.Width; 107          oh=originalImage.Width*height/towidth; 108          x=0; 109          y=(originalImage.Height-oh)/2; 110        } 111        break; 112      default: 113        break; 114    } 115 116    //新建一个bmp图片 117    System.Drawing.Imagebitmap=newSystem.Drawing.Bitmap(towidth,toheight); 118 119    //新建一个画板 120    System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(bitmap); 121 122    //设置高质量插值法 123    g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High; 124 125    //设置高质量,低速度呈现平滑程度 126    g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality; 127 128    //清空画布并以透明背景色填充 129    g.Clear(System.Drawing.Color.Transparent); 130 131    //在指定位置并且按指定大小绘制原图片的指定部分 132    g.DrawImage(originalImage,newSystem.Drawing.Rectangle(0,0,towidth,toheight), 133      newSystem.Drawing.Rectangle(x,y,ow,oh), 134      System.Drawing.GraphicsUnit.Pixel); 135 136    try 137    { 138      //以jpg格式保存缩略图 139      bitmap.Save(thumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg); 140    } 141    catch(System.Exceptione) 142    { 143      throwe; 144    } 145    finally 146    { 147      originalImage.Dispose(); 148      bitmap.Dispose(); 149      g.Dispose(); 150    } 151  } 152 153  /**//**/ 154  /**////<summary> 155  ///在图片上增加文字水印 156  ///</summary> 157  ///<paramname="Path">原服务器图片路径</param> 158  ///<paramname="Path_sy">生成的带文字水印的图片路径</param> 159  protectedvoidAddShuiYinWord(stringPath,stringPath_sy) 160  { 161    stringaddText="测试水印"; 162    System.Drawing.Imageimage=System.Drawing.Image.FromFile(Path); 163    System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(image); 164    g.DrawImage(image,0,0,image.Width,image.Height); 165    System.Drawing.Fontf=newSystem.Drawing.Font("Verdana",16); 166    System.Drawing.Brushb=newSystem.Drawing.SolidBrush(System.Drawing.Color.Blue); 167 168    g.DrawString(addText,f,b,15,15); 169    g.Dispose(); 170 171    image.Save(Path_sy); 172    image.Dispose(); 173  } 174 175  /**//**/ 176  /**////<summary> 177  ///在图片上生成图片水印 178  ///</summary> 179  ///<paramname="Path">原服务器图片路径</param> 180  ///<paramname="Path_syp">生成的带图片水印的图片路径</param> 181  ///<paramname="Path_sypf">水印图片路径</param> 182  protectedvoidAddShuiYinPic(stringPath,stringPath_syp,stringPath_sypf) 183  { 184    System.Drawing.Imageimage=System.Drawing.Image.FromFile(Path); 185    System.Drawing.ImagecopyImage=System.Drawing.Image.FromFile(Path_sypf); 186    System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(image); 187    g.DrawImage(copyImage,newSystem.Drawing.Rectangle(image.Width-copyImage.Width,image.Height-copyImage.Height,copyImage.Width,copyImage.Height),0,0,copyImage.Width,copyImage.Height,System.Drawing.GraphicsUnit.Pixel); 188    g.Dispose(); 189 190    image.Save(Path_syp); 191    image.Dispose(); 192  } 193}
前一篇:定做程序设计
后一篇:加密码的问题同时可以用于解密
分享到: