用ASP.NET完成容易的文字水印
發(fā)表時(shí)間:2024-01-25 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]代碼如下:<%@ Import Namespace="System" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Drawin...
代碼如下:
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Page language="vb" %>
<script runat="server">
Dim FilePath As String = Server.MapPath("FengEr.jpg")
Sub Page_Load(Sender As Object, E As EventArgs)
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile( FilePath )
Dim g As Graphics = Graphics.FromImage(image)
g.DrawImage(image, 0, 0, image.Width, image.Height)
Dim f As Font = new Font("華文行楷", 30)
Dim b As Brush = new SolidBrush(Color.Green)
Dim s As String = Request.QueryString("Str")
g.DrawString(s, f, b, 20, 290)
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
g.Dispose()
image.Dispose()
End Sub
</script>
只要把這個(gè)代碼存成一個(gè)aspx文件,比如Test.aspx。然后放到wwwroot里面(假設(shè)你的虛擬目錄是默認(rèn)的)。再做一個(gè)Test.jpg的圖片,就可以在(20, 290)這個(gè)位置打印出“華文行楷”這種字體的文字了。調(diào)用方法很簡(jiǎn)單:
http://localhost/Test.aspx?Str=Dicky's Blog!
對(duì)于打印的位置和字體還有圖片文件都是可以自己設(shè)定的。另外,如果出現(xiàn)了以英文作為參數(shù)就可以正常顯示,而對(duì)于中文就無(wú)法顯示的情況,是因?yàn)锳sp.net的web.config設(shè)置不正確造成了,需要進(jìn)行如下設(shè)置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" fileEncoding="gb2312"/>
</system.web>
</configuration>
這樣,就可以正常顯示了。