日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HttpHandler:给指定路径下的图片添加水印显示

發布時間:2023/12/19 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HttpHandler:给指定路径下的图片添加水印显示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
圣誕節,25日,要交ACCP5.0認證的項目,其中有這樣一個要求:書店的所有圖書的封面放在了\images\convers\下面,要求所有引用這一路徑下的圖片都添加書店的店名水印圖片。就是說攔截Http請求了,自然想到HttpHandler可以辦到。考慮下,實現的效果應該是這樣的:為了通用,監視的路徑,水印圖片路徑,默認圖片路徑3者應該在配置文件里面設定,方便修改;監視路徑下的所有圖片只要物理存在都要有水印,物理不存在用默認圖片替代,若水印圖片不存在用文字代替。訪問其他路徑下的圖片應該正常顯示沒有水印....廢話不多說,實現的代碼如下:為方便調試,編譯httpHandler類的時候要加調試選項,并在項目中引用這個dll(編譯:csc /t:library WatermarkHandler.cs /debug)
using?System;
using?System.Data;
using?System.Configuration;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.HtmlControls;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;

namespace?xumh
{
????
/**////?<summary>
????
///?圖片處理模塊:給指定路徑下的圖片添加水印輸出
????
///?1.監視的目錄路徑,水印圖片路徑,默認圖片三者都在web.config文件中設定
????
///?2.請求的圖片的路徑不屬于監視路徑,直接輸出圖片
????
///?3.請求的圖片的路徑屬于監視路徑,若圖片不存在,顯示默認圖片,不加水印
????
///?4.請求的圖片的路徑屬于監視路徑,且文件存在,添加水印
????
///????????水印圖片存在:圖片+水印圖片,輸出
????
///????????水印圖片不存在:圖片+水印文字,輸出
????
///????????
????
///?5.web.config部分如下:
????
///?????????<appSettings>
????
///??????????????????<!--要監視的路徑-->
????
///?????????????????????<add?key="monitorPath"?value="/images/bookcovers/"/>
????
///?????????????????<!--圖書封面不存在時顯示的默認封面圖片的相對路徑-->
????
///????????????????????<add?key="defaultImage"?value="images\default.jpg"/>
????
///?????????????????<!--相對于物理應用程序的根路徑的水印圖片的相對路徑-->
????
///??????????????????????<add?key="watermarkImage"?value="images\WaterMark.jpg"/>
????
///????????</appSettings>
????
///?????
????
///?????????<httpHandlers>
????
///????????????????<add?verb="*"?path="*.jpg"?type="xumh.WatermarkHandler,WatermarkHandler"/>
????
///????????????</httpHandlers>
????
///???????許明會??2007-12-23?18:00?Visual?Studio?2005?調試通過
????
///?</summary>

????public?class?WatermarkHandler:IHttpHandler
????
{
????????
public?bool?IsReusable
????????
{
????????????
get?{?return?true;?}
????????}


????????
public?void?ProcessRequest(HttpContext?context)
????????
{
????????????
string?url?=?context.Request.Url.AbsoluteUri.ToLower();
????????????
string?monitorPath?=?System.Configuration.ConfigurationManager.AppSettings["monitorPath"];
????????????
//若圖片不屬于封面,直接輸出圖片:若圖片不存在不做處理
????????????bool?IsInterestUrl?=?url.Contains(monitorPath);
????????????System.Drawing.Image?imgSource?
=?null;
????????????
if?(!IsInterestUrl)??
????????????
{
????????????????
if?(!System.IO.File.Exists(context.Request.PhysicalPath))
????????????????????
return;
????????????????imgSource?
=?System.Drawing.Image.FromFile(context.Request.PhysicalPath);
????????????????imgSource.Save(context.Response.OutputStream,?imgSource.RawFormat);
????????????????imgSource.Dispose();
????????????????
return;
????????????}
?
????????????
//圖片屬于封面,但圖片imageSource不存在,顯示默認圖片default.jpg
????????????string?physicalPath?=?context.Request.PhysicalPath;//?context.Server.MapPath(url);
????????????if?(!System.IO.File.Exists(physicalPath))
????????????
{
????????????????
string?defaultImage?=?System.Configuration.ConfigurationManager.AppSettings["defaultImage"];
????????????????imgSource?
=?System.Drawing.Image.FromFile(context.Request.PhysicalApplicationPath?+?defaultImage);
????????????????imgSource.Save(context.Response.OutputStream,?imgSource.RawFormat);
????????????????imgSource.Dispose();
????????????????
return;
????????????}

????????????
//--===--------------------只要有封面圖片,就為圖片添加水印----------------------===---
????????????string?watermarkImage?=?System.Configuration.ConfigurationManager.AppSettings["watermarkImage"];
????????????
string?watermarkImagePath?=?context.Request.PhysicalApplicationPath?+?watermarkImage;
????????????
bool?bWatermarkImageExist?=System.IO.File.Exists(watermarkImagePath);
????????????imgSource?
=?System.Drawing.Image.FromFile(physicalPath);
????????????System.Drawing.Graphics?graphic?
=?System.Drawing.Graphics.FromImage(imgSource);
????????????
if?(bWatermarkImageExist)?//“水印圖片”存在:存在則封面混合水印圖片
????????????{
????????????????System.Drawing.Image?imgWatermark?
=?System.Drawing.Image.FromFile(watermarkImagePath);
????????????????graphic.DrawImage(imgWatermark,
????????????????????imgSource.Width?
-?imgWatermark.Width,?imgSource.Height?-?imgWatermark.Height,
????????????????????imgWatermark.Width,?imgWatermark.Height);
????????????????
//把圖片保存到輸出流
????????????????imgSource.Save(context.Response.OutputStream,?System.Drawing.Imaging.ImageFormat.Jpeg);
????????????????imgWatermark.Dispose();
????????????}

????????????
else???????????????????????????????//“水印圖片”不存在:輸出文本
????????????{
????????????????System.Drawing.Font?font?
=?new?System.Drawing.Font("幼圓",?13.0f,System.Drawing.FontStyle.Bold);
????????????????graphic.DrawString(
"第三波+書店",font?,?System.Drawing.Brushes.Red,?new?System.Drawing.PointF());
????????????????imgSource.Save(context.Response.OutputStream,?System.Drawing.Imaging.ImageFormat.Jpeg);
????????????}

????????????imgSource.Dispose();
????????????graphic.Dispose();
????????????
//標明類型為jpg:如果不標注,IE沒有問題,但Firefox會出現亂碼
????????????context.Response.ContentType?=?"image/jpeg";
????????????context.Response.Flush();
????????????context.Response.End();
????????}

????}

}



轉載于:https://www.cnblogs.com/flaaash/archive/2007/12/23/1011640.html

總結

以上是生活随笔為你收集整理的HttpHandler:给指定路径下的图片添加水印显示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。