C # generate a graphic Verification Code

Source: Internet
Author: User

From http://www.cnblogs.com/tuyile006/archive/2007/04/13/711874.html

Source code for generating the verification code image:

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. drawing;
Using system. IO;

Public partial class creatimage: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String checkcode = createcode (4 );
Session ["checkcode"] = checkcode;
Createimage (checkcode );
}

/* Generate a Verification Code */
Public String createcode (INT codelength)
{

String so = "1, 2, 3, 4, 5, 6, 7, 8, 9, 0, A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P, Q, R, S, T, U, V, W, X, Y, Z, A, B, C, D, E, F, G, h, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z ";
String [] strarr = So. Split (',');
String code = "";
Random Rand = new random ();
For (INT I = 0; I <codelength; I ++)
{
Code + = strarr [Rand. Next (0, strarr. Length)];
}
Return code;
}

/* Generate a verification image */
Public void createimage (string code)
{

Bitmap image = new Bitmap (60, 20 );
Graphics G = graphics. fromimage (image );
Webcolorconverter WW = new webcolorconverter ();
G. Clear (color) ww. convertfromstring ("# fae264 "));

Random random = new random ();
// Draw the background noise line of the image
For (INT I = 0; I <12; I ++)
{
Int X1 = random. Next (image. width );
Int X2 = random. Next (image. width );
Int Y1 = random. Next (image. Height );
Int y2 = random. Next (image. Height );

G. drawline (new pen (color. lightgray), X1, Y1, X2, Y2 );
}
Font font = new font ("Arial", 15, fontstyle. Bold | fontstyle. italic );
System. Drawing. drawing2d. lineargradientbrush brush = new system. Drawing. drawing2d. lineargradientbrush (
New rectangle (0, 0, image. Width, image. Height), color. Blue, color. Gray, 1.2f, true );
G. drawstring (Code, Font, brush, 0, 0 );

// Foreground noise of the image
// For (INT I = 0; I <10; I ++)
//{
// Int x = random. Next (image. width );
// Int y = random. Next (image. Height );
// Image. setpixel (X, Y, color. White );
//}

// Draw the border line of the image
// G. drawrectangle (new pen (color. Silver), 0, 0, image. Width-1, image. Height-1 );

System. Io. memorystream MS = new system. Io. memorystream ();
Image. Save (MS, system. Drawing. imaging. imageformat. GIF );
Response. clearcontent ();
Response. contenttype = "image/GIF ";
Response. binarywrite (Ms. toarray ());
G. Dispose ();
Image. Dispose ();
}
}

Usage:
Save the above Code in the webpage: createimg. aspx

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.