Random verification code,

Source: Internet
Author: User

Random verification code,

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;
Using System. Windows. Forms;

Namespace GDI _
{
Public partial class: Form
{
Public letter ()
{
InitializeComponent ();
}
// Implement Random verification codes in WinForm
Private void picturebox#click (object sender, EventArgs e)
{
// Define the canvas size
Bitmap bmp = new Bitmap (100,100 );
// Define a variable
String str = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
Random r = new Random (); // randomly generated
String newstr = "";
For (int I = 0; I <6; I ++)
{
Newstr + = str [r. Next (0, 62)];
}
Graphics g = Graphics. FromImage (bmp );
// Random font
String [] fname = {"", ""};
// Random color

Color [] color = {Color. Red, Color. Green, Color. Pink, Color. Yellow, Color. Gold, Color. Black };
For (int I = 0; I <newstr. Length; I ++)
{
Point p = new Point (I * 15, 20 );
G. drawString (newstr [I]. toString (), new Font (fname [r. next (0, 6)], 18, FontStyle. italic), new SolidBrush (color [r. next (0, 6)]), p );
}
// The background noise line of the picture on the Verification Code
For (int I = 0; I <15; I ++)
{
G. drawLine (new Pen (color [r. next (0, 6)]), new Point (r. next (1, 0,100), r. next (0,100), new Point (r. next (1, 0,100), r. next (0,100 )));
}
// The background noise of the image on the Verification Code
For (int I = 0; I <300; I ++)
{
Bmp. SetPixel (r. Next (0,100), r. Next (0,100), color [r. Next (0, 6)]);
}
This. pictureBox1.Image = bmp;
}

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.