asp.net development of WebForm image watermarking and image Verification code implementation Method _ Practical skills

Source: Internet
Author: User

Both require the introduction of namespaces: Using System.Drawing;

First, picture watermark

Front photoshuiyin.aspx Code:

<div>
<asp:fileupload id= "FileUpload1" runat= "Server"/> <asp:button "id=" "Button1"
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 ("bold");
Color
Brush b = new SolidBrush (color.red);
0,0--begins to paint the location of the watermark
g.drawstring (A, F, B, 0, 0);
Save path
String path = "images/" + fileupload1.filename;
Img. Save (Server.MapPath (path));
Show Image1.imageurl = path in the image control
;

Effect Display:

Second, picture Verification code

Front 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/>
Authentication 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 = "Abcdefghijklmnopqrstuvwxyza
BCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ";
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 display of the validation 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 ("bold", 20);
Brush B = new SolidBrush (color.red);
Generate g.DrawString (str, F, B, 10, 0);
Add a noise line for (int i = 0; i < R.next (6); i++) {Brush bb = new SolidBrush (clist[r.next (0, Clist.count)]);
Pen p = new Pen (BB, 1);
G.drawline (P, r.next (0), R.next (0), R.next (0), R.next (0, 30)); }//Save complete IMG. Save (RESPONSE.OUtputstream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.End (); }

Effect Display:

The above is a small set to introduce the ASP.net development of the WebForm image watermark and picture verification code to achieve the method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.