** 代码如下: **
1@ Import Namespace="System"
1@ Import Namespace="System.IO"
1@ Import Namespace="System.Drawing"
1@ Page language="vb"
1<script runat="server">
2Dim FilePath As String = Server.MapPath("FengEr.jpg")
3
4Sub Page_Load(Sender As Object, E As EventArgs)
5Dim image As System.Drawing.Image = System.Drawing.Image.FromFile( FilePath )
6Dim g As Graphics = Graphics.FromImage(image)
7g.DrawImage(image, 0, 0, image.Width, image.Height)
8Dim f As Font = new Font("华文行楷", 30)
9Dim b As Brush = new SolidBrush(Color.Green)
10Dim s As String = Request.QueryString("Str")
11g.DrawString(s, f, b, 20, 290)
12image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
13g.Dispose()
14image.Dispose()
15End Sub
16</script>
只要把这个代码存成一个aspx文件,比如Test.aspx。然后放到wwwroot里面(假设你的虚拟目录是默认的)。再做一个Test.jpg的图片,就可以在(20, 290)这个位置打印出“华文行楷”这种字体的文字了。调用方法很简单:
http://localhost/Test.aspx?Str=Dicky's Blog!
对于打印的位置和字体还有图片文件都是可以自己设定的。另外,如果出现了以英文作为参数就可以正常显示,而对于中文就无法显示的情况,是因为Asp.net的web.config设置不正确造成了,需要进行如下设置:
1<configuration>
2<system.web>
3<globalization culture="zh-CN" fileencoding="gb2312" requestencoding="gb2312" responseencoding="gb2312"></globalization>
4</system.web>
5</configuration>
这样,就可以正常显示了。