博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
验证码生成 c#
阅读量:7098 次
发布时间:2019-06-28

本文共 3808 字,大约阅读时间需要 12 分钟。

1.验证码生成类

1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System.Drawing.Drawing2D; 5 using System.Drawing.Imaging; 6 using System.IO; 7 using System.Linq; 8 using System.Web; 9 using System.Web.UI.WebControls;10 11 namespace AuthenticationDemo.Common12 {13     public class ValidationCodeHelper14     {15         /// 16         /// 创建验证码17         /// 18         /// 验证码位数19         /// 
20 public string CreateCode(int codeLength)21 {22 // 不要 "0"23 string so = "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,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";24 string[] strArr = so.Split(',');25 string code = "";26 Random rand = new Random();27 for (int i = 0; i < codeLength; i++)28 {29 code += strArr[rand.Next(0, strArr.Length)];30 }31 return code;32 }33 /// 34 /// 产生图片35 /// 36 /// 验证码37 public byte[] CreateImage(string code)38 {39 Bitmap image = new Bitmap(64, 32);40 Graphics g = Graphics.FromImage(image);41 try42 {43 //随机转动角度 44 //int randAngle = 45;45 //实例随机生成器46 Random random = new Random();47 //清空图片背景色48 g.Clear(Color.White);49 //画图片的干扰线50 for (int i = 0; i < 25; i++)51 {52 int x1 = random.Next(image.Width);53 int x2 = random.Next(image.Width);54 int y1 = random.Next(image.Height);55 int y2 = random.Next(image.Height);56 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);57 }58 Font font = new Font("Arial", 16, (FontStyle.Bold | FontStyle.Italic));59 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),60 Color.Blue, Color.DarkRed, 1.2f, true);61 //float angle = random.Next(-randAngle,randAngle);62 //g.RotateTransform(angle);63 g.DrawString(code, font, brush, 3, 2);64 //g.RotateTransform(angle);65 //画图片的前景干扰点66 for (int i = 0; i < 100; i++)67 {68 int x = random.Next(image.Width);69 int y = random.Next(image.Height);70 image.SetPixel(x, y, Color.FromArgb(random.Next()));71 }72 //画图片的边框线73 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);74 //保存图片数据75 MemoryStream ms = new MemoryStream();76 image.Save(ms, ImageFormat.Jpeg);77 //输出图片流78 return ms.ToArray();79 }80 catch (Exception)81 {82 throw;83 }84 finally85 {86 g.Dispose();87 image.Dispose();88 }89 }90 91 }92 }

2.后台调用

public ActionResult GetVCode()        {            ValidationCodeHelper vc = new ValidationCodeHelper();            //验证码            string code = vc.CreateCode(4);            Session["vCode"] = code;//保存生成的验证码,提供之后与用户的输入的验证码比较            byte[] bytes = vc.CreateImage(code);            return File(bytes, @"image/jpeg");        }

3.html页面

html代码: 验证码图片 换一张 js代码:

function changeOne() {

var image = document.getElementById('vCodeImage');
image.src = "@Url.Action("GetVCode","Account")/?t=" + (new Date()).getTime();
}

 

转载于:https://www.cnblogs.com/nzgbk/p/7459001.html

你可能感兴趣的文章
Ubuntu 12.04 安装Scrapy爬虫框架
查看>>
selenium在执行浏览器最大化方法的时候报错
查看>>
[Meteor] meteor project structure
查看>>
用优先级队列实现先进先出队列;
查看>>
ROS + Kinect2 跑ORB_SLAM2 安装步骤记录
查看>>
tomcat 7/8 启动非常慢的解决方法
查看>>
Activex--ocx包装成cab全过程
查看>>
java 调用webservice的各种方法总结
查看>>
iptables从入门到精通
查看>>
浅谈http请求数据分析
查看>>
利用html5压缩图片,产出base64图片
查看>>
2018.12.3 区块链论文翻译
查看>>
JAVA 简单线程池区别
查看>>
面向对象2
查看>>
购物商城Web开发第十六天
查看>>
配置sonar、jenkins进行持续审查
查看>>
body / documentElement
查看>>
Dom4J__ZZ_我的示例代码
查看>>
C#编码标准66条
查看>>
初识HTML
查看>>