Example of adding a text watermark to an image using Asp.net

Source: Internet
Author: User

Use Asp.net to implement text watermarks. Here is a simple method. Although it is simple, it is very practical and easy to understand and modify.

 

The code is as follows: Copy code

<% @ Import Namespace = "System" %>
<% @ Import Namespace = "System. IO" %>
<% @ Import Namespace = "System. Drawing" %>

<% @ Page language = "vb" %>

<Script runat = "server">
Dim FilePath As String = Server. MapPath ("Talk. 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 (" 文 ", 40)
Dim B As Brush = new SolidBrush (Color. Black)
Dim s As String = Left (Request. QueryString ("str"), 7)
G. DrawString (s, f, B, 20,190)
Image. Save (Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg)
G. Dispose ()
Image. Dispose ()
End Sub
</Script>

 

You only need to save this code as an aspx file, such as Test. aspx. Put it in wwwroot (assuming your virtual directory is default ). Make another image of Talk. JPG and print the text in the font "文" in (20,190. The call method is simple:

Http: // localhost/Test. aspx? Str = test failed!

You can set the printing position, font, and image files. In addition, if e is used as the parameter, it can be displayed normally, but Chinese cannot be displayed, because Asp.net web. if the config settings are incorrect, the following settings are required:

 

The code is as follows: Copy code
<? 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.