最近寫的圖形驗證碼程式,提供大家參考。
最近寫的圖形驗證碼程式,提供大家參考。
using System;
using System.Web;
using System.Data;
using System.Drawing;
namespace Captcha
{
///
/// 圖形驗證碼
///
public class Captcha : IHttpHandler
{
int int_Red = 0, int_Green = 0, int_Blue = 0, int_Base = 0, int_Rand = 0;
Random RandGen = new Random();
int FontSize = 14;
string[] aryFonts = new string[] { "Arial Black", "Verdana", "Comic Sans MS"
, "sans-serif", "Helvetica"};
public void ProcessRequest(HttpContext context)
{
//依個人專案需求取得隨機亂碼
string RandomCode = getRandomCode();
CreateCheckCodeImage(RandomCode);
}
///
/// 取得顏色
///
public Color getColor(int int_Base=0, int int_Rand=255)
{
int_Red = RandGen.Next(0, int_Rand) + int_Base;
int_Green = RandGen.Next(0, int_Rand) + int_Base;
int_Blue = RandGen.Next(0, int_Rand) + int_Base;
return Color.FromArgb(int_Red, int_Green, int_Blue);
}
///
/// 取得字型隨機顏色
///
public Color getFontColor()
{
int_Base = 30;
int_Rand = 100;
return getColor(int_Base, int_Rand);
}
///
/// 取得中間隨機顏色
///
public Color getMidColor()
{
int_Base = 100;
int_Rand = 100;
return getColor(int_Base, int_Rand);
}
private void CreateCheckCodeImage(string checkCode)
{
if (string.IsNullOrEmpty(checkCode))
return;
int i = 0, x = 0, y = 0;
FontStyle fontset = default(FontStyle);
Font font = default(Font);
System.Drawing.Drawing2D.LinearGradientBrush brush;
Bitmap image = new Bitmap(checkCode.Length * 25, 30);
Graphics g = Graphics.FromImage(image);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
try
{
//設定畫布底色
g.Clear(Color.White);
//背景斜線
for (i = 0; i < 5; i++)
g.DrawLine(new Pen(getFontColor()), RandGen.Next(image.Width), RandGen.Next(image.Height)
, RandGen.Next(image.Width), RandGen.Next(image.Height));
for (i = 0; i < checkCode.Length; i++)
{
//隨機樣式
switch (RandGen.Next(0, 2))
{
case 0:
fontset = FontStyle.Bold;
break;
case 1:
fontset = FontStyle.Italic;
break;
case 2:
fontset = FontStyle.Regular;
break;
}
//隨機字型
font = new Font(aryFonts[RandGen.Next(0, aryFonts.Length - 1)], FontSize, fontset);
//隨機位移
x = (i * 22) + RandGen.Next(0, 10);
y = 1 + RandGen.Next(0, 3);
//產生隨機顏色的漸層筆刷
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height)
, getFontColor(), getMidColor(), 1.2f, true);
//依照設定畫出文字
g.DrawString(checkCode[i].ToString(), font, brush, x, y);
}
//在文字上放入干擾雜點
for (i = 0; i < 100; i++)
image.SetPixel(RandGen.Next(image.Width), RandGen.Next(image.Height), getFontColor());
//畫出方型外框
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
//設置無Cache
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Buffer=true;
HttpContext.Current.Response.ExpiresAbsolute=DateTime.Now;
HttpContext.Current.Response.Expires=0;
HttpContext.Current.Response.AppendHeader("Pragma", "No-Cache");
HttpContext.Current.Response.CacheControl="no-cache";
HttpContext.Current.Response.ContentType = "image/Png";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
catch
{
throw;
}
finally {
ms.Close();
g.Dispose();
image.Dispose();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}