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

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

/// /// 生成验证码图片/// public void CreateCheckCodeImage(){string checkCode = VerficationCodeSrc(5);if (checkCode == null || checkCode.Trim() == String.Empty)return;//string checkCode = "asfd2";Bitmap image = new Bitmap(90, 28);Graphics g = Graphics.FromImage(image);try{//生成随机生成器 Random random = new Random();//清空图片背景色 g.Clear(Color.White);//画图片的背景噪音线 for (int i = 0; i < 2; 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.Black), x1, y1, x2, y2);}Font font = new Font("Arial", 16, (FontStyle.Bold));System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);g.DrawString(checkCode, font, brush, 2, 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);System.IO.MemoryStream ms = new System.IO.MemoryStream();image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);Response.ClearContent();Response.ContentType = "image/Gif";Response.BinaryWrite(ms.ToArray());}finally{g.Dispose();image.Dispose();}}/// /// 生成验证码字符串/// /// /// 
private string VerficationCodeSrc(int numberOfChars){if (numberOfChars > 36){throw new InvalidOperationException("Random Word Charecters can not be greater than 36.");}char[] columns = new char[36];//字母for (int charPos = 97; charPos < 97 + 26; charPos++)columns[charPos - 97] = (char)charPos;//数字for (int intPos = 48; intPos <= 57; intPos++)columns[intPos - 22] = (char)intPos;StringBuilder randomBuilder = new StringBuilder();Random randomSeed = new Random();for (int incr = 0; incr < numberOfChars; incr++){randomBuilder.Append(columns[randomSeed.Next(36)].ToString());}return randomBuilder.ToString();}

 

转载于:https://www.cnblogs.com/LiuFengH/p/10915980.html

你可能感兴趣的文章
solrCloud+tomcat+zookeeper集群配置
查看>>
/etc/fstab,/etc/mtab,和 /proc/mounts
查看>>
Apache kafka 简介
查看>>
socket通信Demo
查看>>
技术人员的焦虑
查看>>
js 判断整数
查看>>
建设网站应该考虑哪些因素
查看>>
mongodb $exists
查看>>
js实现页面跳转的几种方式
查看>>
sbt笔记一 hello-sbt
查看>>
常用链接
查看>>
pitfall override private method
查看>>
!important 和 * ----hack
查看>>
聊天界面图文混排
查看>>
控件的拖动
查看>>
svn eclipse unable to load default svn client的解决办法
查看>>
Android.mk 文件语法详解
查看>>
QT liunx 工具下载
查看>>
内核源码树
查看>>
Java 5 特性 Instrumentation 实践
查看>>