一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net图片加水印代码

时间:2022-06-25 06:10:46 编辑:袖梨 来源:一聚教程网

<%@ 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访问

热门栏目