首页 > 技术文章 > 007-一般处理程序动态处理图片

ninghongkun 2017-01-12 02:45 原文

-》使用GDI+完成图片的处理,需要引入程序集System.Drawing
-》GDI+的基本处理模型
《1》创建画布Bitmap
《2》创建画图工具对象Graphics
《3》调用Draw***、Fill***系列方法完成绘制
《4》保存,可以保存到物理文件中,也可以保存到输出流中
在ashx中使用,需要指定ContentType="image/jpeg";
使用画布对象的Save()方法输出,可以输出到一个物理文件中,也可以输出到流中
使用:<img src="pic.ashx"/>
-》示例1:生成水印图
思路:在原有图片上绘制一段文字或小图片
-》示例2:生成验证码
思路:创建画布,随机生成字符并绘制
附加功能:“看不清,换一张”
-》示例3:缩略图
思路:将原图按照一个缩小比例,绘制到一个新图上,并完成物理保存

加水印

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 5     <title></title>
 6 </head>
 7 <body>
 8     <form method="POST" action="AddWater.ashx" enctype="multipart/form-data">
 9         <input type="file" name="f1"/>
10         <br/>
11         <input type="submit" value="加水印"/>
12     </form>
13 </body>
14 </html>
 1         public void ProcessRequest(HttpContext context)
 2         {
 3             context.Response.ContentType = "text/html";
 4 
 5             //接收文件
 6             HttpPostedFile file1 = context.Request.Files["f1"];
 7 
 8             //根据上传流创建画布
 9             Image bitmap1 = Bitmap.FromStream(file1.InputStream);
10 
11             //获取绘制工具
12             Graphics graphics = Graphics.FromImage(bitmap1);
13 
14             //根据logo图片创建image对象
15             Image logoImage = Bitmap.FromFile(context.Request.MapPath("/Uploads/logo.gif"));
16 
17             //绘制logo图片
18             graphics.DrawImage(logoImage,
19                 bitmap1.Width - logoImage.Width,
20                 bitmap1.Height - logoImage.Height,
21                 logoImage.Width,
22                 logoImage.Height);
23 
24             //保存
25             string path = GetDirectory();
26             string path2 = context.Request.MapPath(path);
27             if (!Directory.Exists(path2))
28             {
29                 Directory.CreateDirectory(path2);
30             }
31             bitmap1.Save(path2+file1.FileName,ImageFormat.Jpeg);
32 
33             context.Response.Write("<img src='"+path+file1.FileName+"'/>");
34         }
35 
36         private string GetDirectory()
37         {
38             DateTime now = DateTime.Now;
39             StringBuilder sb = new StringBuilder("/Uploads/");
40             sb.Append(now.Year + "/");
41             sb.Append(now.Month + "/");
42             sb.Append(now.Day + "/");
43 
44             return sb.ToString();
45         }

 验证码

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 5     <title></title>
 6     <script>
 7         function ChangeCode() {
 8             var vCode = document.getElementById('vCode');
 9             vCode.src = 'ValidateCode.ashx?r='+Math.random();
10         }
11     </script>
12 </head>
13     <body>
14         <input type="text"/>
15         <img id="vCode" src="ValidateCode.ashx" onclick="ChangeCode();"/>
16         <a href="javascript:ChangeCode();">看不清,换一张</a>
17     </body>
18 </html>
 1         public void ProcessRequest(HttpContext context)
 2         {
 3             context.Response.ContentType = "image/jpeg";
 4 
 5             //创建画布
 6             Bitmap bitmap=new Bitmap(70,30);
 7 
 8             //bitmap.SetPixel(x,y,color);
 9 
10             //获取绘制工具
11             Graphics graphics = Graphics.FromImage(bitmap);
12 
13             //刷背景
14             graphics.Clear(Color.White);
15 
16             //绘制边框
17             graphics.DrawRectangle(new Pen(Color.Black), 0,0,69,29);
18 
19             //获取字符串
20             string temp = "abcdef0123456789";
21 
22             int len = 4,len2=temp.Length;
23             StringBuilder sb=new StringBuilder("");
24             Random random=new Random();
25             for (int i = 0; i < len; i++)
26             {
27                 int index= random.Next(0, len2);
28                 sb.Append(temp[index].ToString());
29             }
30             string temp1 = sb.ToString();
31 
32             //绘制字符串
33             graphics.DrawString(temp1,
34                 new Font("宋体",20),
35                 new SolidBrush(Color.Red), 
36                 0,0
37                 );
38 
39             //保存
40             bitmap.Save(context.Response.OutputStream,ImageFormat.Jpeg);
41 
42         }

缩略图

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 5     <title></title>
 6 </head>
 7 <body>
 8     <form method="POST" action="ScaleTest.ashx" enctype="multipart/form-data">
 9         <input type="file" name="file1"/>
10         <br/>
11         <input type="submit" value="上传"/>
12     </form>
13 </body>
14 </html>
 1         public void ProcessRequest(HttpContext context)
 2         {
 3             HttpPostedFile file1 = context.Request.Files["file1"];
 4 
 5             //获取文件路径
 6             string path = PathHelper.GetDirectory();
 7             string path2 = context.Request.MapPath(path);
 8             if (!Directory.Exists(path2))
 9             {
10                 Directory.CreateDirectory(path2);
11             }
12 
13             //将原始图片进行保存
14             file1.SaveAs(path2 + file1.FileName);
15 
16             //计算目标的宽高
17             //目标:宽或高均不超过100
18             Image bitmapBig = Bitmap.FromFile(path2 + file1.FileName);
19             int targetWidth = 100;
20             int scale = bitmapBig.Width / targetWidth;
21             int targetHeight = bitmapBig.Height / scale;
22             if (targetHeight > 100)
23             {
24                 targetHeight = 100;
25                 scale = bitmapBig.Height / targetHeight;
26                 targetWidth = bitmapBig.Width / scale;
27             }
28 
29             //创建画布
30             Bitmap bitmap = new Bitmap(targetWidth, targetHeight);
31 
32             //获取绘制工具
33             Graphics graphics = Graphics.FromImage(bitmap);
34 
35             //绘制图片
36             graphics.DrawImage(bitmapBig,
37                 new Rectangle(0, 0, targetWidth, targetHeight),//目标缩放图的尺寸
38                 new Rectangle(0, 0, bitmapBig.Width, bitmapBig.Height),//原始大图的尺寸
39                 GraphicsUnit.Pixel);
40 
41             //保存sg1.jpg->sg1_s.jpg
42             string fileName = file1.FileName;
43             string fileName2 = fileName.Substring(0, fileName.LastIndexOf('.')) + "_s" + Path.GetExtension(fileName);
44             bitmap.Save(path2 + fileName2, ImageFormat.Jpeg);
45 
46             context.Response.ContentType = "text/html";
47             context.Response.Write("<img src='" + path + fileName2 + "'/>");
48         }
49         public static string GetDirectory()
50         {
51             DateTime now = DateTime.Now;
52             StringBuilder sb = new StringBuilder("/Uploads/");
53             sb.Append(now.Year + "/");
54             sb.Append(now.Month + "/");
55             sb.Append(now.Day + "/");
56 
57             return sb.ToString();
58         }

 

推荐阅读