代碼如下:
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);