/* This gets the size of the graphic so we can determine
* the loop counts and placement of the watermarked text. */
SizeF textSize = graphic.MeasureString(watermark, myFont);
//// Write the text across the image.
//for (int y = 0; y < image.Height; y++)
//{
// for (int x = 0; x < image.Width; x++)
// {
// PointF pointF = new PointF(x, y);
// graphic.DrawString(watermark, myFont, brush, pointF);
// x += Convert.ToInt32(textSize.Width);
// }
// y += Convert.ToInt32(textSize.Height);
//}
// Write the text at the right bottom of the image.
for (int y = image.Height-25; y < image.Height; y++)
{
for (int x = image.Width-100; x < image.Width; x++)
{
PointF pointF = new PointF(x, y);
graphic.DrawString(watermark, myFont, brush, pointF);
x += Convert.ToInt32(textSize.Width);
}
y += Convert.ToInt32(textSize.Height);
}
using (MemoryStream memoryStream = new MemoryStream())
{
image.Save(memoryStream, GetImageFormat(context.Request.PhysicalPath));
imageBytes = memoryStream.ToArray();
}
}
return imageBytes;
}
#region IHttpHandler Members
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.ContentType = GetContentType(context.Request.PhysicalPath);
byte[] imageBytes = WatermarkImage(context);
if (imageBytes != null)
{
context.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);
}
else
{
// No bytes = no image which equals NO FILE.
// Therefore send a 404 - not found response.
context.Response.StatusCode = 404;
}
context.Response.End();
}
#endregion
}
第二步,在web.config里添加如下代码:
复制代码 代码如下: