国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

在ASP.NET中上傳圖片并生成帶版權信息的縮略圖

2024-07-10 13:06:03
字體:
來源:轉載
供稿:網友

前臺:
<html>
    <head>
        <title>webform3</title>
            </head>
    <body>
        <form id="form1" method="post" runat="server">
            &nbsp;<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方法即可

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吴堡县| 抚远县| 福泉市| 蓝田县| 大渡口区| 靖远县| 修武县| 慈溪市| 昌吉市| 松江区| 长乐市| 广西| 绥阳县| 康保县| 沁水县| 利津县| 辽宁省| 连州市| 永川市| 东丰县| 秀山| 德阳市| 利辛县| 喀喇沁旗| 天水市| 泰顺县| 岚皋县| 天峻县| 淅川县| 西乡县| 射洪县| 左贡县| 和林格尔县| 延吉市| 浮山县| 石屏县| 禹州市| 焉耆| 惠来县| 论坛| 双鸭山市|