本文實例講述了C#給圖片加水印的簡單實現方法。分享給大家供大家參考。具體分析如下:
這里實現本網站圖片保護功能類:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Drawing;//image的命名空間namespace 實現本網站圖片保護功能{ public class yanzhengma:IHttpHandler { public bool IsReusable { get { throw new NotImplementedException(); } } public void ProcessRequest(HttpContext context) //請求的方法 { Image img = Image.FromFile(context.Request.PhysicalPath); //1:從文件中獲取圖片;獲取請求的文件的物理路徑 Graphics g = Graphics.FromImage(img); //2:聲明graphicse把img做為填充他的參數 g.DrawString("net",new Font("宋體",20,FontStyle.Italic),Brushes.Blue,10,10); //3:在graphicse上寫圖片 img.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg); //4:保存(保存到什么什么流里,什么格式保存) context.Response.Flush(); //5:從緩存區中輸出 context.Response.End(); //6:結束 //7:配置 } }}
webform1.aspx:
<div><%-- <asp:GridView ID="GridView1" runat="server"></asp:GridView><asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>--%><asp:Image ID="Image1" runat="server" imageurl="imgs/1.jpg"/><%--<image src="http://localhost:2309/WebForm1.aspx"></image>--%></div>
配置:
<httpHandlers><add verb="*" path="imgs/*.jpg" type="實現本網站圖片保護功能.yanzhengma"/><!--第一個屬性verb是處理什么樣的文件,path是處理那個文件夾下的圖片,type是要配置的文件類--></httpHandlers>
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答