第一步,添加一个一般处理程序(Handler),本例是ImageHandler
代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mime;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
/// <summary>
/// Summary description for ImageHandler
/// </summary>
public class ImageHandler : IHttpHandler
{
public ImageHandler()
{
}
public string GetContentType(String path)
{
switch (Path.GetExtension(path))
{
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default: break;
}
return String.Empty;
}
public ImageFormat GetImageFormat(String path)
{
switch (Path.GetExtension(path).ToLower())
{
case ".bmp": return ImageFormat.Bmp;
case ".gif": return ImageFormat.Gif;
case ".jpg": return ImageFormat.Jpeg;
case ".png": return ImageFormat.Png;
default: return null;
}
}
protected byte[] WatermarkImage(HttpContext context)
{
byte[] imageBytes = null;
if (File.Exists(context.Request.PhysicalPath))
{
// Normally you'd put this in a config file somewhere.
string watermark = "世复检测";
Image image = Image.FromFile(context.Request.PhysicalPath);
新闻热点
疑难解答
图片精选