62,268
社区成员
发帖
与我相关
我的任务
分享try
{
bmp.Save(context.Response.OutputStream, ImageFormat.Gif);
context.Session["xktinfo"] = chkCode.ToString(); //先保存在Session中,验证与用户输入是否一致
context.Response.End();
}
finally
{
//显式释放资源
bmp.Dispose();
g.Dispose();
}
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Gif);
context.Session["xktinfo"] = chkCode.ToString(); //先保存在Session中,验证与用户输入是否一致
context.Response.ClearContent();
context.Response.ContentType = "image/Png";
context.Response.BinaryWrite(ms.ToArray());
}
finally
{
//显式释放资源
bmp.Dispose();
g.Dispose();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;
using System.Web.SessionState;
namespace XKT.WebUI.ashx
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class validateimage : IHttpHandler, IRequiresSessionState,
{
/**//// <summary>
/// 生成验证图片核心代码
/// </summary>
/// <param name="hc"></param>
public void ProcessRequest(HttpContext context)
{
//设置输出流图片格式
context.Response.ContentType = "image/gif";
string chkCode = string.Empty;
//颜色列表,用于验证码、噪线、噪点
Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
//字体列表,用于验证码
string[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
//验证码的字符集,去掉了一些容易混淆的字符
char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//生成验证码字符串
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
Bitmap bmp = new Bitmap(100, 40);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画噪线
for (int i = 0; i < 10; i++)
{
int x1 = rnd.Next(bmp.Width);
int y1 = rnd.Next(bmp.Height);
int x2 = rnd.Next(bmp.Width);
int y2 = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证码字符串
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, 24);
Color clr = Color.Black;
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)4);
}
//画噪点
for (int i = 0; i < 100; i++)
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, bmp.Width - 1, bmp.Height - 1);
try
{
bmp.Save(context.Response.OutputStream, ImageFormat.Gif);
context.Session["xktinfo"] = chkCode.ToString(); //先保存在Session中,验证与用户输入是否一致
context.Response.End();
}
finally
{
//显式释放资源
bmp.Dispose();
g.Dispose();
}
}
/**//// <summary>
/// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
/// </summary>
public bool IsReusable
{
get
{
return true;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;
using System.Web.SessionState;
namespace XKT.WebUI.ashx
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class validateimage : IHttpHandler, IRequiresSessionState,
{
/**//// <summary>
/// 生成验证图片核心代码
/// </summary>
/// <param name="hc"></param>
public void ProcessRequest(HttpContext context)
{
//设置输出流图片格式
context.Response.ContentType = "image/gif";
string chkCode = string.Empty;
//颜色列表,用于验证码、噪线、噪点
Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
//字体列表,用于验证码
string[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
//验证码的字符集,去掉了一些容易混淆的字符
char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//生成验证码字符串
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
Bitmap bmp = new Bitmap(100, 40);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画噪线
for (int i = 0; i < 10; i++)
{
int x1 = rnd.Next(bmp.Width);
int y1 = rnd.Next(bmp.Height);
int x2 = rnd.Next(bmp.Width);
int y2 = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证码字符串
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, 24);
Color clr = Color.Black;
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)4);
}
//画噪点
for (int i = 0; i < 100; i++)
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, bmp.Width - 1, bmp.Height - 1);
try
{
bmp.Save(context.Response.OutputStream, ImageFormat.Gif);
context.Session["xktinfo"] = chkCode.ToString(); //先保存在Session中,验证与用户输入是否一致
context.Response.End();
}
finally
{
//显式释放资源
bmp.Dispose();
g.Dispose();
}
}
/**//// <summary>
/// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
/// </summary>
public bool IsReusable
{
get
{
return true;
}
}
}
}
public class login : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string username = context.Request["uname"];
string password = context.Request["upass"];
string checkcode = context.Request["code"];
if (checkcode!=context.Session["xktinfo"].ToString().ToLower())
{
context.Response.Write("checkcodeerror");
}
else
{
if (username == "admin")
{
context.Response.Write("ok");
}
else
{
context.Response.Write("no");
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}