简单的图像验证码程序
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string str_validateCode = GetRandomNumString(4);
this.Session["ValidateCode"] = str_validateCode;
createImage(str_validateCode);
}
}
public static System.Drawing.Color GetRandomColor()
{
System.Random randomnum_first = new Random((int)DateTime.Now.Ticks);
System.Threading.Thread.Sleep(randomnum_first.Next(50));
System.Random randomnum_second = new Random((int)DateTime.Now.Ticks);
int int_red = randomnum_first.Next(256);
int int_green = randomnum_second.Next(256);
int int_blue = (int_red + int_green) > 400 ? 0 : 400 - int_red - int_green;
int_blue = (int_blue > 255) ? 255 : int_blue;
return Color.FromArgb(int_red, int_green, int_blue);
}
public static void createImage(string str_validateCode)
{
int int_imageWidth = str_validateCode.Length * 15;
Random random = new Random();
System.Drawing.Bitmap theBitmap = new Bitmap(int_imageWidth, 20);
System.Drawing.Graphics theGraphics = System.Drawing.Graphics.FromImage(theBitmap);
theGraphics.Clear(Color.White);
theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_imageWidth - 1, 19);
System.Drawing.Font thefont = new Font("Arial", 10);
for (int int_index = 0; int_index < str_validateCode.Length; int_index++)
{
string str_char = str_validateCode.Substring(int_index, 1);
System.Drawing.Brush newbrush = new SolidBrush(GetRandomColor());
System.Drawing.Point thepos = new Point(int_index * 13 + 1 + random.Next(3), 1 + random.Next(3));
theGraphics.DrawString(str_char, thefont, newbrush, thepos);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
theBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ContentType = "image/Gif";
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
theGraphics.Dispose();
theBitmap.Dispose();
System.Web.HttpContext.Current.Response.End();
}
public static string GetRandomNumString(int n)
{
string yzm = string.Empty;
for (int i = 0; i <= 9; i++)
{
yzm += i.ToString() + ",";
}
for (int j = 65; j <= 90; j++)
{
yzm += ((char)j).ToString() + ",";
}
for (int j = 97; j <= 122; j++)
{
yzm += ((char)j).ToString() + ",";
}
yzm = yzm.Substring(0, yzm.Length - 1);
string[] varray = yzm.Split(',');
string Vnum = string.Empty;
int temp = -1;
System.Random random = new Random();
for (int i = 1; i <= n; i++)
{
if (temp != -1)
{
random = new Random(i * temp * (int)DateTime.Now.Ticks);
}
int t = random.Next(varray.Length);
if (temp != -1 && temp == t)
{
return GetRandomNumString(n);
}
temp = t;
Vnum += varray[t];
}
return Vnum;
}
{
if (!this.IsPostBack)
{
string str_validateCode = GetRandomNumString(4);
this.Session["ValidateCode"] = str_validateCode;
createImage(str_validateCode);
}
}
public static System.Drawing.Color GetRandomColor()
{
System.Random randomnum_first = new Random((int)DateTime.Now.Ticks);
System.Threading.Thread.Sleep(randomnum_first.Next(50));
System.Random randomnum_second = new Random((int)DateTime.Now.Ticks);
int int_red = randomnum_first.Next(256);
int int_green = randomnum_second.Next(256);
int int_blue = (int_red + int_green) > 400 ? 0 : 400 - int_red - int_green;
int_blue = (int_blue > 255) ? 255 : int_blue;
return Color.FromArgb(int_red, int_green, int_blue);
}
public static void createImage(string str_validateCode)
{
int int_imageWidth = str_validateCode.Length * 15;
Random random = new Random();
System.Drawing.Bitmap theBitmap = new Bitmap(int_imageWidth, 20);
System.Drawing.Graphics theGraphics = System.Drawing.Graphics.FromImage(theBitmap);
theGraphics.Clear(Color.White);
theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_imageWidth - 1, 19);
System.Drawing.Font thefont = new Font("Arial", 10);
for (int int_index = 0; int_index < str_validateCode.Length; int_index++)
{
string str_char = str_validateCode.Substring(int_index, 1);
System.Drawing.Brush newbrush = new SolidBrush(GetRandomColor());
System.Drawing.Point thepos = new Point(int_index * 13 + 1 + random.Next(3), 1 + random.Next(3));
theGraphics.DrawString(str_char, thefont, newbrush, thepos);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
theBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ContentType = "image/Gif";
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
theGraphics.Dispose();
theBitmap.Dispose();
System.Web.HttpContext.Current.Response.End();
}
public static string GetRandomNumString(int n)
{
string yzm = string.Empty;
for (int i = 0; i <= 9; i++)
{
yzm += i.ToString() + ",";
}
for (int j = 65; j <= 90; j++)
{
yzm += ((char)j).ToString() + ",";
}
for (int j = 97; j <= 122; j++)
{
yzm += ((char)j).ToString() + ",";
}
yzm = yzm.Substring(0, yzm.Length - 1);
string[] varray = yzm.Split(',');
string Vnum = string.Empty;
int temp = -1;
System.Random random = new Random();
for (int i = 1; i <= n; i++)
{
if (temp != -1)
{
random = new Random(i * temp * (int)DateTime.Now.Ticks);
}
int t = random.Next(varray.Length);
if (temp != -1 && temp == t)
{
return GetRandomNumString(n);
}
temp = t;
Vnum += varray[t];
}
return Vnum;
}