zoukankan      html  css  js  c++  java
  • 图片加水印

    防止图片被别人转载使用,我们会对图片进行水印处理。

    <%@ WebHandler Language="C#" Class="BookHandler" %>

    using System;

    using System.Web;

    using System.Drawing;

    using System.IO;

     

    public class BookHandler : IHttpHandler {

        private const string COVERSADDR = "~/Image/"; //封面文件夹路径

        private const string WATERMARK_URL = "~/Images/WaterMark.jpg"; //数字水印路径

        private const string DEFAULT_URL = "~/Images/default.jpg"; //默认图片路径

       

        public void ProcessRequest (HttpContext context) {

            string path = context.Request.MapPath(COVERSADDR + context.Request.Params["ISBN"].ToString() + ".jpg");

            Image Cover;

            if (File.Exists(path))

            {

                Cover = Image.FromFile(path);

                Image watermark = Image.FromFile(context.Request.MapPath(WATERMARK_URL));

                Graphics g = Graphics.FromImage(Cover);

                g.DrawImage(watermark, new Rectangle(Cover.Width - watermark.Width, Cover.Height - watermark.Height, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel);

                g.Dispose();

                watermark.Dispose();

            }

            else

            {

                Cover = Image.FromFile(context.Request.MapPath(DEFAULT_URL));

            }

     

            context.Request.ContentType = "image/ipeg";

            Cover.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);

            Cover.Dispose();

            context.Response.End();

        }

     

        public bool IsReusable {

            get {

                return false;

            }

        }

     

    }

    最后在页面或代码中将连接图片的路径都改成

    BookCover.ashx?ISBN=数字就可以了

     

    在iis要配置一下JPG访问

    多思考,多创新,才是正道!
  • 相关阅读:
    排序算法之希尔排序
    排序算法之直接插入排序
    PL/SQL之异常
    PL/SQL之包
    PL/SQL之存储过程和函数
    Oracle左连接、右连接、全外连接以及(+)号用法
    PL/SQL之存储过程和触发器实例
    PL/SQL之游标的使用
    Tag Tree
    目录:JAVA
  • 原文地址:https://www.cnblogs.com/shuang121/p/2074651.html
Copyright © 2011-2022 走看看