An implementation method of WebForm image watermark and image Verification code for ASP.

Source: Internet
Author: User
Both need to introduce namespaces: using System.Drawing;

First, the picture watermark

Front desk photoshuiyin.aspx Code:

<div><asp:fileupload id= "FileUpload1" runat= "Server"/><asp:button id= "Button1" runat= "Server" text= " Upload "/><br/><asp:image id=" Image1 "runat=" Server "/></div>

Background Photoshuiyin.aspx.cs Code:

protected void Page_Load (object sender, EventArgs e) {Button1.Click + = button1_click;} void Button1_Click (object sender, EventArgs e) {///1, making canvas System.Drawing.Image img = System.Drawing.Image.FromStream ( Fileupload1.filecontent); Graphics g = Graphics.fromimage (IMG);//Watermark Style: Draw something string a = "http://www.itnba.com";//font, size font f = new Font ("Blackbody", 30);//Yan Color Brush B = New SolidBrush (color.red);//0,0--begin to draw the position of the watermark G. DrawString (A, F, B, 0, 0);//save path of String path = "images/" + fileupload1.filename;img. Save (path);//Show Image1.imageurl = path in the image control, Server.MapPath}

Effect Show:

Second, the Image verification code

Front desk photoyanzhengma.aspx Code:

<form id= "Form1" runat= "Server" ><div> User name: <asp:textbox id= "TextBox1" runat= "Server" ></asp: textbox><br/> Password: <asp:textbox id= "TextBox2" runat= "server" ></asp:textbox><br/> Verification Code:< Asp:textbox id= "TextBox3" runat= "Server" ></asp:textbox><asp:image id= "Image1" runat= "Server" Imageurl= " Yzm.aspx "/><asp:label id=" Label1 "runat=" Server "text=" Label "></asp:label><br/><asp: Button id= "Button1" runat= "Server" text= "button"/></div></form></body>

protected void Page_Load (object sender, EventArgs e) {Random r = new random (); string aaa = "Abcdefghijklmnopqrstuvwxyzabcde FGHIJKLMNOPQRSTUVWXYZ1234567890 ";//Generate canvas Bitmap img = new Bitmap (80, 30);//canvas background color generic combination list<color> Clist = new List <Color> (); Clist.add (Color.yellow); Clist.add (Color.green); Clist.add (Color.Blue); Clist.add (Color.aqua); Clist.add (Color.orange); Clist.add (Color.pink); Graphics g = Graphics.fromimage (img); G.fillrectangle (New SolidBrush (Clist[r.next (0, Clist.count)]), 0, 0, 80, 30);// Randomly generated verification code combination string str = ""; for (int i = 0; I < 4; i++) {str + = AAA. Substring (R.next (0, AAA). Length), 1);} session["YZM"] = str; Font f = new Font ("Blackbody", 20); Brush B = new SolidBrush (color.red);//Generate G. DrawString (str, F, B, 10, 0);//Add interference line for (int i = 0; i < R.next (6); i++) {Brush bb = new SolidBrush (clist[r.next (0, C List. Count)]); Pen p = new Pen (BB, 1), G.drawline (p, r.next (0, N), R.next (0, +), R.next (0, N), R.next (0, 30));} Save complete IMG. Save (Response.outputstream, System.Drawing.Imaging.IMAGEFORMAT.JPEG); Response.End ();}

Effect Show:

  • Related Article

    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.