-》使用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 }
时间: 2024-10-16 08:43:46