前台:
<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方法即可
新闻热点
疑难解答
图片精选