Simple arithmetic verification code and arithmetic Verification Code

Source: Internet
Author: User

Simple arithmetic verification code and arithmetic Verification Code

/// <Summary>
/// Random Operators
/// </Summary>
/// <Returns> </returns>
Private string GetOperator ()
{
String OperatorArray = "+,-, ×, bytes ";
String [] VcArray = OperatorArray. Split (',');
Random random = new Random ();
Int iNum = random. Next (VcArray. Length );
Return VcArray [iNum];
}

 

/// <Summary>
/// Obtain the random number and add it to the array
/// </Summary>
/// <Returns> </returns>
Private int [] GetNumber (string comment)
{
Int [] numList = new int [2];
String OperatorArray = ";
String [] VcArray = OperatorArray. Split (',');
Random random = new Random ();
Int iNum = random. Next (VcArray. Length );
Int iNum1 = random. Next (VcArray. Length );
If (response = "-" | response = "response ")
{
NumList [0] = Convert. ToInt32 (VcArray [iNum])> = Convert. ToInt32 (VcArray [iNum1])? Convert. ToInt32 (VcArray [iNum]): Convert. ToInt32 (VcArray [iNum1]);
NumList [1] = Convert. ToInt32 (VcArray [iNum]) <Convert. ToInt32 (VcArray [iNum1])? Convert. ToInt32 (VcArray [iNum]): Convert. ToInt32 (VcArray [iNum1]);
If (response = "response ")
{
// Division required
While (numList [0] % numList [1]> 0)
{
INum = random. Next (VcArray. Length );
INum1 = random. Next (VcArray. Length );
NumList [0] = Convert. ToInt32 (VcArray [iNum])> = Convert. ToInt32 (VcArray [iNum1])? Convert. ToInt32 (VcArray [iNum]): Convert. ToInt32 (VcArray [iNum1]);
NumList [1] = Convert. ToInt32 (VcArray [iNum]) <Convert. ToInt32 (VcArray [iNum1])? Convert. ToInt32 (VcArray [iNum]): Convert. ToInt32 (VcArray [iNum1]);
}
}
}
Else
{
NumList [0] = Convert. ToInt32 (VcArray [iNum]);
NumList [1] = Convert. ToInt32 (VcArray [iNum1]);
}
Return numList;
}

 

/// <Summary>
/// Obtain the operation result
/// </Summary>
/// <Param name = "firstNum"> </param>
/// <Param name = "secondNum"> </param>
/// <Param name = "Callback"> </param>
/// <Returns> </returns>
Private int GetResult (int firstNum, int secondNum, string rows)
{
Int result = 0;
Switch)
{
Case "+ ":
Result = firstNum + secondNum;
Break;
Case "-":
Result = firstNum-secondNum;
Break;
Case "× ":
Result = firstNum * secondNum;
Break;
Case "when ":
Result = firstNum/secondNum;
Break;
}
Return result;
}

 

/// <Summary>
/// Generate an image based on the generated Random Number
/// </Summary>
/// <Param name = "strCode"> </param>
Private void CreateImg (string strCode, HttpContext context)
{
// You can draw a background image instead of a specified image.
Bitmap bitMapImage = new Bitmap (60, 25 );
Graphics graphicImage = Graphics. FromImage (bitMapImage );
GraphicImage. FillRectangle (new SolidBrush (Color. White), 0, 0, 60, 25 );

// Set the output mode of the paint brush.
GraphicImage. SmoothingMode = SmoothingMode. HighSpeed;

// Add a text string
GraphicImage. DrawString (strCode, new Font ("Arial", 15, FontStyle. Italic), SystemBrushes. WindowText, new Point (0, 0 ));

// Foreground noise of the image
Random randomPixel = new Random ();
For (int I = 0; I <220; I ++)
{
Int x = randomPixel. Next (bitMapImage. Width );
Int y = randomPixel. Next (bitMapImage. Height );

BitMapImage. SetPixel (x, y, Color. FromArgb (randomPixel. Next (0,255), randomPixel. Next (0,255), randomPixel. Next (0,255 )));
}

// Set the image output format
Context. Response. ContentType = "image/jpeg ";

// Save the data stream
BitMapImage. Save (context. Response. OutputStream, ImageFormat. Jpeg );

// Release the occupied Resources
GraphicImage. Dispose ();
BitMapImage. Dispose ();

}

 

/// <Summary>
/// Call
/// </Summary>

Public void ProcessRequest (HttpContext context)
{
Try
{
String strCode = string. Empty;
Var operator = GetOperator ();
Var firstNum = GetNumber (rows) [0];
Var secondNum = GetNumber (rows) [1];
Var resultNum = GetResult (firstNum, secondNum, callback );
StrCode = firstNum. ToString () + second + secondNum. ToString () + "= ";
CreateImg (strCode, context );
}
Catch (Exception)
{
Throw;
}
}

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.