using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace xumh
{
/// <summary>
/// 图片处理模块:给指定路径下的图片添加水印输出
/// 1.监视的目录路径,水印图片路径,默认图片三者都在web.config文件中设定
/// 2.请求的图片的路径不属于监视路径,直接输出图片
/// 3.请求的图片的路径属于监视路径,若图片不存在,显示默认图片,不加水印
/// 4.请求的图片的路径属于监视路径,且文件存在,添加水印
/// 水印图片存在:图片+水印图片,输出
/// 水印图片不存在:图片+水印文字,输出
///
/// 5.web.config部分如下:
/// <appSettings>
/// <!--要监视的路径-->
/// <add key="monitorPath" value="/images/bookcovers/"/>
/// <!--图书封面不存在时显示的默认封面图片的相对路径-->
/// <add key="defaultImage" value="images\default.jpg"/>
/// <!--相对于物理应用程序的根路径的水印图片的相对路径-->
/// <add key="watermarkImage" value="images\WaterMark.jpg"/>
/// </appSettings>
///
/// <httpHandlers>
/// <add verb="*" path="*.jpg" type="xumh.WatermarkHandler,WatermarkHandler"/>
/// </httpHandlers>
/// 许明会 2007-12-23 18:00 Visual Studio 2005 调试通过
/// </summary>
public class WatermarkHandler:IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
string url = context.Request.Url.AbsoluteUri.ToLower();
string monitorPath = System.Configuration.ConfigurationManager.AppSettings["monitorPath"];
//若图片不属于封面,直接输出图片:若图片不存在不做处理
bool IsInterestUrl = url.Contains(monitorPath);
System.Drawing.Image imgSource = null;
if (!IsInterestUrl)
{
if (!System.IO.File.Exists(context.Request.PhysicalPath))
return;
imgSource = System.Drawing.Image.FromFile(context.Request.PhysicalPath);
imgSource.Save(context.Response.OutputStream, imgSource.RawFormat);
imgSource.Dispose();
return;
}
//图片属于封面,但图片imageSource不存在,显示默认图片default.jpg
string physicalPath = context.Request.PhysicalPath;// context.Server.MapPath(url);
if (!System.IO.File.Exists(physicalPath))
{
string defaultImage = System.Configuration.ConfigurationManager.AppSettings["defaultImage"];
imgSource = System.Drawing.Image.FromFile(context.Request.PhysicalApplicationPath + defaultImage);
imgSource.Save(context.Response.OutputStream, imgSource.RawFormat);
imgSource.Dispose();
return;
}
//--===--------------------只要有封面图片,就为图片添加水印----------------------===---
string watermarkImage = System.Configuration.ConfigurationManager.AppSettings["watermarkImage"];
string watermarkImagePath = context.Request.PhysicalApplicationPath + watermarkImage;
bool bWatermarkImageExist =System.IO.File.Exists(watermarkImagePath);
imgSource = System.Drawing.Image.FromFile(physicalPath);
System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(imgSource);
if (bWatermarkImageExist) //“水印图片”存在:存在则封面混合水印图片
{
System.Drawing.Image imgWatermark = System.Drawing.Image.FromFile(watermarkImagePath);
graphic.DrawImage(imgWatermark,
imgSource.Width - imgWatermark.Width, imgSource.Height - imgWatermark.Height,
imgWatermark.Width, imgWatermark.Height);
//把图片保存到输出流
imgSource.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
imgWatermark.Dispose();
}
else //“水印图片”不存在:输出文本
{
System.Drawing.Font font = new System.Drawing.Font("幼圆", 13.0f,System.Drawing.FontStyle.Bold);
graphic.DrawString("第三波+书店",font , System.Drawing.Brushes.Red, new System.Drawing.PointF());
imgSource.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
imgSource.Dispose();
graphic.Dispose();
//标明类型为jpg:如果不标注,IE没有问题,但Firefox会出现乱码
context.Response.ContentType = "image/jpeg";
context.Response.Flush();
context.Response.End();
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace xumh
{
/// <summary>
/// 图片处理模块:给指定路径下的图片添加水印输出
/// 1.监视的目录路径,水印图片路径,默认图片三者都在web.config文件中设定
/// 2.请求的图片的路径不属于监视路径,直接输出图片
/// 3.请求的图片的路径属于监视路径,若图片不存在,显示默认图片,不加水印
/// 4.请求的图片的路径属于监视路径,且文件存在,添加水印
/// 水印图片存在:图片+水印图片,输出
/// 水印图片不存在:图片+水印文字,输出
///
/// 5.web.config部分如下:
/// <appSettings>
/// <!--要监视的路径-->
/// <add key="monitorPath" value="/images/bookcovers/"/>
/// <!--图书封面不存在时显示的默认封面图片的相对路径-->
/// <add key="defaultImage" value="images\default.jpg"/>
/// <!--相对于物理应用程序的根路径的水印图片的相对路径-->
/// <add key="watermarkImage" value="images\WaterMark.jpg"/>
/// </appSettings>
///
/// <httpHandlers>
/// <add verb="*" path="*.jpg" type="xumh.WatermarkHandler,WatermarkHandler"/>
/// </httpHandlers>
/// 许明会 2007-12-23 18:00 Visual Studio 2005 调试通过
/// </summary>
public class WatermarkHandler:IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
string url = context.Request.Url.AbsoluteUri.ToLower();
string monitorPath = System.Configuration.ConfigurationManager.AppSettings["monitorPath"];
//若图片不属于封面,直接输出图片:若图片不存在不做处理
bool IsInterestUrl = url.Contains(monitorPath);
System.Drawing.Image imgSource = null;
if (!IsInterestUrl)
{
if (!System.IO.File.Exists(context.Request.PhysicalPath))
return;
imgSource = System.Drawing.Image.FromFile(context.Request.PhysicalPath);
imgSource.Save(context.Response.OutputStream, imgSource.RawFormat);
imgSource.Dispose();
return;
}
//图片属于封面,但图片imageSource不存在,显示默认图片default.jpg
string physicalPath = context.Request.PhysicalPath;// context.Server.MapPath(url);
if (!System.IO.File.Exists(physicalPath))
{
string defaultImage = System.Configuration.ConfigurationManager.AppSettings["defaultImage"];
imgSource = System.Drawing.Image.FromFile(context.Request.PhysicalApplicationPath + defaultImage);
imgSource.Save(context.Response.OutputStream, imgSource.RawFormat);
imgSource.Dispose();
return;
}
//--===--------------------只要有封面图片,就为图片添加水印----------------------===---
string watermarkImage = System.Configuration.ConfigurationManager.AppSettings["watermarkImage"];
string watermarkImagePath = context.Request.PhysicalApplicationPath + watermarkImage;
bool bWatermarkImageExist =System.IO.File.Exists(watermarkImagePath);
imgSource = System.Drawing.Image.FromFile(physicalPath);
System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(imgSource);
if (bWatermarkImageExist) //“水印图片”存在:存在则封面混合水印图片
{
System.Drawing.Image imgWatermark = System.Drawing.Image.FromFile(watermarkImagePath);
graphic.DrawImage(imgWatermark,
imgSource.Width - imgWatermark.Width, imgSource.Height - imgWatermark.Height,
imgWatermark.Width, imgWatermark.Height);
//把图片保存到输出流
imgSource.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
imgWatermark.Dispose();
}
else //“水印图片”不存在:输出文本
{
System.Drawing.Font font = new System.Drawing.Font("幼圆", 13.0f,System.Drawing.FontStyle.Bold);
graphic.DrawString("第三波+书店",font , System.Drawing.Brushes.Red, new System.Drawing.PointF());
imgSource.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
imgSource.Dispose();
graphic.Dispose();
//标明类型为jpg:如果不标注,IE没有问题,但Firefox会出现乱码
context.Response.ContentType = "image/jpeg";
context.Response.Flush();
context.Response.End();
}
}
}