在 ASP.NET 網頁上加入浮水印
通常在Confidential的頁面上會顯示浮水印,這樣當User在拍下畫面時,至少知道是誰在何時外流的...
首先撰寫產出圖型的 WaterMark.ashx,欲顯示的文字內容可視情況調整...
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<%@ WebHandler Language="C#" Class="WaterMark" %>
using System; using System.Web; public class WaterMark : IHttpHandler { public void ProcessRequest (HttpContext context) { byte[] myPic = null; string id = context.Request["id"]; myPic = createImg(id, 22, 20, 300, 180); context.Response.ContentType = "image/png"; context.Response.OutputStream.Write(myPic, 0, myPic.Length); } public bool IsReusable { get { return true; } } private byte[] createImg(string strWaterMark, float f1, float f2, int w, int h) { Bitmap newBitmap = null; Graphics g = null; MemoryStream ms = new MemoryStream(); try { Font fontCounter = new Font("Lucida Sans Unicode", f1); Font fontCounter2 = new Font("Lucida Sans Unicode", f2); // calculate size of the string. newBitmap = new Bitmap(1, 1, PixelFormat.Format32bppArgb); g = Graphics.FromImage(newBitmap); SizeF stringSize = g.MeasureString(strWaterMark, fontCounter); int nWidth = (int)stringSize.Width; int nHeight = (int)stringSize.Height; g.Dispose(); newBitmap.Dispose(); newBitmap = new Bitmap(w, h, PixelFormat.Format32bppArgb); g = Graphics.FromImage(newBitmap); g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, w, h)); g.RotateTransform(23.0F); g.DrawString("Rexchip Confidential", fontCounter2, new SolidBrush(Color.Black), 40, 0 - 20); g.DrawString("Printed by " + strWaterMark, fontCounter, new SolidBrush(Color.Black), 40, nHeight * 0.5f - 20); g.DrawString(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), fontCounter2, new SolidBrush(Color.Black), 40, nHeight * 1.2f - 20); newBitmap.Save(ms, ImageFormat.Png); } catch (Exception e) {} finally { if (null != g) g.Dispose(); if (null != newBitmap) newBitmap.Dispose(); } return ms.ToArray(); } } |
然後到要顯示浮水印的頁面增加一個 div 及 img html控制項,div 用來塞浮水印 image,div 視情況調整top及left位置...
|
1
2 |
<div id="bgMark" class="bgMark" style="background-color:transparent;width:600px;height:480px; position: relative;top:-495px;left:160px;"> </div>
<img id="imgWaterMark" style="DISPLAY: none; WIDTH: 600px; " runat="server"> |
class="bgMark" 可設定浮水印的濃度,CSS如下:
.bgMark
{
filter:alpha(opacity=8);
-moz-opacity:0.08;
opacity: 0.08;
}
再來就是要產生浮水印,並指定到 div了,後端程式如下:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
protected void Page_Load(object sender, EventArgs e)
{ if (!this.IsPostBack) { //浮水印背景設定 string script = "document.getElementById('bgMark').style.background = \"url(\" + document.getElementById('" + imgWaterMark.ClientID + "').src + \") 0 20 repeat \";"; if (ScriptManager.GetCurrent(this.Page) == null) Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "block", script, true); else ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "block", script, true); //浮水印圖 string ID = getUserID(); imgWaterMark.Src = ResolveUrl("~") + string.Format("WaterMark.ashx?id={0}", ID); } } |
浙公网安备 33010602011771号