萬盛學電腦網

 萬盛學電腦網 >> 網頁制作 >> Html5 >> 實現自己網站的圖片假水印功能

實現自己網站的圖片假水印功能

 --------Web.config----------------
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpHandlers>
<add verb="*" path="images/*.jpg" type="模塊與處理程序.ImageDeal"/>
</httpHandlers>
</system.web>
</configuration>
-------------ImageDeal.cs類-------------------------
namespace 模塊與處理程序
{
public class ImageDeal:IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
Image img = Image.FromFile(context.Request.PhysicalPath);
Graphics g = Graphics.FromImage(img);
g.DrawString("QQ", new Font("宋體", 20, FontStyle.Bold), Brushes.Red, 10, 10);
img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.Flush();
context.Response.End();
}
}
}
----------default2.aspx前台------------------------
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="sid" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="sid" HeaderText="sid" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" />
<asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
<asp:ImageField DataImageUrlField="photo"
DataImageUrlFormatString="~/images/{0}" HeaderText="照片">
</asp:ImageField>
<asp:HyperLinkField DataNavigateUrlFields="sid,sname"
DataNavigateUrlFormatString="{0}/{1}/viewDetail.html" HeaderText="操作"
Text="查看詳情" />
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
SelectCommand="SELECT [sid], [sname], [sex], [age], [photo] FROM [student]">
</asp:SqlDataSource>

</div>
</form>
</body>
-------------default3前台-----------------------
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" ImageUrl="http://localhost:1695/images/liubei.jpg" runat="server" />
</div>
</form>
</body>

copyright © 萬盛學電腦網 all rights reserved