您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“asp.net繼承IHttpHandler接口如何實現(xiàn)給網(wǎng)站圖片添加水印功能”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“asp.net繼承IHttpHandler接口如何實現(xiàn)給網(wǎng)站圖片添加水印功能”這篇文章吧。
先展示圖片效果:
1. 在App_Code下添加類文件,命名為ImageSY 文件內(nèi)容如下
public class ImageSY : IHttpHandler { public ImageSY() { // //TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } #region IHttpHandler 成員 public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { //獲得請求的物理圖片路徑 string imagePath = context.Request.PhysicalPath; System.Drawing.Image image = null; if (File.Exists(imagePath)) { //定義水印文字 string text = "本圖片來至我的網(wǎng)站"; //定義水印文字字體大小 int fontSize = 22; //水印文字字體 Font font = new Font("宋體", fontSize); //根據(jù)圖片物理地址加載圖片 image = System.Drawing.Image.FromFile(imagePath); Graphics g = Graphics.FromImage(image); //獲取要繪制水印文字所需要的顯示區(qū)域大小 SizeF size = g.MeasureString(text, font); if (size.Width > image.Width || size.Height > image.Height) { } else { Brush brush = Brushes.Red; g.DrawString(text, font, brush, image.Width - size.Width, image.Height - size.Height); g.Dispose(); } } else { } image.Save(context.Response.OutputStream, ImageFormat.Jpeg); } #endregion }
2. 配置WebConfig,添加Location新節(jié)點
<location path="images"> <system.web> <httpHandlers> <!---對jpg文件添加水印--> <add verb="*" type="ImageSY" path="*.jpg"/> <add verb="*" type="ImageSY" path="*.gif"/> <add verb="*" type="ImageSY" path="*.bmp"/> </httpHandlers> </system.web> </location>
3. 測試,新建aspx頁面,顯示圖片,水印就會自動加上了
以上是“asp.net繼承IHttpHandler接口如何實現(xiàn)給網(wǎng)站圖片添加水印功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。