前臺:
<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方法即可 
新聞熱點
疑難解答
圖片精選