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

ASP.NET 圖片加水印防盜鏈實(shí)現(xiàn)代碼

 更新時(shí)間:2011年12月22日 22:32:01   作者:  
ASP.NET 圖片加水印防盜鏈實(shí)現(xiàn)代碼,需要的朋友可以參考下。
首先建一個(gè)類:
復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
/// <summary>
///Class1 的摘要說(shuō)明
/// </summary>
public class Class1:IHttpHandler //調(diào)用接口
{
public Class1()
{
//
//TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
HttpRequest req = context.Request;
if (req.UrlReferrer != null && req.UrlReferrer.Host.Length > 0) //反盜鏈代碼判斷
{
System.Drawing.Image img = System.Drawing.Image.FromFile(context.Request.PhysicalPath);
System.Drawing.Graphics g = Graphics.FromImage(img);
g.DrawString("三國(guó)演義", new Font("宋體", 20, FontStyle.Bold), Brushes.White, 10, 10);
img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.Flush();
context.Response.End();
}
else
{
context.Response.Write("您不能盜鏈本站圖片");
}
}
}

在web.config中注冊(cè)接口:
復(fù)制代碼 代碼如下:

<httpHandlers>
<add verb="*" path="images/*.jpg" type="Class1,App_Code"/>
</httpHandlers>

url:http://greatverve.cnblogs.com/archive/2011/12/20/asp-net-hotlinking.html
參考:
1.修改web.config
復(fù)制代碼 代碼如下:

<system.web>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<!--解決圖片防盜鏈問(wèn)題-->
<add verb="*" path="*.jpg" type="MyHttpHandler.Watermark"/>
<add verb="*" path="*.gif" type="MyHttpHandler.Watermark"/>
<add verb="*" path="*.png" type="MyHttpHandler.Watermark"/>
</httpHandlers>
</system.web>

