在ASP.NET中上传图片并生成带版权信息的缩略图
<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 }
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 }