<%@ WebHandler Language="C#" Class="ImageWater" %>
using System;
using System.Web;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
public class ImageWater : IHttpHandler
{
private string imagepath = "~/ImageFiles/"; //图片所在的文件夹
private const string waterimage = "~/ImageFiles/kg.png"; //水印图片
private const string defaultimage = "~/ImageFiles/noperson.jpg"; //没有找到图片时显示的图片
private const float alpha = 0.5F; //透明度
public void ProcessRequest(HttpContext context)
{
imagepath += context.Request.QueryString["title"];//获得图片名称
Image image; //图片类
if (File.Exists(context.Server.MapPath(imagepath)))
{
if (Path.GetExtension(context.Server.MapPath(imagepath)).ToLower() == ".gif")
{
//生成的高质量图片名称
//string strGoodFile = context.Request.MapPath(imagepath).Substring(0, context.Request.MapPath(imagepath).LastIndexOf(".")) + ".jpg";
//从文件取得图片对象
image = Image.FromFile(context.Server.MapPath(imagepath));
//取得图片大小
Size size = new Size(380, 380);
//新建一个bmp图片
Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Blue);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
GraphicsUnit.Pixel);
///文字水印
Graphics G = System.Drawing.Graphics.FromImage(bitmap);
Font f = new Font("宋体", 10);
Brush b = new SolidBrush(Color.Black);
G.DrawString("", f, b, 10, 10);
G.Dispose();
///图片水印
Image copyImage = Image.FromFile(context.Server.MapPath(waterimage));
//关于透明度(使用颜色矩阵)
float[][] nArray ={ new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0,alpha , 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colormatrix = new ColorMatrix(nArray);
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
Graphics a = Graphics.FromImage(bitmap);
a.DrawImage(copyImage, new Rectangle(bitmap.Width - copyImage.Width, bitmap.Height - copyImage.Height, copyImage.Width, copyImage.Height),
0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel, attributes);
copyImage.Dispose();
a.Dispose();
copyImage.Dispose();
//保存高清晰度的缩略图
context.Response.ContentType = "image/jpeg";//输出图片的类型
//bitmap.Save(strGoodFile, ImageFormat.Jpeg);
bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);//将图片存入输出流
g.Dispose();
context.Response.End();
}
else
{
image = Image.FromFile(context.Server.MapPath(imagepath));//将图片内容放到image对象
Graphics g = Graphics.FromImage(image); //获得Graphics 对象
Image watermark = Image.FromFile(context.Server.MapPath(waterimage));//将水印图片放入watermark 对象
//关于透明度(使用颜色矩阵)
float[][] nArray ={ new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0,alpha , 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colormatrix = new ColorMatrix(nArray);
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(watermark, new Rectangle(image.Width - watermark.Width, image.Height - watermark.Height,
watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, attributes);
//在图片指定坐标处放入一个矩形图片内容为水印图片
g.Dispose();
watermark.Dispose();
context.Response.ContentType = "image/jpeg";//输出图片的类型
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);//将图片存入输出流
image.Dispose();
context.Response.End();
}
}
else
{
image = Image.FromFile(context.Server.MapPath(defaultimage));//如果不存在图片就输出defaultimage。
context.Response.ContentType = "image/jpeg";//输出图片的类型
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);//将图片存入输出流
image.Dispose();
context.Response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}