ASP.NET to achieve a simple text watermark

  • 2020-05-05 11:09:35
  • OfStack

The code is as follows :

< %@ 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 Font = new Font(" Chinese script ", 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 >


Just save the code as an aspx file, such as Test.aspx. Then put it in wwwroot (assuming your virtual directory is the default). If you make another picture of Test.jpg, you can print "Chinese characters" in the position of (20, 290). Calling the method is simple:

http: / / localhost Test aspx? Str = Dicky 's Blog!

The printing position and font as well as the image file can be set. In addition, if the English parameter is used as the parameter, it can be displayed normally, but the Chinese parameter cannot be displayed, because the web.config setting of Asp.net is not correct, the following setting is needed:

< ?xml version="1.0" encoding="utf-8"? >
< configuration >
      < system.web >
      < globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" fileEncoding="gb2312"/ >
      < /system.web >
< /configuration >

In this way, it can be displayed normally.


Related articles: