Implementation of simple C #, ASP. NET MVC Verification Code

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Text;
Using System.Drawing;
Using System.IO;

The implementation of the namespace verification code. Validatecode
{
<summary>
Verification Code generation Tool class
</summary>
///


public class Validatecodehelper
{
private static Random Rand = new Random ();
private static string code;
<summary>
Randomly generate verification codes of the specified length
</summary>
<param name= "Length" ></param>
<returns></returns>
public static string GetCode (int length) {

String codes = "AaBbCcDdEeFfJjHhIiJjKkMmNnPpQrRSsTtUuVvWwXxYyZz0123456789";



StringBuilder sb = new StringBuilder ();
for (int i = 0; I <length; i++)
{
int Index=rand. Next (codes. Length);
if (sb.) ToString (). Contains (Codes[index])) {

i--;
Continue
}
Sb. Append (Codes[index]);
}
Code = SB. ToString ();
return code;

}

<summary>
Get random colors
</summary>
<returns></returns>
private static Color Getrandomcolor () {


int red = rand. Next (10, 255);
int green = rand. Next (10, 255);
int blue = rand. Next (10, 255);

Return Color.FromArgb (red, green, blue);
}

<summary>
Generate Verification Code
</summary>
<returns></returns>
public static byte[] Validatecode (string code) {

Bitmap img = new Bitmap (100,30);
Graphics g = Graphics.fromimage (IMG);
G.fillrectangle (brushes.white, 0, 0, IMG. Width, IMG. Height);
G.drawrectangle (New Pen (Color.Black), 1, 1, IMG. Width-2, IMG. HEIGHT-2);
Brush Bush = new SolidBrush (color.steelblue);
g.DrawString (Code, New Font ("Blackbody", Fontstyle.italic), Bush, 10, 2);
Random r = new Random ();
Draw lines
for (int i = 0; i < 5; i++)
{
G.drawline (New Pen (Getrandomcolor ()), R.next (IMG. Width), R.next (IMG. Height), R.next (IMG. Width), R.next (IMG. Height));
}
Draw a bit of mania
for (int i = 0; i <; i++)
{
Img. SetPixel (R.next (img. Width), R.next (IMG. Height), Getrandomcolor ());
}
MemoryStream ms = new MemoryStream ();
Img. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg);

byte[] data = Ms. GetBuffer ();
G.dispose ();
Ms. Close ();
return data;
}

<summary>
Generate Verification Code
</summary>
<param name= "Code" > Verification Code </param>
<param name= "FontColor" > Captcha color </param>
<returns></returns>
public static byte[] Validatecode (string Code,color fontcolor) {

Bitmap img = new Bitmap (100,30);
Graphics g = Graphics.fromimage (IMG);

G.fillrectangle (brushes.white,0, 0, IMG. Width, IMG. Height);

Brush Bush = new SolidBrush (fontcolor);
g.DrawString (Code, New Font ("Blackbody", Fontstyle.italic), Bush, 10, 2);
Random r = new Random ();
Draw lines
for (int i = 0; i < 5; i++)
{
G.drawline (New Pen (Getrandomcolor ()), R.next (IMG. Width), R.next (IMG. Height), R.next (IMG. Width), R.next (IMG. Height));
}
Draw a bit of mania
for (int i = 0; i <; i++)
{
Img. SetPixel (R.next (img. Width), R.next (IMG. Height), Getrandomcolor ());
}
MemoryStream ms = new MemoryStream ();
Img. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg);

byte[] data = Ms. GetBuffer ();
G.dispose ();
Ms. Close ();
return data;
}
<summary>
Generate Verification Code
</summary>
<param name= "Code" > Verification Code </param>
<param name= "FontColor" > Captcha color </param>
<param name= "BackgroundColor" > Captcha background color </param>
<returns></returns>
public static byte[] Validatecode (String Code,color backgroundcolor, Color fontcolor)
{

Bitmap img = new Bitmap (100, 30);
Graphics g = Graphics.fromimage (IMG);
Brush bush1 = new SolidBrush (backgroundcolor);
G.fillrectangle (bush1, 0, 0, IMG. Width, IMG. Height);
Brush Bush = new SolidBrush (fontcolor);
g.DrawString (Code, New Font ("Blackbody", Fontstyle.italic), Bush, 10, 2);
Random r = new Random ();
Draw lines
for (int i = 0; i < 5; i++)
{
G.drawline (New Pen (Getrandomcolor ()), R.next (IMG. Width), R.next (IMG. Height), R.next (IMG. Width), R.next (IMG. Height));
}
Draw a bit of mania
for (int i = 0; i <; i++)
{
Img. SetPixel (R.next (img. Width), R.next (IMG. Height), Getrandomcolor ());
}
MemoryStream ms = new MemoryStream ();
Img. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg);

byte[] data = Ms. GetBuffer ();
G.dispose ();
Ms. Close ();
return data;
}

<summary>
Determine if the verification code is correct
</summary>
<param name= "Code" ></param>
<returns></returns>
public static bool Isvalidate (string Code) {

if (string. IsNullOrEmpty (Code) | |! Code. ToLower (). Equals (Code.tolower ())) {

return false;
}

return true;
}
}
}

Calls in the controller

Public ActionResult Validatecode () {

Gets the specified length verification code

String code= Validatecodehelper.getcode (5);
tempdata["Code" = code;//Storage Verification Code for verification

Draws the captcha to the picture, saves it to the memory stream, and returns a byte array
Byte[] data= Validatecodehelper.validatecode (code);

return File (data, "image/jpeg");
}

On the front end of the call

<script>


function Change () {

var img = document.getelementsbytagname (' img ') [0];

IMG.SRC = Img.src + "?";

}
</script>
<form method= "POST" action= "/home/login" >
<table>
<tr>
<td> Verification Code:</td>
<td>
<a href= "javascript:void (0)" onclick= "Change ()" > For a new </a>
</td>
</tr>
<tr>
<td> Enter the verification code:</td>
<td><input type= "text" name= "code"/></td>
</tr>
</table>
<input type= "Submit" value= "Submission"/>
</form>

Implementation of simple C #, ASP. NET MVC Verification Code

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.