asp.net 簡(jiǎn)易生成注冊(cè)碼(數(shù)字+大小寫(xiě)字母)
如果有哪里看不懂的,請(qǐng)留言哦
生成隨機(jī)碼類:SigowayRandom.cs
using System;
namespace RongYi.Model.Common
{
/// <summary>
/// SigowayRandom 的摘要說(shuō)明
/// </summary>
public class SigowayRandom
{
#region 獲取校驗(yàn)碼
/// <summary>
/// 獲取校驗(yàn)碼
/// </summary>
/// <returns>校驗(yàn)碼字符數(shù)組</returns>
public static string[] GetCheckCode()
{
string[] strCheckCode = new string[4];
// 已系統(tǒng)時(shí)間毫秒為隨機(jī)種子
int nSeed = Convert.ToInt16(DateTime.Now.Millisecond);
Random random = new Random(nSeed);
// 產(chǎn)生0-9隨機(jī)數(shù)
strCheckCode[0] = Convert.ToString(random.Next(1, 10));
// 產(chǎn)生a-z、A-Z隨機(jī)字母
strCheckCode[1] = SigowayRandom.GetLetter(random);
strCheckCode[2] = Convert.ToString(random.Next(1, 10));
strCheckCode[3] = SigowayRandom.GetLetter(random);
// 返回校驗(yàn)碼
return strCheckCode;
}
#endregion
#region 獲取字母,區(qū)分大小寫(xiě)
/// <summary>
/// 獲取字母,區(qū)分大小寫(xiě)
/// </summary>
/// <returns>大小寫(xiě)字母</returns>
private static string GetLetter(Random random)
{
// 隨機(jī)數(shù)
int nChar = random.Next(65, 122);
// 非字母ASCII段
if (nChar >= 91 && nChar <= 96)
{
nChar -= 6;
}
return Convert.ToString((char)nChar);
}
#endregion
}
}
繪制校驗(yàn)碼類:SigowayDraw.cs
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
namespace RongYi.Model.Common
{
/// <summary>
/// SigowayDraw 的摘要說(shuō)明
/// </summary>
public class SigowayDraw
{
#region 構(gòu)造方法
/// <summary>
/// 構(gòu)造方法
/// </summary>
public SigowayDraw() { }
#endregion
#region 畫(huà)校驗(yàn)碼
/// <summary>
/// 畫(huà)校驗(yàn)碼
/// </summary>
/// <returns>校驗(yàn)碼</returns>
public string DrawString()
{
// 設(shè)置字體
Font drawFont = new Font("Arial", 10);
// 創(chuàng)建位圖元素
Bitmap objBitmap = new Bitmap(50, 20);
// 創(chuàng)建畫(huà)圖對(duì)象
Graphics objGraphics = Graphics.FromImage(objBitmap);
// 設(shè)置畫(huà)布背景色
objGraphics.Clear(Color.White);
// 獲取隨機(jī)字符串
string[] strDrawString = SigowayRandom.GetCheckCode();
// 畫(huà)字符串
objGraphics.DrawString(strDrawString[0], drawFont, new SolidBrush(Color.Purple), 1, 2);
objGraphics.DrawString(strDrawString[1], drawFont, new SolidBrush(Color.Green), 12, 2);
objGraphics.DrawString(strDrawString[2], drawFont, new SolidBrush(Color.Red), 24, 2);
objGraphics.DrawString(strDrawString[3], drawFont, new SolidBrush(Color.SteelBlue), 35, 2);
// 畫(huà)干擾線
objGraphics.DrawLine(Pens.Silver, 5, 10, 40, 3);
objGraphics.DrawLine(Pens.Gray, 10, 5, 45, 15);
objGraphics.DrawLine(Pens.HotPink, 15, 20, 30, 10);
objGraphics.DrawLine(Pens.LightPink, 10, 15, 35, 20);
// 把圖像畫(huà)到位圖對(duì)象中
objGraphics.DrawImage(objBitmap, 0, 0);
// 設(shè)置保存圖片路徑及名字
string strFile = HttpRuntime.AppDomainAppPath.ToString() + "/Resource/img/CheckCode.gif";
// 輸出文件
objBitmap.Save(strFile, ImageFormat.Gif);
// 連接校驗(yàn)碼字符串
string strCheckCode = string.Empty;
foreach (string strTemp in strDrawString)
{
strCheckCode += strTemp;
}
// 返回校驗(yàn)碼
return strCheckCode;
}
#endregion
}
}
相關(guān)文章
ASP.Net刷新頁(yè)面后自動(dòng)滾動(dòng)到原來(lái)位置方法匯總
本文給大家匯總了3種ASP.Net實(shí)現(xiàn)的刷新頁(yè)面后自動(dòng)滾動(dòng)到原來(lái)位置方法,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-06-06.net core項(xiàng)目中常用的幾款類庫(kù)詳解(值得收藏)
這篇文章主要給大家介紹了關(guān)于.net core項(xiàng)目中常用的幾款類庫(kù)的相關(guān)資料,文章通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.net core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04C#中的Equals、RefrenceEquals和==的區(qū)別與聯(lián)系
C#中判斷兩個(gè)對(duì)象是否相等有Equals、RefrenceEquals和==三種,其中==為運(yùn)算符,其它兩個(gè)為方法,而Equals又有兩種版本,一個(gè)是靜態(tài)的,一個(gè)是虛擬的,詳細(xì)了解可以參考本文2012-12-12asp.net?core?MVC?全局過(guò)濾器之ExceptionFilter過(guò)濾器(1)
這篇文章主要為大家詳細(xì)介紹了asp.net?core?MVC?全局過(guò)濾器之ExceptionFilter過(guò)濾器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08不使用web服務(wù)(Service)實(shí)現(xiàn)文本框自動(dòng)完成擴(kuò)展
以前寫(xiě)Ajax 的AutoCompleteExtender功能,都需要寫(xiě)WCF Service或是Web Service數(shù)據(jù)源,下面的演示,不用寫(xiě)Service來(lái)實(shí)現(xiàn)文本框的AutoCompete extender功能,感興趣的朋友可以參考下哈2013-04-04.Net Core和jexus配置HTTPS服務(wù)方法
下面小編就為大家分享一篇.Net Core和jexus配置HTTPS服務(wù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)之用戶資料的修改和刪除3(七)
這篇文章主要為大家詳細(xì)介紹了ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)之用戶資料的修改和刪除,感興趣的小伙伴們可以參考一下2016-08-08asp.net 抓取網(wǎng)頁(yè)源碼三種實(shí)現(xiàn)方法
asp.net 抓取網(wǎng)頁(yè)源碼三種實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-06-06