public class ImageHandler:IHttpHandler
{
public ImageHandler()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IHttpHandler 成员
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
//throw new Exception("The method or operation is not implemented.");
//获取请求的物理图片路径
string imagePath = context.Request.PhysicalPath;
Bitmap image = null;
if (context.Cache[imagePath] == null)//如果当前缓存中没有指定的图片就将该图片添加水印并缓存
{
image = new Bitmap(imagePath);
image = AddWaterMark(image);
context.Cache[imagePath] = image;
}
else//否则就直接从混存中取出添加了水印的图片,节省时间
{
image = context.Cache[imagePath] as Bitmap;
}
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);//将添加水印的图片输入到当前流中
}
#endregion
private Bitmap AddWaterMark(Bitmap image)
{
string text = System.Configuration.ConfigurationManager.AppSettings["WaterMark"].ToString();
int fontSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Font-Size"].ToString());
Font font = new Font("宋体", fontSize);
//Brush brush = Brushes.DarkGray;
Brush brush = Brushes.Red;
Graphics g = Graphics.FromImage(image);
SizeF size = g.MeasureString(text, font);
g.DrawString(text, font, brush, image.Width - size.Width, image.Height - size.Height);
g.Dispose();
return image;
}
}
public class ImageHandler:IHttpHandler
{
public ImageHandler()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IHttpHandler 成员
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
//throw new Exception("The method or operation is not implemented.");
//获取请求的物理图片路径
string imagePath = context.Request.PhysicalPath;
Bitmap image = null;
if (context.Cache[imagePath] == null)//如果当前缓存中没有指定的图片就将该图片添加水印并缓存
{
image = new Bitmap(imagePath);
image = AddWaterMark(image);
context.Cache[imagePath] = image;
}
else//否则就直接从混存中取出添加了水印的图片,节省时间
{
image = context.Cache[imagePath] as Bitmap;
}
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);//将添加水印的图片输入到当前流中
}
#endregion
private Bitmap AddWaterMark(Bitmap image)
{
string text = System.Configuration.ConfigurationManager.AppSettings["WaterMark"].ToString();
int fontSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Font-Size"].ToString());
Font font = new Font("宋体", fontSize);
//Brush brush = Brushes.DarkGray;
Brush brush = Brushes.Red;
Graphics g = Graphics.FromImage(image);
SizeF size = g.MeasureString(text, font);
g.DrawString(text, font, brush, image.Width - size.Width, image.Height - size.Height);
g.Dispose();
return image;
}
}
参照: http://blog.csdn.net/zhoufoxcn/archive/2008/01/10/2033530.aspx
view plaincopy to clipboardprint?
<appSettings>
<add key="WaterMark" value="动态添加水印的内容"/>
<add key="font-size" value="20"/>
</appSettings>
<httpHandlers>
<add path="*.jpg,*.bmp" verb="*" type="ImageHandler"/>
//可以制定路径 path="image/*.jpg"
</httpHandlers>
<appSettings>
<add key="WaterMark" value="动态添加水印的内容"/>
<add key="font-size" value="20"/>
</appSettings>
<httpHandlers>
<add path="*.jpg,*.bmp" verb="*" type="ImageHandler"/>
//可以制定路径 path="image/*.jpg"
</httpHandlers>
在本地测试正常 可是在IIS上图片就没有正常显示
这时:在网站上目录上右击,选择属性,选择主目录,配置,
随便选择列表中的一行,点击编缉,复制执行文件的路径,然后关闭,再点击添加,可执行文件用粘贴就可以了,后缀名写上.jpg,点击确定。按此方法再添加其他后缀名
本文来自CSDN博客,转载请标明出处: http://blog.csdn.net/hei25hei/archive/2010/08/10/5801497.aspx
{
public ImageHandler()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IHttpHandler 成员
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
//throw new Exception("The method or operation is not implemented.");
//获取请求的物理图片路径
string imagePath = context.Request.PhysicalPath;
Bitmap image = null;
if (context.Cache[imagePath] == null)//如果当前缓存中没有指定的图片就将该图片添加水印并缓存
{
image = new Bitmap(imagePath);
image = AddWaterMark(image);
context.Cache[imagePath] = image;
}
else//否则就直接从混存中取出添加了水印的图片,节省时间
{
image = context.Cache[imagePath] as Bitmap;
}
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);//将添加水印的图片输入到当前流中
}
#endregion
private Bitmap AddWaterMark(Bitmap image)
{
string text = System.Configuration.ConfigurationManager.AppSettings["WaterMark"].ToString();
int fontSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Font-Size"].ToString());
Font font = new Font("宋体", fontSize);
//Brush brush = Brushes.DarkGray;
Brush brush = Brushes.Red;
Graphics g = Graphics.FromImage(image);
SizeF size = g.MeasureString(text, font);
g.DrawString(text, font, brush, image.Width - size.Width, image.Height - size.Height);
g.Dispose();
return image;
}
}
public class ImageHandler:IHttpHandler
{
public ImageHandler()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IHttpHandler 成员
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
//throw new Exception("The method or operation is not implemented.");
//获取请求的物理图片路径
string imagePath = context.Request.PhysicalPath;
Bitmap image = null;
if (context.Cache[imagePath] == null)//如果当前缓存中没有指定的图片就将该图片添加水印并缓存
{
image = new Bitmap(imagePath);
image = AddWaterMark(image);
context.Cache[imagePath] = image;
}
else//否则就直接从混存中取出添加了水印的图片,节省时间
{
image = context.Cache[imagePath] as Bitmap;
}
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);//将添加水印的图片输入到当前流中
}
#endregion
private Bitmap AddWaterMark(Bitmap image)
{
string text = System.Configuration.ConfigurationManager.AppSettings["WaterMark"].ToString();
int fontSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Font-Size"].ToString());
Font font = new Font("宋体", fontSize);
//Brush brush = Brushes.DarkGray;
Brush brush = Brushes.Red;
Graphics g = Graphics.FromImage(image);
SizeF size = g.MeasureString(text, font);
g.DrawString(text, font, brush, image.Width - size.Width, image.Height - size.Height);
g.Dispose();
return image;
}
}
参照: http://blog.csdn.net/zhoufoxcn/archive/2008/01/10/2033530.aspx
view plaincopy to clipboardprint?
<appSettings>
<add key="WaterMark" value="动态添加水印的内容"/>
<add key="font-size" value="20"/>
</appSettings>
<httpHandlers>
<add path="*.jpg,*.bmp" verb="*" type="ImageHandler"/>
//可以制定路径 path="image/*.jpg"
</httpHandlers>
<appSettings>
<add key="WaterMark" value="动态添加水印的内容"/>
<add key="font-size" value="20"/>
</appSettings>
<httpHandlers>
<add path="*.jpg,*.bmp" verb="*" type="ImageHandler"/>
//可以制定路径 path="image/*.jpg"
</httpHandlers>
在本地测试正常 可是在IIS上图片就没有正常显示
这时:在网站上目录上右击,选择属性,选择主目录,配置,
随便选择列表中的一行,点击编缉,复制执行文件的路径,然后关闭,再点击添加,可执行文件用粘贴就可以了,后缀名写上.jpg,点击确定。按此方法再添加其他后缀名
本文来自CSDN博客,转载请标明出处: http://blog.csdn.net/hei25hei/archive/2010/08/10/5801497.aspx