asp.net|上傳|上傳圖片|縮圖
前台:
<HTML>
<HEAD>
<title>WebForm3</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<INPUT id="loadFile" type="file" runat="server">
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button><BR>
<asp:Image id="Image1" runat="server"></asp:Image></form>
</body>
</HTML>
後台CS代碼:
1/**//// <summary>
2 /// 產生縮圖
3 /// </summary>
4 /// <param name="fileName">原檔案</param>
5 /// <param name="width">寬度</param>
6 /// <param name="height">高度</param>
7 private void CreateThumbnailImage( string fileName,string smallPath,int width,int height )
8 {
9 //著作權資訊
10 string strAdd = "www.wander.com";
11 //產生的縮圖的名字=年月日+檔案大小(防止檔案名稱相同)
12 string newFileName = smallPath;
13
14 System.Drawing.Image image,newImage;
15 //載入原映像
16 image = System.Drawing.Image.FromFile( Server.MapPath( fileName ) );
17 //回調
18 System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort( callBack );
19 //產生縮圖
20 newImage = image.GetThumbnailImage( width,height,callb,new System.IntPtr() );
21
22 AddInfo( newImage,strAdd,newFileName,16 );
23
24 image.Dispose();
25 newImage.Dispose();
26
27 Image1.ImageUrl = newFileName;
28 }
29
30 /**//// <summary>
31 /// 添加著作權資訊
32 /// </summary>
33 /// <param name="image"></param>
34 /// <param name="strAdd"></param>
35 /// <param name="newFileName"></param>
36 /// <param name="fontSize"></param>
37 private void AddInfo( System.Drawing.Image image,string strAdd,string newFileName,int fontSize )
38 {
39 Response.Clear();
40 Bitmap b = new Bitmap( image );
41 Graphics g = Graphics.FromImage( b );
42 g.DrawString( strAdd,new Font( "宋體",fontSize ),new SolidBrush( Color.Red ),image.Width/2-80 ,image.Height-30 );
43 b.Save( Server.MapPath( newFileName ),System.Drawing.Imaging.ImageFormat.Gif );
44 }
45
46 private bool callBack()
47 {
48 return true;
49 }
50
51 private void Button1_Click(object sender, System.EventArgs e)
52 {
53 if( loadFile.PostedFile != null )
54 {
55 //判斷是不是影像檔
56 if( loadFile.PostedFile.ContentType.ToLower().IndexOf( "image" ) < 0 )
57 {
58 //檔案類型錯誤
59 }
60 else
61 {
62 string ext = loadFile.PostedFile.FileName.Substring( loadFile.PostedFile.FileName.LastIndexOf( "." ) );
63 //產生新的原檔案名稱 年月日+檔案大小+副檔名
64 string path = "Uploads\\" + System.DateTime.Now.Date.ToShortDateString() + loadFile.PostedFile.ContentLength.ToString() + ext ;
65 //加上著作權資訊的檔案
66 string newPath = "Uploads\\" + System.DateTime.Now.Date.ToShortDateString() + loadFile.PostedFile.ContentLength.ToString() + "new" + ext ;
67 //縮圖檔案名稱
68 string newFileName = "uploads\\" + System.DateTime.Now.Date.ToShortDateString() + loadFile.PostedFile.ContentLength.ToString() + "small.gif";
69 //上傳原圖
70 loadFile.PostedFile.SaveAs( Server.MapPath( path ) );
71 //產生縮圖
72 CreateThumbnailImage( path,newFileName,200,300 );
73
74 //給原圖加上著作權資訊
75 System.Drawing.Image image = System.Drawing.Image.FromFile( Server.MapPath( path ) );
76 AddInfo( image,"www.wander.com",newPath,20 );
77 image.Dispose();
78 }
79 }
80 }
其實把著作權資訊改成浮水印的也很簡單,只需要修改AddInfo方法即可