2.添加一個(gè)一般執(zhí)行文件Watermark.ashx,代碼如下:
復(fù)制代碼 代碼如下:

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
namespace MyHttpHandler
{
/// <summary>
/// Summary description for $codebehindclassname$
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Watermark : IHttpHandler
{
public bool IsReusable
{
get
{
return false;
}
}
public void ProcessRequest(HttpContext context)
{
//設(shè)置客戶端緩沖時(shí)間過(guò)期時(shí)間為0,即立即過(guò)期
//context.Response.Expires = 0;
//清空服務(wù)器端為此會(huì)話開啟的輸出緩存
//context.Response.Clear();
//設(shè)置輸出文件類型
context.Response.ContentType = "image/jpg";
//將請(qǐng)求文件寫入到輸出緩存中
#region 獲取XML配置信息
DataSet dsConfing = new DataSet();
string watermarkConfigPath = context.Server.MapPath("~/Config/WaterMarkConfig.xml");
if (System.IO.File.Exists(watermarkConfigPath))
dsConfing.ReadXml(watermarkConfigPath);
else
{
//添加默認(rèn)的水印配置
}
DataRow drConfing = dsConfing.Tables[0].Rows[0];
#endregion
string currentHost = drConfing["allowhost"].ToString();
//判斷是否是本地網(wǎng)站引用圖片,如果是則返回正確的圖片
if (context.Request.Url.Authority.Equals(currentHost, StringComparison.InvariantCultureIgnoreCase))
{
string localPath = context.Request.Url.LocalPath;
localPath = localPath.Remove(localPath.LastIndexOf('/')).ToLower();// "/images/userphoto"
if (drConfing["isflag"].Equals("true") && drConfing["files"].ToString().ToLower().IndexOf(localPath) > 0)
{
#region 水印代碼
string sImgStartPhysicalPath = context.Request.PhysicalPath;
System.Drawing.Image imgStart = System.Drawing.Image.FromFile(sImgStartPhysicalPath);
//備份原圖片
//int indexOf = sImgStartPhysicalPath.LastIndexOf(".");
//string bakPath = sImgStartPhysicalPath.Remove(indexOf) + "_bak" + sImgStartPhysicalPath.Substring(indexOf);
//imgStart.Save(bakPath);
Graphics gh = System.Drawing.Graphics.FromImage(imgStart);
if (drConfing["type"].Equals("img"))
{
System.Drawing.Image imgWatermark = System.Drawing.Image.FromFile(context.Server.MapPath(drConfing["img-path"].ToString()));
Rectangle rg = SetImgPosition(drConfing["position"].ToString(), imgStart.Width, imgStart.Height, imgWatermark.Width, imgWatermark.Height);
gh.DrawImage(imgWatermark, rg, 0, 0, imgWatermark.Width, imgWatermark.Height, GraphicsUnit.Pixel);
gh.Save();
gh.Dispose();
imgWatermark.Dispose();
}
else if (drConfing["type"].Equals("font"))
{
//文字水印
string content = drConfing["font-content"].ToString();
float Size = (float)Convert.ToDouble(drConfing["font-size"].ToString());
FontStyle fontStyle = (FontStyle)int.Parse(drConfing["font-style"].ToString());
System.Drawing.Font f = new System.Drawing.Font("Arial", Size, fontStyle);
Color G_Color = Color.FromName(drConfing["font-color"].ToString());
System.Drawing.Brush b = new System.Drawing.SolidBrush(G_Color);
SizeF sizeF = gh.MeasureString(content, f);
gh.DrawString(content, f, b, SetFontPosition(drConfing["position"].ToString(), imgStart.Width, imgStart.Height, (int)sizeF.Width, (int)sizeF.Height));
gh.Save();
gh.Dispose();
}
//將請(qǐng)求文件寫入到輸出緩存中
imgStart.Save(context.Response.OutputStream, ImageFormat.Jpeg);
imgStart.Dispose();
#endregion
}
else
{
#region 輸出原圖
//將請(qǐng)求文件寫入到輸出緩存中
context.Response.WriteFile(context.Request.Url.AbsolutePath);
#endregion
}
}
//如果不是本地引用,則是盜鏈本站圖片
else
{
//將請(qǐng)求文件寫入到輸出緩存中
context.Response.WriteFile(context.Request.PhysicalApplicationPath + drConfing["errimgpath"].ToString());
}
//將輸出緩存中的信息傳送到客戶端
context.Response.End();
}
/// <summary>
/// 圖片繪畫水印的位置
/// </summary>
/// <param name="positionConfig">位置類型</param>
/// <param name="width">原圖片寬</param>
/// <param name="height"></param>
/// <param name="watermarkWidth">水印圖寬</param>
/// <param name="watermarkHeight"></param>
/// <returns></returns>
private Rectangle SetImgPosition(string positionConfig,int width,int height,int watermarkWidth,int watermarkHeight)
{
int xpos = 0;
int ypos = 0;
int margin = 10;
int width_margin = width - margin;
int height_margin = height - margin;
double proportion = 1d;//水印圖片縮放比例
//int
if ((width_margin > watermarkWidth * proportion) && (height_margin > watermarkHeight * proportion))
{
}
else if ((width_margin > watermarkWidth * proportion) && (height_margin < watermarkHeight * proportion))
{
proportion = Convert.ToDouble( height_margin) / Convert.ToDouble( watermarkHeight);
}
else if ((width_margin < watermarkWidth * proportion) && (height_margin > watermarkHeight * proportion))
{
proportion = Convert.ToDouble(width_margin) / Convert.ToDouble(watermarkWidth);
}
else
{
double proportionW = Convert.ToDouble(width_margin) / Convert.ToDouble(watermarkWidth);
double proportionH = Convert.ToDouble(height_margin) / Convert.ToDouble(watermarkHeight);
proportion = proportionW >= proportionH ? proportionH : proportionW;
}
watermarkWidth = Convert.ToInt32(watermarkWidth * proportion);
watermarkHeight = Convert.ToInt32(watermarkHeight * proportion);
switch (positionConfig)
{
case "top-left":
xpos = margin;
ypos = margin;
break;
case "top-right":
xpos = width_margin - watermarkWidth;
ypos = margin;
break;
case "bottom-left":
xpos = margin;
ypos = height_margin - watermarkHeight;
break;
case "bottom-right":
xpos = width_margin - watermarkWidth ;
ypos = height_margin - watermarkHeight ;
break;
default:
xpos = width_margin - watermarkWidth ;
ypos = height_margin - watermarkHeight;
break;
}
return new Rectangle(xpos,ypos,watermarkWidth,watermarkHeight);
}
/// <summary>
/// 圖片繪畫文字位置
/// </summary>
/// <param name="positionConfig">位置類型</param>
/// <param name="width">原圖片寬</param>
/// <param name="height"></param>
/// <param name="fontWidth">文字長(zhǎng)度</param>
/// <param name="fontHeight"></param>
/// <returns></returns>
private Point SetFontPosition(string positionConfig, int width, int height, int fontWidth, int fontHeight)
{
int xpos = 0;
int ypos = 0;
int margin = 10;
int width_margin = width - margin;
int height_margin = height - margin;
double proportion = 1d;//水印圖片縮放比例
//int
if ((width_margin > fontWidth * proportion) && (height_margin > fontHeight * proportion))
{
}
else if ((width_margin > fontWidth * proportion) && (height_margin < fontHeight * proportion))
{
proportion = Convert.ToDouble(height_margin) / Convert.ToDouble(fontHeight);
}
else if ((width_margin < fontWidth * proportion) && (height_margin > fontHeight * proportion))
{
proportion = Convert.ToDouble(width_margin) / Convert.ToDouble(fontWidth);
}
else
{
double proportionH = Convert.ToDouble(height_margin) / Convert.ToDouble(fontHeight);
double proportionW = Convert.ToDouble(width_margin) / Convert.ToDouble(fontWidth);
proportion = proportionW >= proportionH ? proportionH : proportionW;
}
fontWidth = Convert.ToInt32(fontWidth * proportion);
fontHeight = Convert.ToInt32(fontHeight * proportion);
switch (positionConfig)
{
case "top-left":
xpos = margin;
ypos = margin;
break;
case "top-right":
xpos = width_margin - fontWidth;
ypos = margin;
break;
case "bottom-left":
xpos = margin;
ypos = height_margin - fontHeight;
break;
case "bottom-right":
xpos = width_margin - fontWidth;
ypos = height_margin - fontHeight;
break;
default:
xpos = width_margin - fontWidth;
ypos = height_margin - fontHeight;
break;
}
return new Point(xpos, ypos);
}
}
}

