用ASP.NET上传图片并生成可带版权信息的缩略图

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

http://www.aspxboy.com/ShowArticle.Aspx?ID=218

Published At
Categories with Web编程
Tagged with
comments powered by Disqus