一般处理程序处理图片(动态给图片加上水印、保存缩略图、验证码)

       对网页所有的图片加水印的方式有 2 种:

  1. 以破坏图片的方式加上水印(这种方式的话,服务器端一般还有原图的备份)
  2. 在图片被请求时动态加上水印

 

文字水印

Html 页面:

<body>
    <!-- 图片 src 属性请求了一个服务器端的一般处理程序 -->
    <img src="AddWaterMarkDynamic.ashx?name=sky11.jpg" alt="" />
</body>

一般处理程序:

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "image/jpeg";
    string msg = "Hello World!";
    string imgName = context.Request.QueryString["name"];
    if (!string.IsNullOrEmpty(imgName))
    {
        string imgPath = context.Server.MapPath("upload/image/" + imgName);
        using (Image img = Bitmap.FromFile(imgPath))
        {
            // 创建一个绘图者,并指定绘制图像
            using (Graphics g = Graphics.FromImage(img))
            {
                g.DrawString(msg, new Font("Verdana", 12, FontStyle.Bold), Brushes.Black, new PointF(5, 8));
                img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            }
        }
    }
}

效果:

image

 

 

图片水印

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "image/jpeg";
    string msg = "Hello World!";
    string imgName = context.Request.QueryString["name"];
    if (!string.IsNullOrEmpty(imgName))
    {
        string imgPath = context.Server.MapPath("upload/image/" + imgName);
        using (Image img = Bitmap.FromFile(imgPath))
        {
            string water = context.Server.MapPath("upload/image/water.jpg");
            using (Image waterImg = Image.FromFile(water))
            {
                using (Graphics g = Graphics.FromImage(img))
                {
                    g.DrawImage(waterImg, (img.Width - waterImg.Width) / 2, (img.Height - waterImg.Height) / 2);
                    img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                }
            }
        }
    }
}

       此示例很简单,可优化的部分有很多,比如重构成一个水印工厂类,来做到精确控制水印内容、位置、文字水印还是图片水印、透明度等等。

 

 

保存缩略图

       在接收到用户上传后的文件,如果文件类型是图片类型,你在保存原图的同时可能还需要保存一张缩略图。

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/html";
 
    HttpPostedFile file = context.Request.Files[0];
    if (file.ContentLength > 0)
    {
        string extName = Path.GetExtension(file.FileName);
        string fileName = Guid.NewGuid().ToString();
        string fullName = fileName + extName;
        string phyFilePath = context.Server.MapPath("~/Upload/Image/") + fullName;
        file.SaveAs(phyFilePath);
        context.Response.Write("上传成功!文件名:" + fullName + "<br />");
 
        if (file.ContentType.Contains("image"))
        {
            context.Response.Write(string.Format("<img src='Upload/Image/{0}'/><br />", fullName));
            using (Image img = Bitmap.FromStream(file.InputStream))
            {
                using (Bitmap smallImg = new Bitmap(120, 40))
                {
                    using (Graphics g = Graphics.FromImage(smallImg))
                    {
                        g.DrawImage(img, new Rectangle(0, 0, smallImg.Width, smallImg.Height),
                            new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
                        smallImg.Save(context.Server.MapPath("~/Upload/Image/") + fileName + "_small" + extName);
                        context.Response.Write(string.Format("缩略图保存成功!<br />"));
                        context.Response.Write(string.Format("<img src='Upload/Image/{0}'/><br />", (fileName + "_small" + extName)));
                    }
                }
            }
        }
    }
}

 

 

一般处理程序创建简单的数字验证码图片

       一般处理程序中要使用 Session 对象,必须实现一个标记接口:IRequiresSessionState 。微软在后期的开发,对于标记都采用了 Attribute 属性,但在开发 Session 时,还是启用了空的标记接口的做法。

public class ValidateCode : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/jpeg";
        string code = CreateRandomNumbers();
        context.Session["code"] = code;
        using (Bitmap img = new Bitmap(100, 40))
        {
            using (Graphics g = Graphics.FromImage(img))
            {
                g.DrawString(code, new Font("微软雅黑", 16), Brushes.White, 15, 7);
                img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            }
        }
    }
 
    public string CreateRandomNumbers()
    {
        Random r = new Random();
        string str = string.Empty;
        for (int i = 0; i < 5; i++)
        {
            str += r.Next(10).ToString();
        }
        return str;
    }
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

 

       在前台页面实现点击图片生成新的验证码效果,只需加上一小段 js 代码,用来操纵图片的 src 属性即可(随机数使浏览器的缓存机制失效):

<script type="text/javascript">
    window.onload = function () {
        document.getElementById("vCodeImg").onclick = function () {
            this.src = "ValidateCode.ashx?a=" + new Date();
        };
    }
</script>
原文地址:https://www.cnblogs.com/SkySoot/p/3525700.html