public void ProcessRequest(HttpContext context)
{
string checkcode = Gent(5);
context.Session["yzm"] = checkcode;
Bitmap bit = new Bitmap(70, 22);
Graphics gra = Graphics.FromImage(bit);
try
{
Random random = new Random();
gra.Clear(Color.White);
//干扰线
for (int i = 0; i < 20; i++)
{
int x1 = random.Next(bit.Width);
int x2 = random.Next(bit.Width);
int y1 = random.Next(bit.Height);
int y2 = random.Next(bit.Height);
gra.DrawLine(new Pen(Color.Red), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold|FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, bit.Width, bit.Height), Color.Blue, Color.DarkRed, 1.2f, true);
gra.DrawString(checkcode, font, brush, 2, 2);
gra.DrawRectangle(new Pen(Color.Silver), 0, 0, bit.Width - 1, bit.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bit.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
context.Response.ClearContent();
context.Request.ContentType = "image/Gif";
context.Response.BinaryWrite(ms.ToArray());
}
catch (Exception)
{
throw;
}
finally
{
bit.Dispose();
gra.Dispose();
}
}
private string Gent(int num)
{
string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] chars = str.ToCharArray();
string code = "";
Random ran = new Random();
for (int i = 0; i < num; i++)
{
code += str.Substring(ran.Next(0, str.Length), 1);
}
return code;
{
string checkcode = Gent(5);
context.Session["yzm"] = checkcode;
Bitmap bit = new Bitmap(70, 22);
Graphics gra = Graphics.FromImage(bit);
try
{
Random random = new Random();
gra.Clear(Color.White);
//干扰线
for (int i = 0; i < 20; i++)
{
int x1 = random.Next(bit.Width);
int x2 = random.Next(bit.Width);
int y1 = random.Next(bit.Height);
int y2 = random.Next(bit.Height);
gra.DrawLine(new Pen(Color.Red), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold|FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, bit.Width, bit.Height), Color.Blue, Color.DarkRed, 1.2f, true);
gra.DrawString(checkcode, font, brush, 2, 2);
gra.DrawRectangle(new Pen(Color.Silver), 0, 0, bit.Width - 1, bit.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bit.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
context.Response.ClearContent();
context.Request.ContentType = "image/Gif";
context.Response.BinaryWrite(ms.ToArray());
}
catch (Exception)
{
throw;
}
finally
{
bit.Dispose();
gra.Dispose();
}
}
private string Gent(int num)
{
string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] chars = str.ToCharArray();
string code = "";
Random ran = new Random();
for (int i = 0; i < num; i++)
{
code += str.Substring(ran.Next(0, str.Length), 1);
}
return code;
}
前台调用点击事件function
var imgas = document.getElementById("");
if (imgas != null) {
imgas.src = imgas.src + "?";