using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.IO; /// <summary> /// 生成验证码的类 /// </summary> public class ValidateCode { public ValidateCode() { } /// <summary> /// 验证码的最大长度 /// </summary> public int MaxLength { get { return 10; } } /// <summary> /// 验证码的最小长度 /// </summary> public int MinLength { get { return 1; } } /// <summary> /// 生成验证码 /// </summary> /// <param name="length">指定验证码的长度</param> /// <returns></returns> public string CreateValidateCode(int length) { if (length < 1) { length = 1; } if (length > 10) { length = 10; } string[] sourceCode = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; Random r = new Random(unchecked((int)DateTime.Now.Ticks)); string codes = ""; for (int i = 0; i < length; i++) { int l = r.Next(0, 35); codes += sourceCode[l]; } return codes; } /// <summary> /// 创建验证码的图片 /// </summary> /// <param name="containsPage">要输出到的page对象</param> /// <param name="validateNum">验证码</param> public byte[] CreateValidateGraphic(string validateCode) { Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 15.0), 24); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的干扰线 for (int i = 0; i < 25; 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.Silver), x1, y1, x2, y2); } Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString(validateCode, font, brush, 3, 2); //画图片的前景干扰点 for (int i = 0; i < 100; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //画图片的边框线 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); //保存图片数据 MemoryStream stream = new MemoryStream(); image.Save(stream, ImageFormat.Jpeg); //输出图片流 return stream.ToArray(); } finally { g.Dispose(); image.Dispose(); } } /// <summary> /// 得到验证码图片的长度 /// </summary> /// <param name="validateNumLength">验证码的长度</param> /// <returns></returns> public static int GetImageWidth(int validateNumLength) { return (int)(validateNumLength * 13.0); } /// <summary> /// 得到验证码的高度 /// </summary> /// <returns></returns> public static double GetImageHeight() { return 22.5; } } ====================ashx================ public class ashxGetCode : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; ValidateCode vc = new ValidateCode(); string code = vc.CreateValidateCode(4); byte[] result = vc.CreateValidateGraphic(code); context.Response.BinaryWrite(result); } public bool IsReusable { get { return false; } } } =============mvc======== public ActionResult GetValidateCode() |
ashx与验证码
时间: 2024-10-08 10:19:11
ashx与验证码的相关文章
asp.net中ashx生成验证码代码放在Linux(centos)主机上访问时无法显示问题
最近有个项目加入了验证码功能,就从自己博客以前的代码中找到直接使用,直接访问验证码页面报错如下: 源代码:asp.net中使用一般处理程序生成验证码 Application Exception System.ArgumentException The requested FontFamily could not be found [GDI+ status: FontFamilyNotFound] Description: HTTP 500.Error processing request. De
用Session实现验证码
新建一个 ashx 一般处理程序 如: YZM.ashx继承接口 IRequiresSessionState //在一般处理程序里面继承 HttpContext context 为请求上下文,包含此次请求处理要使用到的信息和对象都在里面,有Response,有Request 下面为 YZM.ashx网页内容: public class YZM : IHttpHandler,System.Web.SessionState.IRequiresSessionState { public void Pr
C# WinForm 上传图片,文件到服务器的方法Uploader.ashx
网上有很多方案,起初用时,因为对asp.net不太了解,觉得FTP实现不错,可是后来发现,如果机器在域控下,就会有问题. 一年过去了,asp.net也熟悉了,知道ajax没事应该用ashx,验证码也用ashx,当然这里要说的WinForm上传也应该是ashx了吧,哈哈,先提供简单思路: 接收文件的asp.net是:Uploader.ashx,相关代码: view plaincopy to clipboardprint? <%@ WebHandler Language="C#" C
验证码在页面中的使用
记得前几天刚学习了怎么样写一个验证码,但是只是创建了一个验证码并没有把它使用在登录或者注册页面中,嘿嘿,所以一直都在想着这个问题,想尽快的解决下,而就是今天下午意外一些事情没有课,就把这个重新的总结一下,似乎好多友友说验证码百度上面搜索好多可以拿来使用的,但是我现在是一个学习的状态,应该以一种学习的态度好好总结一下它,下面就总结一下我理解上的验证码的使用. 验证码的创建以及使用 public class ValicateCode : IHttpHandler, IRequiresSessionS
一般处理程序结合gdi生成简单验证码
using (Bitmap bitmap=new Bitmap(100,40)) { using (Graphics g=Graphics.FromImage(bitmap)) { //生成验证码string s = code(); g.DrawString(s, new Font("黑体", 20), Brushes.Red, 0, 0); bitmap.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFo
XX管理系统案例
一.登录界面建立登录文件夹Login,在此目录下面建立如下文件:Index.htm:登录页面ValidateCode.cs:生成验证码ProcessVerification.ashx:处理验证码CommonHelper.cs:帮助处理类Login.ashx:处理登录 Index.htm:登录页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xht
asp.net一般处理程序(.ashx)动态生成验证码案例。
{使用一般处理程序动态生成验证码} 1.新建WebSite项目,添加一般处理程序命名为 yzm.ashx,添加如下代码: public void ProcessRequest(HttpContext context) { //将context.Response.ContentType = "text/plain";修改为context.Response.ContentType = "image/JPEG"; context.Response
ASP.NET ashx实现无刷新页面生成验证码
现在大部分网站登陆时都会要求输入验证码,在网上也看了一些范例,现在总结一下如何实现无刷新页面生成验证码. 效果图: 实现方式: 前台: 1 <div> 2 <span>Identifying Code:</span> 3 <asp:TextBox ID="txtValidationCode" runat="server" Width="130px" MaxLength="4">&
随机验证码.ashx
ValidateCode.ashx <%@ WebHandler Language="C#" class="ValidateCode" %> using System;using System.Collections;using System.Data;using System.Linq;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using