1. 程式人生 > >MVC中登錄頁圖片驗證碼總結

MVC中登錄頁圖片驗證碼總結

圖片 idt ali rap nag returns emp 簡單的 sys

直接上代碼了

後臺代碼:

技術分享圖片
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Manage.Controllers
{
public class ValidateCodeController : Controller
{
//
// GET: /ValidateCode/
public
FileContentResult GetCode() { //string code = GetRndStr(); string code = GetNumAndStr(4); byte[] imageByte; using (Bitmap img = CreateImages(code, "ch")) { imageByte = BitmapToByte(img); Session["validate"] = code; } return File(imageByte, "image/gif"); } public static byte[] BitmapToByte(Bitmap bitmap) {
using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Jpeg); byte[] data = new byte[stream.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(data, 0, Convert.ToInt32(stream.Length)); return data; } } public bool IsReusable { get { return false; } } /// <summary> /// 數字隨機數
/// </summary> /// <returns></returns> private string GetRndNum() { string code = string.Empty; Random random = new Random(); for (int i = 0; i < 4; i++) { code += random.Next(9); } return code; } /// <summary> /// 英文隨機 /// </summary> /// <returns></returns> private string GetRndStr() { string Vchar = "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[] VcArray = Vchar.Split(,); string checkCode = string.Empty; Random rand = new Random(); for (int i = 0; i < 4; i++) { int t = rand.Next(VcArray.Length); checkCode += VcArray[t]; } return checkCode; } /// <summary> /// 中文隨機 /// </summary> /// <returns></returns> private string GetRndCh() { System.Text.Encoding gb = System.Text.Encoding.Default;//獲取GB2312編碼頁(表) object[] bytes = CreateRegionCode(4);//生4個隨機中文漢字編碼 string[] str = new string[4]; System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < 4; i++) { //根據漢字編碼的字節數組解碼出中文漢字 str[i] = gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[]))); sb.Append(str[i].ToString()); } return sb.ToString(); } /// <summary> /// 產生隨機中文字符 /// </summary> /// <param name="strlength"></param> /// <returns></returns> private static object[] CreateRegionCode(int strlength) { //定義一個字符串數組儲存漢字編碼的組成元素 string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; Random rnd = new Random(); object[] bytes = new object[strlength]; for (int i = 0; i < strlength; i++) { //區位碼第1位 int r1 = rnd.Next(11, 14); string str_r1 = rBase[r1].Trim(); //區位碼第2位 rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i); int r2; if (r1 == 13) { r2 = rnd.Next(0, 7); } else { r2 = rnd.Next(0, 16); } string str_r2 = rBase[r2].Trim(); //區位碼第3位 rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);//更換隨機種子 int r3 = rnd.Next(10, 16); string str_r3 = rBase[r3].Trim(); //區位碼第4位 rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i); int r4; if (r3 == 10) { r4 = rnd.Next(1, 16); } else if (r3 == 15) { r4 = rnd.Next(0, 15); } else { r4 = rnd.Next(0, 16); } string str_r4 = rBase[r4].Trim(); //定義兩個字節變量存儲產生的隨機漢字區位碼 byte byte1 = Convert.ToByte(str_r1 + str_r2, 16); byte byte2 = Convert.ToByte(str_r3 + str_r4, 16); //將兩個字節變量存儲在字節數組中 byte[] str_r = new byte[] { byte1, byte2 }; //將產生的一個漢字的字節數組放入object數組中 bytes.SetValue(str_r, i); } return bytes; } public string GetNumAndStr(int length) { string str = string.Empty; string Vchar = "0,1,2,3,4,5,6,7,8,9,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[] VcArray = Vchar.Split(new Char[] { , });//拆分成數組 string[] num = new string[length]; int temp = -1;//記錄上次隨機數值,盡量避避免生產幾個一樣的隨機數 Random rand = new Random(); //采用一個簡單的算法以保證生成隨機數的不同 for (int i = 1; i < length + 1; i++) { if (temp != -1) { rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks)); } int t = rand.Next(61); temp = t; str += VcArray[t]; // num[i - 1] = VcArray[t]; } return str; } /// <summary> /// 畫圖片的背景圖+幹擾線 /// </summary> /// <param name="checkCode"></param> /// <returns></returns> private Bitmap CreateImages(string checkCode, string type) { int step = 0; if (type == "ch") { step = 5;//中文字符,邊界值做大 } int iwidth = (int)(checkCode.Length * (13 + step)); System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 22); Graphics g = Graphics.FromImage(image); g.Clear(Color.White);//清除背景色 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };//定義隨機顏色 string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" }; Random rand = new Random(); for (int i = 0; i < 50; i++) { int x1 = rand.Next(image.Width); int x2 = rand.Next(image.Width); int y1 = rand.Next(image.Height); int y2 = rand.Next(image.Height); g.DrawLine(new Pen(Color.LightGray, 1), x1, y1, x2, y2);//根據坐標畫線 } for (int i = 0; i < checkCode.Length; i++) { int cindex = rand.Next(7); int findex = rand.Next(5); Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold); Brush b = new System.Drawing.SolidBrush(c[cindex]); int ii = 4; if ((i + 1) % 2 == 0) { ii = 2; } g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * (12 + step)), ii); } g.DrawRectangle(new Pen(Color.Silver, 0), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); return image; } } }
View Code

前臺頁面:

<img id="imgCode" onclick="CodeChange();" style="cursor: pointer; height: 40px; width: 105.3px; margin-bottom:-14px;margin-top:0px; margin-right:0px;" title="看不清?" />

$(function () {
$("#imgCode").attr("src", "/ValidateCode/GetCode?t=" + (new Date()).valueOf());
})

// 更換圖片驗證碼
function CodeChange() {
var imgNode = $("#imgCode");
imgNode.attr("src", "/ValidateCode/GetCode?t=" + (new Date()).valueOf());
}

FileContentResult類型的返回一個圖片文件

本文原文來自:https://www.cnblogs.com/huaqiqi/p/5859458.html

MVC中登錄頁圖片驗證碼總結