ASP.NET水印+HttpHandler类用法-

40 篇文章 0 订阅
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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值