zoukankan      html  css  js  c++  java
  • asp.net如何给每张图片动态添加水印方法(二)

    第一步,在App_Code中新建一个类,类名为BookCoverHandler,然后代码如下

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Drawing;

    using System.Drawing.Imaging;

    using System.IO;


    /// <summary>

    ///BookCoverHandler 的摘要说明

    /// </summary>

    public class BookCoverHandler:IHttpHandler

    {

        public void ProcessRequest(HttpContext context)

        {

            //获取请求的图片信息

            //string isbn = context.Request.QueryString["isbn"];


            //找图片

            //string path = context.Server.MapPath("~/Images/BookCovers/" + isbn + ".jpg");

            string path = context.Request.PhysicalPath;

            string defaultPath = context.Server.MapPath("~/images/default.jpg");

            string waterPath = context.Server.MapPath("~/images/watermark.jpg");

            //处理图片

            Image cover;

            //图片存在加水印

            if (File.Exists(path))

            {

                Image water = Image.FromFile(waterPath);

                cover = Image.FromFile(path);

                Graphics g = Graphics.FromImage(cover);

                g.DrawImage(water,

                    cover.Width - water.Width,

                    cover.Height - water.Height,

                    water.Width,

                    water.Height);

                g.Dispose();

            }

            else//图片不存在加载默认图片 

            {

                cover = Image.FromFile(defaultPath);

            }

            context.Response.ContentType = "image/jpeg";

            cover.Save(context.Response.OutputStream, ImageFormat.Jpeg);

            cover.Dispose();

            context.Response.End();

        }


        public bool IsReusable

        {

            get

            {

                return true;

            }

        }

    }

    第二部,在web.config中

        <httpHandlers>

          <!--图片水印-->

          <add verb="*" type="BookCoverHandler" path="Images/BookCovers/*.jpg" />

        </httpHandlers>

    然后就会为Images/BookCovers/下的jpg图片自动添加水印


    经验在于积累----武二郎
  • 相关阅读:
    Android-WebView路由登录192.168.1.1
    Win7 & VirtualBox虚拟Ubuntu 本地虚拟机之间文件共享
    Android 简单的JNI编程
    Android ActionBar简单使用
    多个APK之间简单数据共享
    js代码移动Div 移动平台与PC平台
    JavaScript面向对象
    《SSO CAS单点系列》之 APP原生应用如何访问CAS认证中心
    insh.exe:*** Couldn't reserve space for cygwin's heap,Win32 error 0
    解决:SSM框架中普通类调用Service的问题 (转)
  • 原文地址:https://www.cnblogs.com/zhanghai/p/4461255.html
Copyright © 2011-2022 走看看