好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > ASP.NET 圖片加水印防盜鏈實現代碼

ASP.NET 圖片加水印防盜鏈實現代碼

熱門標簽:浙江外呼系統怎么安裝 陜西人工外呼系統哪家好 地圖標注多個行程 銅川小型外呼系統運營商 海外地圖標注門市標 云南外呼電銷機器人系統 廈門商鋪地圖標注 山西防封卡電銷卡套餐 上海楊浦怎么申請申請400電話
首先建一個類:
復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
/// summary>
///Class1 的摘要說明
/// /summary>
public class Class1:IHttpHandler //調用接口
{
public Class1()
{
//
//TODO: 在此處添加構造函數邏輯
//
}
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("三國演義", 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中注冊接口:
復制代碼 代碼如下:

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
復制代碼 代碼如下:

system.web>
httpHandlers>
remove verb="*" path="*.asmx"/>
!--解決圖片防盜鏈問題-->
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.添加一個一般執行文件Watermark.ashx,代碼如下:
復制代碼 代碼如下:

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)
{
//設置客戶端緩沖時間過期時間為0,即立即過期
//context.Response.Expires = 0;
//清空服務器端為此會話開啟的輸出緩存
//context.Response.Clear();
//設置輸出文件類型
context.Response.ContentType = "image/jpg";
//將請求文件寫入到輸出緩存中
#region 獲取XML配置信息
DataSet dsConfing = new DataSet();
string watermarkConfigPath = context.Server.MapPath("~/Config/WaterMarkConfig.xml");
if (System.IO.File.Exists(watermarkConfigPath))
dsConfing.ReadXml(watermarkConfigPath);
else
{
//添加默認的水印配置
}
DataRow drConfing = dsConfing.Tables[0].Rows[0];
#endregion
string currentHost = drConfing["allowhost"].ToString();
//判斷是否是本地網站引用圖片,如果是則返回正確的圖片
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();
}
//將請求文件寫入到輸出緩存中
imgStart.Save(context.Response.OutputStream, ImageFormat.Jpeg);
imgStart.Dispose();
#endregion
}
else
{
#region 輸出原圖
//將請求文件寫入到輸出緩存中
context.Response.WriteFile(context.Request.Url.AbsolutePath);
#endregion
}
}
//如果不是本地引用,則是盜鏈本站圖片
else
{
//將請求文件寫入到輸出緩存中
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">文字長度/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,內容如下:
復制代碼 代碼如下:

?xml version="1.0" encoding="utf-8" ?>
watermark>
allowhost>localhost:6219/allowhost>!--允許訪問的域名-->
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, 中間有直線通過的文本 8-->
font-size>60/font-size>
font-color>red/font-color>
font-content>¥:8000元/font-content>
errimgpath>images/error.jpg/errimgpath>!-- 盜圖片的請求返回的跟目錄下的某圖片 -->
/watermark>
您可能感興趣的文章:
  • 如何在ASP.NET Core中給上傳圖片功能添加水印實例代碼
  • ASP.NET百度Ueditor編輯器實現上傳圖片添加水印效果
  • Asp.net開發之webform圖片水印和圖片驗證碼的實現方法
  • asp.net繼承IHttpHandler接口實現給網站圖片添加水印功能實例
  • ASP.NET簡單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
  • Asp.net簡單實現給圖片增加文字水印
  • asp.net如何在圖片上加水印文字具體實現
  • asp.net上傳圖片并作處理水印與縮略圖的實例代碼
  • asp.net中上傳圖片文件實現防偽圖片水印并寫入數據庫
  • ASP.NET實現圖片自動添加水印

標簽:信陽 許昌 常州 自貢 孝感 西雙版納 朔州 萊蕪

巨人網絡通訊聲明:本文標題《ASP.NET 圖片加水印防盜鏈實現代碼》,本文關鍵詞  ASP.NET,圖片,加,水印,防盜,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《ASP.NET 圖片加水印防盜鏈實現代碼》相關的同類信息!
  • 本頁收集關于ASP.NET 圖片加水印防盜鏈實現代碼的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 超级黄且详细床戏的小说| 毛片免费在线播放| 日韩高清一区二区三区不卡| 野外一男一女一级毛片| chinese乱子伦xxxx国语对白| 国产精品亚洲欧美日韩一区在线| 快穿肉欲娇喘高HH1v1| 菠萝菠萝蜜在线观看3| 青青成人福利国产在线视频| 超级乱淫伦小说一女多男| 国产欧美一区二区精品性色超碰| 真实男女xx00动态图视频| 保定市| 亚洲一区免费视频| 特级太黄久久A片免费播放一网站| 色天使色偷偷色噜噜最新版本下载| 久久久99精品免费观看精品| 国内精品免费久久影院| 乖让我尿到里面H| 想看一级毛片| 人禽性xxxbbb欧美| 91麻豆精品一区二区三区| 免费观看很黄很色的大片| 播放毛片| 国产精品亚洲一区二区三区二| 99久久国产精品免费免费| 理论片一区| 黑人巨大精品欧美一区二区区| 被老师摁着强进了好爽H漫画| 极品J撕破丝袜自慰喷水AV| 被精夜浸濡的小柔| 啊…学长我们换个地方| 玩物h| 欲求不满人妻中文系列| 萌白酱白色情趣毛衣在线| 特黄特黄aaaa级毛片免费看| 韩国成人毛片aaa黄| JapaneseⅩⅩⅩHD18| 91中文乱幕日产无线码区| 欧美狂野成人AV在线千千视频| 香蕉69精品视频在线观看|