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:配置
}
}
}
</pre> 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></pre>
本文由用戶 enpc 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!