C#中文验证码
来源:互联网 发布:xp无法找到网络打印机 编辑:程序博客网 时间:2024/05/22 17:45
public class ValidateCode : IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { string checkCode = GenCode(4); // 产生4位随机字符 context.Session["Code"] = checkCode; //将字符串保存到Session中,以便需要时进行验证 context.Session.Timeout = 1; System.Drawing.Bitmap image = new System.Drawing.Bitmap(100, 35);//图片大小 Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); // 画图片的背景噪音线 int i; Font font = new System.Drawing.Font("楷书", 20, (System.Drawing.FontStyle.Bold)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.Black, 1.2F, true); for (i = 0; i < checkCode.Length; i++) { g.DrawString(checkCode[i].ToString(), font, brush,i*20 , 2); } for (i = 0; i < 7; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.White,2), x1, y1, x2, y2); } // g.DrawString(checkCode, font, brush, 1, 1); //画图片的前景噪音点 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); context.Response.ClearContent(); context.Response.ContentType = "image/Gif"; context.Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } } /// <summary> /// 产生随机字符串 /// </summary> /// <param name="num">随机出几个字符</param> /// <returns>随机出的字符串</returns> private string GenCode(int num) { //string str = "0123456789ABCDEFGHiJKLMNOPQRSTUVWXYZ";//图片上随机文字 string str = "寒蝉凄切对长亭晚骤雨初歇都门帐饮无绪留恋处兰舟催发执手相看泪眼竟无语凝噎念去去千里烟波暮霭沉沉楚天阔多情自古伤离别更那堪冷落清秋节今宵酒醒何处杨柳岸晓风残月此去经年应是良辰好景虚设便纵有千种风情更与何人说好景一作美景";//图片上随机文字 char[] chastr = str.ToCharArray(); string code = ""; Random rd = new Random(); int i; for (i = 0; i < num; i++) { code += str.Substring(rd.Next(0, str.Length), 1); } return code; } public bool IsReusable { get { return false; } } }
0 0
- C#中文验证码
- c# 生成中文验证码
- C#生成中文验证码
- 用C#生成中文汉字验证码
- C#生成随机中文汉字验证码
- C#生成中文汉字验证码源码
- C#生成中文汉字验证码源码
- C#生成中文汉字验证码源码
- C#生成中文汉字验证码源码
- C#生成中文汉字验证码源码
- 用C#实现中文验证码
- C#生成中文汉字验证码源码
- C#生成中文汉字验证码源码
- C#生成中文汉字验证码源码
- C#随机中文汉字验证码
- 用C#实现中文验证码
- 用C#实现中文验证码
- c# winform 生成中文验证码
- 测试工具对比汇总
- UVALive 3667 dfs
- DES加密算法
- 函数模板
- AppScan的使用说明
- C#中文验证码
- x264 杂记
- Jsp运行原理
- Microsoft VC++ runtime installer 问题解决
- screen命令——会话分离
- SwipeListView删除item保持列表位置不变
- XUtils网络请求,图片加载(适合新手)。
- 自动调整linux系统时间和时区与Internet时间同步
- redis修改密码