在ASP.NET中上傳圖片並產生帶著作權資訊的縮圖

來源:互聯網
上載者:User
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方法即可



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.