最初接觸時,感覺很好玩,然後就自己研究了下,做了個demo,然後整理下,下次可以直接使用啦,英文大小寫,點擊可以切換 上代碼了。。。。 頁面代碼: <img id="Img" src="/Login/GetCheckCode" /> 這個是頁面JQuery點擊更換方法 $("#Img").click ...
最初接觸時,感覺很好玩,然後就自己研究了下,做了個demo,然後整理下,下次可以直接使用啦,英文大小寫,點擊可以切換
上代碼了。。。。
頁面代碼:
<img id="Img" src="/Login/GetCheckCode" />
這個是頁面JQuery點擊更換方法
$("#Img").click(function () {
//this.src = "/Login/GetCheckCode?time="+(new Date()).getTime(); //這個也可以的
//$("#Img").attr("src", "/Login/GetCheckCode?"+Math.random());
$("#Img").attr("src", "/Login/GetCheckCode?time=" + (new Date()).getTime());
註意:src後面加個time的時間參數,是為了再次載入驗證碼,否則載入不了。
網上的解釋:伺服器端編寫好生成驗證碼的方法,由於客戶端瀏覽器會緩存URL相同的資源,可以使用隨機數/或者時間來重新請求:
});
後臺代碼:
public ActionResult GetCheckCode()
{
string code = GenerateCheckCodes(4);
Session["ValidateCode"] = code;
byte[] bytes = CreateCheckCodeImage(code);
return File(bytes, @"image/jpeg");
}
private string GenerateCheckCodes(int iCount)
{
char[] oCharacter = {'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'
};
//int number;
string checkCode = String.Empty;
int iSeed = DateTime.Now.Millisecond;
System.Random random = new Random(iSeed);
for (int i = 0; i < iCount; i++)
{
checkCode += oCharacter[random.Next(oCharacter.Length)];
//純數字
//number = random.Next(10);
//number = oCharacter[random.Next(oCharacter.Length)];
//checkCode += number.ToString();
}
return checkCode;
}
private byte[] CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return null;
}
int iWordWidth = 20;
int iImageWidth = checkCode.Length * iWordWidth;
Bitmap image = new Bitmap(iImageWidth, 30);
Graphics g = Graphics.FromImage(image);
try
{
//生成隨機生成器
Random random = new Random();
//清空圖片背景色
g.Clear(Color.White);
//畫圖片的背景噪音點
for (int i = 0; i < 20; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
//畫圖片的背景噪音線
for (int i = 0; i < 2; i++)
{
int x1 = 0;
int x2 = image.Width;
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
if (i == 0)
{
g.DrawLine(new Pen(Color.Gray, 2), x1, y1, x2, y2);
}
}
for (int i = 0; i < checkCode.Length; i++)
{
string Code = checkCode[i].ToString();
int xLeft = iWordWidth * (i);
random = new Random(xLeft);
int iSeed = DateTime.Now.Millisecond;
int iValue = random.Next(iSeed) % 4;
if (iValue == 0)
{
Font font = new Font("Arial", 16, (FontStyle.Bold | System.Drawing.FontStyle.Italic));
Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Red, 1.5f, true);
g.DrawString(Code, font, brush, xLeft, 2);
}
else if (iValue == 1)
{
Font font = new System.Drawing.Font("楷體", 16, (FontStyle.Bold));
Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.DarkRed, 1.3f, true);
g.DrawString(Code, font, brush, xLeft, 2);
}
else if (iValue == 2)
{
Font font = new System.Drawing.Font("宋體", 16, (System.Drawing.FontStyle.Bold));
Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Green, Color.Blue, 1.2f, true);
g.DrawString(Code, font, brush, xLeft, 2);
}
else if (iValue == 3)
{
Font font = new System.Drawing.Font("黑體", 16, (System.Drawing.FontStyle.Bold |
System.Drawing.FontStyle.Bold));
Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Green, 1.8f, true);
g.DrawString(Code, font, brush, xLeft, 2);
}
}
////畫圖片的前景噪音點 ---有無這段代碼 貌似沒啥變化
for (int i = 0; i < 8; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//畫圖片的邊框線
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
//Response.ClearContent();
//Response.ContentType = "image/jpeg";
//Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
好了,就到這裡了,有問題或者不正確的可以指正,我每天都會看一次。謝謝,希望對你有幫助。
說一下,這個代碼的格式化 ,太麻煩了,每次都要一行一行的tab鍵。