asp.net图片加水印代码

作者:袖梨 2022-06-25

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

相关文章

精彩推荐