標籤:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Drawing;
using System.IO;
namespace 驗證碼的實現.ValidateCode
{
/// <summary>
/// 驗證碼產生工具類
/// </summary>
///
public class ValidateCodeHelper
{
private static Random rand = new Random();
private static string code;
/// <summary>
/// 隨機產生指定長度的驗證碼
/// </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>
/// 擷取隨機顏色
/// </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>
/// 產生驗證碼
/// </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("黑體", 20, FontStyle.Italic), bush, 10, 2);
Random r = new Random();
//畫線條
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));
}
//畫躁點
for (int i = 0; i < 100; 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>
/// 產生驗證碼
/// </summary>
/// <param name="code">驗證碼</param>
/// <param name="fontColor">驗證碼顏色</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("黑體", 20, FontStyle.Italic), bush, 10, 2);
Random r = new Random();
//畫線條
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));
}
//畫躁點
for (int i = 0; i < 100; 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>
/// 產生驗證碼
/// </summary>
/// <param name="code">驗證碼</param>
/// <param name="fontColor">驗證碼顏色</param>
/// <param name="backgroundColor">驗證碼背景顏色</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("黑體", 20, FontStyle.Italic), bush, 10, 2);
Random r = new Random();
//畫線條
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));
}
//畫躁點
for (int i = 0; i < 100; 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>
/// 判斷驗證碼是否正確
/// </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;
}
}
}
在控制器中的調用
public ActionResult ValidateCode(){
//擷取指定長度驗證碼
string code= ValidateCodeHelper.GetCode(5);
TempData["code"] = code;//儲存驗證碼用於驗證
//將驗證碼繪製到圖片上、儲存到記憶體流中並返回位元組數組
byte[] data= ValidateCodeHelper.ValidateCode(code);
return File(data,"image/jpeg");
}
在前端的調用
<script>
function change() {
var img = document.getElementsByTagName(‘img‘)[0];
img.src = img.src + "?";
}
</script>
<form method="post" action="/Home/Login">
<table>
<tr>
<td>驗證碼:</td>
<td><img src="/Home/ValidateCode" style="cursor:pointer" onclick="this.src =this.src+‘?‘" />
<a href="javascript:void(0)" onclick="change()">換一張</a>
</td>
</tr>
<tr>
<td>輸入驗證碼:</td>
<td><input type="text" name="code"/></td>
</tr>
</table>
<input type="submit" value="提交"/>
</form>
簡單C#、asp.net mvc驗證碼的實現