using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
/// <summary>
///TestHandler 的摘要说明
/// </summary>
public class TestHandler:IHttpHandler
{
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
//字符串
//context.Response.ContentType = "text/plain"; //响应类型是字符串
//context.Response.Write("这是一个字符串");
//html页面
//context.Response.ContentType = "text/html";//响应类型是html
//context.Response.Write("<html>");
//context.Response.Write("<body");
//context.Response.Write("我是html页面");
//context.Response.Write("</body");
//context.Response.Write("</html>");
//图片
//context.Response.ContentType = "image/jpeg"; //响应类型是jpg格式的图片
//Bitmap bm = new Bitmap(100, 100); //new一个宽高为100的图片
//Graphics g = Graphics.FromImage(bm); //拿到了一个跟这个图片关联的绘图对象
//g.FillEllipse(Brushes.White, 0, 0, 100, 100); //调用绘图对象里面的画园的方法
//bm.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg); //将这张图片保存在输出流
//context.Response.Flush();//把东西清到客户端
//g.Dispose();
//bm.Dispose();
//水印
//MapPath("") 就是获取当前文件的路径
string isbn = context.Request.QueryString["ISBN"]; //拿到从页面传过来的ISBN
if (isbn == null)
{
string imagePath = context.Server.MapPath("BookCovers") + "/9293450.jpg";
Image cover = Image.FromFile(imagePath); //通过FromFile找到某路径下这张图片
string waterPath = context.Server.MapPath("images") + "/WaterMark.jpg";
Image water = Image.FromFile(waterPath);//通过FromFile找到某路径下的这张图片
Graphics g = Graphics.FromImage(cover); //拿到了一个跟这个图片关联的绘图对象
g.DrawImage(water, cover.Width - water.Width, cover.Height - water.Height, water.Width, water.Height); //调用画图的方法
cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); //将这张图片保存在输出流
context.Response.Flush(); //把东西清到客户端
g.Dispose(); //Dispose()[释放,销毁,关闭]类似close()
water.Dispose();
cover.Dispose();
}
else {
string imagePath = context.Server.MapPath("BookCovers") + isbn+".jpg";
Image cover = Image.FromFile(imagePath); //通过FromFile找到某路径下这张图片
string waterPath = context.Server.MapPath("images") + "/WaterMark.jpg";
Image water = Image.FromFile(waterPath);//通过FromFile找到某路径下的这张图片
Graphics g = Graphics.FromImage(cover); //拿到了一个跟这个图片关联的绘图对象
g.DrawImage(water, cover.Width - water.Width, cover.Height - water.Height, water.Width, water.Height); //调用画图的方法
cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); //将这张图片保存在输出流
context.Response.Flush(); //把东西清到客户端
g.Dispose(); //Dispose()[释放,销毁,关闭]类似close()
water.Dispose();
cover.Dispose();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<!-- 引入Test这个页面就相当于引入这个页面的那张图片-->
<img src="Test.aspx" alt="图片" />
</div>
</form>
</body>
</html>