3.配置文件的WaterMarkConfig.xml,內(nèi)容如下:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8" ?>
<watermark>
<allowhost>localhost:6219</allowhost><!--允許訪問(wèn)的域名-->
<isflag>true</isflag><!-- true、false-->
<type>font</type><!-- img、font-->
<files>/config|/upfiles/ab</files><!--需要加水印的文件夾-->
<position>bottom-right</position><!-- top-left、top-right、bottom-left、bottom-right-->
<img-path>~/UpFiles/Watermark.png</img-path><!-- 水印位置 -->
<font-style>1</font-style><!--普通文本 0, 加粗文本 1, 傾斜文本 2, 帶下劃線的文本 4, 中間有直線通過(guò)的文本 8-->
<font-size>60</font-size>
<font-color>red</font-color>
<font-content>¥:8000元</font-content>
<errimgpath>images/error.jpg</errimgpath><!-- 盜圖片的請(qǐng)求返回的跟目錄下的某圖片 -->
</watermark>

相關(guān)文章

  • ASP.NET Core 7 Razor Pages項(xiàng)目發(fā)布到IIS的詳細(xì)過(guò)程

    ASP.NET Core 7 Razor Pages項(xiàng)目發(fā)布到IIS的詳細(xì)過(guò)程

    這篇文章主要介紹了ASP.NET Core 7 Razor Pages項(xiàng)目發(fā)布到IIS的詳細(xì)過(guò)程,詳細(xì)介紹了發(fā)布過(guò)程遇到的問(wèn)題及解決方法,對(duì)ASP.NET Core 發(fā)布到IIS相關(guān)知識(shí)感興趣的朋友一起看看吧
    2023-01-01
  • asp.net ListView交替背景顏色實(shí)現(xiàn)代碼

    asp.net ListView交替背景顏色實(shí)現(xiàn)代碼

    在asp.net中ListView的交替背景顏色實(shí)現(xiàn),GridView的處理得較多,ListView可以這樣實(shí)現(xiàn)。
    2010-02-02
  • ASP.NET服務(wù)器控件開發(fā)(1)封裝html

    ASP.NET服務(wù)器控件開發(fā)(1)封裝html

    在我們的項(xiàng)目開發(fā)中,由于ASP.NET的服務(wù)器控件功能有限,所以我們經(jīng)常會(huì)自己定義特定的服務(wù)器控件,來(lái)滿足開發(fā)中特定的業(yè)務(wù)要求??梢娭廊绾伍_發(fā)ASP.NET服務(wù)器控件是非常有必要的
    2015-12-12
  • ASP.NET MVC阿里大于短信接口開發(fā)短信群發(fā)能

    ASP.NET MVC阿里大于短信接口開發(fā)短信群發(fā)能

    這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC阿里大于短信接口來(lái)開發(fā)例會(huì)短信群發(fā)能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • .Net中的集合排序可以這么玩你知道嗎

    .Net中的集合排序可以這么玩你知道嗎

    集合為處理大量數(shù)據(jù)時(shí)所用到一種容器類。簡(jiǎn)單講就是數(shù)據(jù)結(jié)構(gòu)算法的具體平臺(tái)上的實(shí)現(xiàn)。下面這篇文章主要給大家介紹了關(guān)于.Net中集合排序的一些你可能不知道的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2018-04-04
  • .net使用自定義類屬性實(shí)例

    .net使用自定義類屬性實(shí)例

    這篇文章主要介紹了.net使用自定義類屬性實(shí)例,詳細(xì)講述了自定義類屬性的原理及實(shí)現(xiàn)方法,需要的朋友可以參考下
    2014-10-10
  • ASP.NET?MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)

    ASP.NET?MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)

    這篇文章主要介紹了ASP.NET?MVC5?網(wǎng)站開發(fā)中用戶登錄、注銷的實(shí)現(xiàn)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2015-09-09
  • .Net?Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟

    .Net?Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟

    最近在使用調(diào)度程序創(chuàng)建簡(jiǎn)單的服務(wù),該服務(wù)將執(zhí)行一些重復(fù)的IO操作,使用的是Coravel調(diào)度庫(kù),下面這篇文章主要給大家介紹了關(guān)于.Net?Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟,需要的朋友可以參考下
    2022-08-08
  • ASP.NET Core中使用默認(rèn)MVC路由的配置

    ASP.NET Core中使用默認(rèn)MVC路由的配置

    這篇文章主要介紹了ASP.NET Core中使用默認(rèn)MVC路由的配置,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • 12小時(shí)制和24小時(shí)制獲取當(dāng)天零點(diǎn)的問(wèn)題探討

    12小時(shí)制和24小時(shí)制獲取當(dāng)天零點(diǎn)的問(wèn)題探討

    這篇文章介紹了12小時(shí)制和24小時(shí)制獲取當(dāng)天零點(diǎn)的問(wèn)題探討,有需要的朋友可以參考一下
    2013-09-09

最新評(píng)論