asp.net如何给每张图片动态添加水印方法(一)
前台
<asp:ImageButton ID="imgBtn2" runat="server" ImageUrl="~/imageHandler.ashx?isbn=7115147353" Width="128" Height="135"/>
httpHandle后台<%@ WebHandler Language="C#" Class="imageHandler" %>
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
public class imageHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string isbn = context.Request.QueryString["isbn"];
string path = context.Server.MapPath("~/Images/BookCovers/"+isbn+".jpg");
string defaultImg = context.Server.MapPath("~/Images/default.jpg");
string waterImg = context.Server.MapPath("~/Images/WaterMark.jpg");
Image cover;
if (File.Exists(path))
{
//加载水印
Image water = Image.FromFile(waterImg);
//加载文件
cover = Image.FromFile(path);
//实例化画布
Graphics g = Graphics.FromImage(cover);
//在Images上绘制水印
g.DrawImage(
water, cover.Width - water.Width,
cover.Height - water.Height,
water.Width, water.Height
);
g.Dispose();
}
else//图片不存在时加载默认图片
{
cover = Image.FromFile(defaultImg);
}
//设置输出类型为jpeg图片
context.Response.ContentType = "image/jpeg";
//将修改的图片存入输出流
cover.Save(context.Response.OutputStream, ImageFormat.Jpeg);
cover.Dispose();
context.Response.End();
}
public bool IsReusable {
get {
return true;
}
}
}