ASP. net c # The Verification Code supports Chinese noise bending.

Source: Internet
Author: User
/// <Summary>
/// Return the bitmap object based on the specified parameter
/// Reference:
/// Using system. drawing;
/// Call example:
/// Eg1. Save it as an image file
/// Bitmap srbmp = srbitmap (SRS );
/// Srbmp. Save (directory. getcurrentdirectory () + "\ srs.gif", system. Drawing. imaging. imageformat. GIF );
/// Srbmp. Dispose ();
/// Eg2. The webpage is called as follows:
/// Bitmap srbmp = srbitmap (SRS );
/// System. Io. memorystream SRMs = new system. Io. memorystream ();
/// Srbmp. Save (SRMs, system. Drawing. imaging. imageformat. GIF );
/// Response. clearcontent ();
/// Response. contenttype = "image/GIF ";
/// Response. binarywrite (SRMs. toarray ());
/// Srbmp. Dispose ();
/// </Summary>
/// <Param name = "SRS"> </param>
/// <Returns> </returns>

Public static bitmap srbitmap (string SRs)
{
// Define the image Bending Angle
Int srseedangle = 45;
// Define the image
Bitmap srbmp = new Bitmap (SRS. length * 20, 30 );
// Draw
Graphics srgraph = graphics. fromimage (srbmp );
// Clear the image
Srgraph. Clear (color. aliceblue );
// Draw a border for the image
Srgraph. drawrectangle (new pen (color. Black, 0), 0, 0, srbmp. Width-1, srbmp. Height-1 );
// Define the instant count
Random srrandom = new random ();
// Define the paint brush
Pen srpen = new pen (color. lightgray, 0 );
// Draw Noise
For (INT I = 0; I <100; I ++)
{
Srgraph. drawrectangle (srpen, srrandom. Next (1, srbmp. Width-2), srrandom. Next (1, srbmp. Height-2), 1, 1 );
}
// Convert a string into a character array
Char [] srchars = SRS. tochararray ();
// Archive text
Stringformat srformat = new stringformat (stringformatflags. noclip );
// Set the vertical center of Text
Srformat. Alignment = stringalignment. Center;
// Set the text to center horizontally
Srformat. linealignment = stringalignment. Center;
// Define the font color

Color [] srcolors = {
Color. Black, color. Red, color. darkblue, color. Blue, color. Orange, color. Brown, color. darkcyan, color. Purple };
// Define the font
String [] srfonts = {"verdana", "Microsoft sans serif", "Comic Sans MS", "Arial", ""};
// Draw each character cyclically
For (INT I = 0, j = srchars. length; I <j; I ++)
{
// Define the font parameters as the font style, font size, and font shape.
Font srfont = new font (srfonts [srrandom. Next (5)], srrandom. Next (12,20), fontstyle. Regular );
// Fill the image
Brush srbrush = new solidbrush (srcolors [srrandom. Next (7)]);
// Define coordinates
Point srpoint = new point (16, 16 );
// Define the skew angle
Float srangle = srrandom. Next (-srseedangle, srseedangle );
// Skew
Srgraph. translatetransform (srpoint. X, srpoint. y );
Srgraph. rotatetransform (srangle );
// Fill in characters
Srgraph. drawstring (srchars [I]. tostring (), srfont, srbrush, 1, 1, srformat );
// Return to normal
Srgraph. rotatetransform (-srangle );
Srgraph. translatetransform (2,-srpoint. y );
}
Srgraph. Dispose ();
Return srbmp;
}

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.