View Code
 1 <%@ WebHandler Language="C#" Class="_03_图片水印" %>
 2 
 3 using System;
 4 using System.Web;
 5 //第一步引入绘图命名空间
 6 using System.Drawing;
 7 public class _03_图片水印 : IHttpHandler {
 8     
 9     public void ProcessRequest (HttpContext context) {
10         //更改输出方式
11         context.Response.ContentType = "image/jpeg";
12         //先得到大图的路径
13         string path = context.Request.MapPath("7.jpg");
14         //得到小图的路径
15         string logoPath = context.Request.MapPath("logo.png");
16         //打开大图在他上面操作
17         using (Image img = Image.FromFile(path))
18         {
19             //打开小图
20             using (Image logoImg = Image.FromFile(logoPath))
21             {
22                 //操作界面
23                 Graphics g = Graphics.FromImage(img);
24                 //改变logo的位置
25                 int x = img.Width - logoImg.Width;
26                 int y = img.Height - logoImg.Height;
27                 //将小图绘制到大图上,1、小图2、在大图的位置上x/y 3、小图的大小x/y
28                 g.DrawImage(logoImg, x, y, logoImg.Width, logoImg.Height);
29                 //将图片输出,也可以输出到文件夹下,但必须要给图片起名字
30                 img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
31             }
32         }
33     }
34  
35     public bool IsReusable {
36         get {
37             return false;
38         }
39     }
40 
41 }
posted on 2012-08-15 23:24  Fan帥帥  阅读(307)  评论(0编辑  收藏  举报