C#WinForm中 驗證碼(漢字)的實現

來源:互聯網
上載者:User

代碼如下:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; enum Chinese{ 在, 上, 古, 早, 期, 的, 人, 名, 一, 般, 都, 很, 樸, 實, 如, 夏, 商, 兩, 代, 留, 下, 孔, 甲, 履, 癸, 外, 丙, 雍, 己, 盤, 庚, 武, 丁, 小, 辛, 等, 以, 幹, 支, 可, 能, 與 }; class DrawVerification { /// <summary> /// 將驗證碼繪製成一張圖片,並將其設為選中控制項的BackgroundImage /// 本方法只適用於支援背景圖片的控制項 /// </summary> /// <param name="control">要繪製驗證碼的控制項</param> /// <param name="xPosition">BackgroundImage的X座標</param> /// <param name="yPosition">BackgroundImage的Y座標</param> /// <param name="width">要繪製圖片的寬度</param> /// <param name="height">要繪製圖片的高度</param> /// <returns>成功執行返回驗證碼</returns> /// public static string Draw(Control control, int xPosition, int yPosition, int width, int height) { string resultStr = string.Empty; if (control != null) { int x = xPosition + width; int y = yPosition + height; //如果BackgroundImage的範圍超出控制項的可簽名簿範圍,不繪製,提示錯誤,隨機返回一個漢字(以防驗證碼為空白也能成功驗證,不過這當然也有隨機能寫對,不過要從int.MaxValue的4次方分之一得到的機率也不大) if (control.Size.Width < x || control.Size.Height < y || width <20 || height<=0) { MessageBox.Show("當前要繪製驗證碼的位置無效", "資訊提示", MessageBoxButtons.OK, MessageBoxIcon.Information); Random r = new Random(); return ((char)r.Next(0, int.MaxValue)).ToString() + ((char)r.Next(0, int.MaxValue)).ToString() + ((char)r.Next(0, int.MaxValue)).ToString() + ((char)r.Next(0, int.MaxValue)).ToString(); } Random random = new Random(); Bitmap image = new Bitmap(xPosition + width, yPosition + height); //建立一張空圖(BackgroundImage的預設位置為該控制項的(0,0)座標處) Graphics g = Graphics.FromImage(image); //以該圖為畫布 Pen p = null; //建立畫筆 for (int i = 0; i < width / 20; i++) //隨機畫(width / 20)條線 { Point p1 = new Point(random.Next(xPosition, x), random.Next(yPosition, y)); Point p2 = new Point(random.Next(xPosition, x), random.Next(yPosition, y)); p = new Pen(new SolidBrush(Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))), random.Next(1, 3)); g.DrawLine(p, p1, p2); for (int j = 0; j < 10; j++) //畫10倍於線的點(實際上是一條短線) { int xPos = random.Next(xPosition, x); int yPos = random.Next(yPosition, y); g.DrawLine(p, new Point(xPos, yPos), new Point(xPos - 2, yPos - 2)); } } p.Dispose(); //釋放 for (int i = 0; i < 4; i++) { int strSize = random.Next(width * 10 / 100, width * 15 / 100); //字型大小 if (strSize > height - 10) { strSize -= 10; if (strSize <= 0) { strSize += 10; } } /*從枚舉中得到漢字 也曾想過以((char)random.Next(19968, 40869)).ToString()得到漢字,但結果是相當多的字不認識,冏 只能自己隨便寫了幾個漢字來用了 */ string str = ((Chinese)random.Next(0, 39)).ToString(); resultStr += str; //向畫布上畫字串 g.DrawString(str, new Font("華文行楷", strSize, FontStyle.Italic), new SolidBrush(Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))), new PointF(xPosition + i * width / 4, yPosition + height / 4) ); } g.Dispose(); //釋放 try { control.BackgroundImageLayout = ImageLayout.None; //設定背景映像布局 } catch { } control.BackgroundImage = image; //設定背景映像 } return resultStr; //返回字串 } }

 

 假如表單上有個PictureBox控制項,name屬性為pictureBox1,調用:

DrawVerification.Draw(pictureBox1, 0, 0, pictureBox1.Width, pictureBox1.Height);

如果直接繪製到表單上,調用:

 DrawVerification.Draw(this, 100, 100, 200, 60);


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.