asp.net 簡易產生註冊碼(數字+大小寫字母)

來源:互聯網
上載者:User

如果有哪裡看不懂的,請留言哦

產生隨機碼類:SigowayRandom.cs

複製代碼 代碼如下:using System;
namespace RongYi.Model.Common
{
/// <summary>
/// SigowayRandom 的摘要說明
/// </summary>
public class SigowayRandom
{
#region 擷取校正碼

/// <summary>
/// 擷取校正碼
/// </summary>
/// <returns>校正碼字元數組</returns>
public static string[] GetCheckCode()
{
string[] strCheckCode = new string[4];

// 已系統時間毫秒為隨機種子
int nSeed = Convert.ToInt16(DateTime.Now.Millisecond);
Random random = new Random(nSeed);

// 產生0-9隨機數
strCheckCode[0] = Convert.ToString(random.Next(1, 10));
// 產生a-z、A-Z隨機字母
strCheckCode[1] = SigowayRandom.GetLetter(random);
strCheckCode[2] = Convert.ToString(random.Next(1, 10));
strCheckCode[3] = SigowayRandom.GetLetter(random);

// 返回校正碼
return strCheckCode;
}

#endregion

#region 擷取字母,區分大小寫

/// <summary>
/// 擷取字母,區分大小寫
/// </summary>
/// <returns>大小寫字母</returns>
private static string GetLetter(Random random)
{
// 隨機數
int nChar = random.Next(65, 122);

// 非字母ASCII段
if (nChar >= 91 && nChar <= 96)
{
nChar -= 6;
}

return Convert.ToString((char)nChar);
}

#endregion
}
}

繪製校正碼類:SigowayDraw.cs 複製代碼 代碼如下:using System.Drawing;
using System.Drawing.Imaging;
using System.Web;

namespace RongYi.Model.Common
{
/// <summary>
/// SigowayDraw 的摘要說明
/// </summary>
public class SigowayDraw
{
#region 構造方法

/// <summary>
/// 構造方法
/// </summary>
public SigowayDraw() { }

#endregion

#region 畫校正碼

/// <summary>
/// 畫校正碼
/// </summary>
/// <returns>校正碼</returns>
public string DrawString()
{
// 設定字型
Font drawFont = new Font("Arial", 10);
// 建立位元影像元素
Bitmap objBitmap = new Bitmap(50, 20);
// 建立畫圖對象
Graphics objGraphics = Graphics.FromImage(objBitmap);
// 設定畫布背景色
objGraphics.Clear(Color.White);
// 擷取隨機字串
string[] strDrawString = SigowayRandom.GetCheckCode();

// 畫字串
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);

// 畫幹擾線
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);

// 把映像畫到位元影像對象中
objGraphics.DrawImage(objBitmap, 0, 0);

// 設定儲存圖片路徑及名字
string strFile = HttpRuntime.AppDomainAppPath.ToString() + "/Resource/img/CheckCode.gif";

// 輸出檔案
objBitmap.Save(strFile, ImageFormat.Gif);

// 串連校正碼字串
string strCheckCode = string.Empty;
foreach (string strTemp in strDrawString)
{
strCheckCode += strTemp;
}

// 返回校正碼
return strCheckCode;
}

#endregion
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.