用ASP.NETt實現簡單的文字水印
2024-07-10 12:56:25
供稿:網友
 
代碼如下:
<%@ 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>
只要把這個代碼存成一個aspx文件,比如test.aspx。然后放到wwwroot里面(假設你的虛擬目錄是默認的)。再做一個test.jpg的圖片,就可以在(20, 290)這個位置打印出“華文行楷”這種字體的文字了。調用方法很簡單:
http://localhost/test.aspx?str=dicky's blog!
對于打印的位置和字體還有圖片文件都是可以自己設定的。另外,如果出現了以英文作為參數就可以正常顯示,而對于中文就無法顯示的情況,是因為asp.net的web.config設置不正確造成了,需要進行如下設置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.web>
 <globalization requestencoding="gb2312" responseencoding="gb2312" culture="zh-cn" fileencoding="gb2312"/>
 </system.web>
</configuration>
這樣,就可以正常顯示了。