Using asp.net to realize simple text watermark

Source: Internet
Author: User

Often see someone on the mop of that dynamic picture, is a string as parameters to a Dynamic Web page, you will generate a picture with this string, called a text watermark. Like what the original Panda series, as well as the later trees and gold bars, are very interesting. This thing looks very fun, want to do a play. Check the data, found that using. NET to do this is very easy, with ASP.net wrote a very simple. All 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 ("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 ("XXFarEastFont-Xingkai")
Dim b As Brush = new SolidBrush (color.black)
Dim s As String = Left (Request.QueryString ("str"), 7)
g.drawstring (S, F, B, 190)
image. Save (Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg)
g.dispose ()
image. Dispose () End
Sub
</script>      

Just save this code as an ASPX file, such as Test.aspx. Then put it in the wwwroot (assuming your virtual directory is the default). To do a talk.jpg picture, you can print the text of the font "XXFarEastFont-Xingkai" in this place (20, 190). Calling the method is simple:

Http://localhost/Test.aspx?str= Test the next!

For print locations and fonts and picture files can be set themselves. In addition, if the appearance of e-text as a parameter can be normal display, and the Chinese will not be able to display the situation, because the asp.net web.config settings are incorrect, you need to do the following settings:

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<system.web>
< Globalization requestencoding= "gb2312" responseencoding= "gb2312" culture= "ZH-CN" 

fileencoding= "gb2312"/>
</system.web>
</configuration>

This way, you can display it normally. It's fun to play.

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.