using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateCheckCodeImage(this.GenerateCheckCode());
}
private string GenerateCheckCode()
{
string str = string.Empty;
Random random = new Random();
for (int i = 0; i < 5 ; i++)
{
int num = random.Next();
str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
}
Response.Cookies.Add(new HttpCookie("ValidateCode", str));
return str;
}
private void CreateCheckCodeImage(string checkCode)
{
if ((checkCode ! = null) && (checkCode.Trim() ! = string.Empty))
{
Bitmap image = new Bitmap((int)Math.Ceiling(checkCode.Length * 12.5), 20);
Graphics graphics = Graphics.FromImage(image);
try
{
Random random = new Random();
graphics.Clear(Color.White);
for (int i = 0; i < 0x19; i++)
{
int num2 = random.Next(image.Width);
int num3 = random.Next(image.Width);
int num4 = random.Next(image.Height);
int num5 = random.Next(image.Height);
graphics.DrawLine(new Pen(Color.Silver), num2, num4, num3, num5);
}
Font font = new Font("Arial", 12f, FontStyle.Italic | FontStyle.Bold);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
graphics.DrawString(checkCode, font, brush, (float)2f, (float)2f);
for (int j = 0; j < 100; j++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
MemoryStream stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
base.Response.ClearContent();
base.Response.ContentType = "image/Gif" ;
base.Response.BinaryWrite(stream.ToArray());
}
finally
{
graphics.Dispose();
image.Dispose();
}
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateCheckCodeImage(this.GenerateCheckCode());
}
private string GenerateCheckCode()
{
string str = string.Empty;
Random random = new Random();
for (int i = 0; i < 5 ; i++)
{
int num = random.Next();
str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
}
Response.Cookies.Add(new HttpCookie("ValidateCode", str));
return str;
}
private void CreateCheckCodeImage(string checkCode)
{
if ((checkCode ! = null) && (checkCode.Trim() ! = string.Empty))
{
Bitmap image = new Bitmap((int)Math.Ceiling(checkCode.Length * 12.5), 20);
Graphics graphics = Graphics.FromImage(image);
try
{
Random random = new Random();
graphics.Clear(Color.White);
for (int i = 0; i < 0x19; i++)
{
int num2 = random.Next(image.Width);
int num3 = random.Next(image.Width);
int num4 = random.Next(image.Height);
int num5 = random.Next(image.Height);
graphics.DrawLine(new Pen(Color.Silver), num2, num4, num3, num5);
}
Font font = new Font("Arial", 12f, FontStyle.Italic | FontStyle.Bold);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
graphics.DrawString(checkCode, font, brush, (float)2f, (float)2f);
for (int j = 0; j < 100; j++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
MemoryStream stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
base.Response.ClearContent();
base.Response.ContentType = "image/Gif" ;
base.Response.BinaryWrite(stream.ToArray());
}
finally
{
graphics.Dispose();
image.Dispose();
}
}
}
}