asp.net如何给每张图片动态添加水印方法(二)
2013-10-10 13:20
806 查看
第一步,在App_Code中新建一个类,类名为BookCoverHandler,然后代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
/// <summary>
///BookCoverHandler 的摘要说明
/// </summary>
public class BookCoverHandler:IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//获取请求的图片信息
//string isbn = context.Request.QueryString["isbn"];
//找图片
//string path = context.Server.MapPath("~/Images/BookCovers/" + isbn + ".jpg");
string path = context.Request.PhysicalPath;
string defaultPath = context.Server.MapPath("~/images/default.jpg");
string waterPath = context.Server.MapPath("~/images/watermark.jpg");
//处理图片
Image cover;
//图片存在加水印
if (File.Exists(path))
{
Image water = Image.FromFile(waterPath);
cover = Image.FromFile(path);
Graphics g = Graphics.FromImage(cover);
g.DrawImage(water,
cover.Width - water.Width,
cover.Height - water.Height,
water.Width,
water.Height);
g.Dispose();
}
else//图片不存在加载默认图片
{
cover = Image.FromFile(defaultPath);
}
context.Response.ContentType = "image/jpeg";
cover.Save(context.Response.OutputStream, ImageFormat.Jpeg);
cover.Dispose();
context.Response.End();
}
public bool IsReusable
{
get
{
return true;
}
}
}
第二部,在web.config中
<httpHandlers>
<!--图片水印-->
<add verb="*" type="BookCoverHandler" path="Images/BookCovers/*.jpg" />
</httpHandlers>
然后就会为Images/BookCovers/下的jpg图片自动添加水印
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
/// <summary>
///BookCoverHandler 的摘要说明
/// </summary>
public class BookCoverHandler:IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//获取请求的图片信息
//string isbn = context.Request.QueryString["isbn"];
//找图片
//string path = context.Server.MapPath("~/Images/BookCovers/" + isbn + ".jpg");
string path = context.Request.PhysicalPath;
string defaultPath = context.Server.MapPath("~/images/default.jpg");
string waterPath = context.Server.MapPath("~/images/watermark.jpg");
//处理图片
Image cover;
//图片存在加水印
if (File.Exists(path))
{
Image water = Image.FromFile(waterPath);
cover = Image.FromFile(path);
Graphics g = Graphics.FromImage(cover);
g.DrawImage(water,
cover.Width - water.Width,
cover.Height - water.Height,
water.Width,
water.Height);
g.Dispose();
}
else//图片不存在加载默认图片
{
cover = Image.FromFile(defaultPath);
}
context.Response.ContentType = "image/jpeg";
cover.Save(context.Response.OutputStream, ImageFormat.Jpeg);
cover.Dispose();
context.Response.End();
}
public bool IsReusable
{
get
{
return true;
}
}
}
第二部,在web.config中
<httpHandlers>
<!--图片水印-->
<add verb="*" type="BookCoverHandler" path="Images/BookCovers/*.jpg" />
</httpHandlers>
然后就会为Images/BookCovers/下的jpg图片自动添加水印
相关文章推荐
- asp.net如何给每张图片动态添加水印方法(一)
- asp.net通过实现IHttpHandler接口方法给图片添加水印图片(动态添加,不破坏原图片,但是耗资源)
- asp.net下用Aspose.Words for .NET动态生成word文档中的图片或水印的方法
- ASP.NET 给图片动态添加文字水印
- asp.net图片添加水印以及GDI+ 中发生一般性错误的解决方法
- asp.net动态添加图片水印
- asp .net 为图片添加文字水印(内包含有加图片水印的方法,但尚未完善暂不能使用)
- asp.net下用Aspose.Words for .NET动态生成word文档中的图片或水印的方法
- asp .net实现给图片添加图片水印方法示例
- asp .net 为图片添加文字水印(内包含有加图片水印的方法) .
- asp.net、C#动态添加图片水印
- ASP.NET 给图片动态添加文字水印
- asp.net下用Aspose.Words for .NET动态生成word文档中的图片或水印的方法(转)
- ASP.NET 如何动态修改 Header 属性如添加 Meta 标签 keywords description!
- asp.net为图片添加文字水印
- ASP.NET MVC 5 (二)动态输出、辅助器方法、模型绑定与添加验证
- asp.net 动态添加JavaScript方法
- asp.net动态添加js文件调用到网页的方法
- ASP.NET Ueditor上传图片添加水印