欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net中如何實(shí)現(xiàn)水印

 更新時(shí)間:2013年09月27日 17:52:14   作者:  
水印的實(shí)現(xiàn)方法有很多,而且各種各樣,在本文將為大家介紹下在asp.net中時(shí)如何實(shí)現(xiàn)的,如果你不會(huì)可以參考下

水印是為了防止別盜用我們的圖片.

兩種方式實(shí)現(xiàn)水印效果

1)可以在用戶(hù)上傳時(shí)添加水印.

a) 好處:與2種方法相比,用戶(hù)每次讀取此圖片時(shí),服務(wù)器直接發(fā)送給客戶(hù)就行了.

b) 缺點(diǎn):破壞了原始圖片.

2)通過(guò)全局的一般處理程序,當(dāng)用戶(hù)請(qǐng)求這張圖片時(shí),加水印.

a) 好處:原始圖片沒(méi)有被破壞

b) 缺點(diǎn):用戶(hù)每次請(qǐng)求時(shí)都需要對(duì)請(qǐng)求的圖片進(jìn)行加水印處理,浪費(fèi)的服務(wù)器的資源.

代碼實(shí)現(xiàn)第二種方式:

復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;

namespace BookShop.Web
{
public class WaterMark : IHttpHandler
{

private const string WATERMARK_URL = "~/Images/watermark.jpg"; //水印圖片
private const string DEFAULTIMAGE_URL = "~/Images/default.jpg";<span style="white-space:pre"> </span> //默認(rèn)圖片
#region IHttpHandler 成員

public bool IsReusable
{
get { return false; }
}

public void ProcessRequest(HttpContext context)
{

//context.Request.PhysicalPath //獲得用戶(hù)請(qǐng)求的文件物理路徑

System.Drawing.Image Cover;
//判斷請(qǐng)求的物理路徑中,是否存在文件
if (File.Exists(context.Request.PhysicalPath))
{
//加載文件
Cover = Image.FromFile(context.Request.PhysicalPath);
//加載水印圖片
Image watermark = Image.FromFile(context.Request.MapPath(WATERMARK_URL));
//通過(guò)書(shū)的封面得到繪圖對(duì)像
Graphics g = Graphics.FromImage(Cover);
//在image上繪制水印
g.DrawImage(watermark, new Rectangle(Cover.Width - watermark.Width, Cover.Height - watermark.Height,

復(fù)制代碼 代碼如下:

watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel);
//釋放畫(huà)布
g.Dispose();
//釋放水印圖片
watermark.Dispose();
}
else
{
//加載默認(rèn)圖片
Cover = Image.FromFile(context.Request.MapPath(DEFAULTIMAGE_URL));
}
//設(shè)置輸出格式
context.Response.ContentType = "image/jpeg";
//將圖片存入輸出流
Cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Cover.Dispose();
context.Response.End();
}

#endregion
}
}

相關(guān)文章

最新評(píng)論