Webform (10)-image watermark, image verification code, and webform watermark

Source: Internet
Author: User

Webform (10)-image watermark, image verification code, and webform watermark

Both of them need to introduce the namespace: using System. Drawing;

I. Image Watermark

Foreground 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 + = button#click;} void button#click (object sender, EventArgs e) {// 1. Create the 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 ("", 30 ); // Color Brush B = new SolidBrush (Color. red); // 0, 0 -- start to draw the watermark position g. drawString (a, f, B, 0, 0); // save path string path = "images/" + FileUpload1.FileName; img. save (Server. mapPath (path); // display Image1.ImageUrl = path in the image control ;}

Effect display:

 

 

 

Ii. Image Verification Code

Foreground Photoyanzhengma. aspx code:

<Form id = "form1" runat = "server"> <div> Username: <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 = "se Rver "Text =" Button "/> </div> </form> </body> 

Protected void Page_Load (object sender, EventArgs e) {Random r = new Random (); string aaa = "Random"; // generate the 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 generate the displayed 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 ("", 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, 20); I ++) {Brush bb = new SolidBrush (Clist [r. next (0, Clist. count)]); Pen p = new Pen (bb, 1); g. drawLine (p, r. next (0, 80), r. next (0, 30), r. next (0, 80), r. next (0, 30);} // save img. save (Response. outputStream, System. drawing. imaging. imageFormat. jpeg); Response. end ();}

Effect display:

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.