C# 图片验证码简单例子
C# 图片验证码简单例子
一、简述
记--使用PictureBox绘制验证码。
例子:外链:https://wwm.lanzouq.com/b0cafckej 密码:cxxn
二、效果
三、工程结构
四、源文件
Form1.cs文件
using System;
using System.Drawing;
using System.Windows.Forms;namespace VerificationCode
{public partial class Form1 : Form{const int mRandomCharCount = 4;string mVerificationCode = "";public Form1(){InitializeComponent();}private void RefreshVerificationCode(){mVerificationCode = "";Random rdm = new Random();string[] fonts = { "微软雅黑", "宋体", "黑体", "隶书", "仿宋" };Color[] colors = { Color.Pink, Color.Blue, Color.Black, Color.Red, Color.Purple, Color.Green };const string randomStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//随机生成4个字符for (int i = 0; i < mRandomCharCount; i++){mVerificationCode += randomStr[rdm.Next(0, randomStr.Length)].ToString();}//4个字符按不同的字体颜色进行绘制Bitmap bmp = new Bitmap(pictureBoxVerificationCode.Width, pictureBoxVerificationCode.Height);Graphics g = Graphics.FromImage(bmp);for (int i = 0; i < mRandomCharCount; i++){Point p = new Point(i * 27, 0);g.DrawString(mVerificationCode[i].ToString(), new Font(fonts[rdm.Next(0, fonts.Length)], 28, FontStyle.Bold), new SolidBrush(colors[rdm.Next(0, colors.Length)]), p);}//绘制17条线条for (int i = 0; i < 16; i++){Point p1 = new Point(rdm.Next(0, bmp.Width), rdm.Next(0, bmp.Height));Point p2 = new Point(rdm.Next(0, bmp.Width), rdm.Next(0, bmp.Height));g.DrawLine(new Pen(Brushes.Green), p1, p2);}//绘制300个点for (int i = 0; i < 300; i++){Point p = new Point(rdm.Next(0, bmp.Width), rdm.Next(0, bmp.Height));bmp.SetPixel(p.X, p.Y, Color.Black);}Console.WriteLine(mVerificationCode);//将图片镶嵌到PictureBox中pictureBoxVerificationCode.Image = bmp;}private void pictureBoxVerificationCode_Click(object sender, EventArgs e){//刷新验证码RefreshVerificationCode();}private void buttonSure_Click(object sender, EventArgs e){//if (textBoxVerificationCode.Text == mVerificationCode) //区分大小写if (textBoxVerificationCode.Text.Equals(mVerificationCode, StringComparison.OrdinalIgnoreCase)) //不s区分大小写{MessageBox.Show("验证码正确!", "提示");}else{MessageBox.Show("验证码错误!", "错误");RefreshVerificationCode();textBoxVerificationCode.Text = "";}}private void Form1_Load(object sender, EventArgs e){RefreshVerificationCode();}}
}
C# 图片验证码简单例子相关推荐
- php验证码大全(实例分享),php图片验证码的例子
代码如下: /** * 生成验证码图片 * site www.jbxue.com * @param String $word 验证码在session中的变量名称 */ function valiCod ...
- 图片上传(加水印、缩略图、远程保存)的简单例子
图片上传(加水印.缩略图.远程保存)的简单例子(应用于51aspx.com) 该源码下载地址:http://51aspx.com/CV/ImageUpload 今天看到xiongeee发的文章使用使用 ...
- java 验证码图片识别_JavaSE图像验证码简单识别程序详解
本文为大家分享了JavaSE图像验证码简单识别程序,供大家参考,具体内容如下 首先你应该对图片进行样本采集,然后将样本进行灰度处理,也就是变成黑白两色. 然后你就可以使用该类,对目标文件进行分析.具体 ...
- eoLinker-API_Shop_验证码识别与生成类API调用的代码示例合集:六位图片验证码生成、四位图片验证码生成、简单验证码识别等...
以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 六位图片验证码生成:包括纯数字.小写字母.大写字母.大小写混合.数 ...
- php设计验证码图片,php实现简单的图片验证码
这是最简单的图片验证码: image.php header("Content-type: image/png"); $string = "abcdefghijklmnop ...
- php 简单图片验证码,PHP 实现简单图片验证码
验证码是网站会员系统中不可缺少的,目前验证码有很多种,但用的比较多的还是图片验证码,这里就用面向对象的方式来简单实现图片验证码, 注意!我这里使用的是 PHP 的 gd 库,如果要查看是否启用了 gd ...
- Android生成简单的图片验证码
暑假的班级项目登录界面用到了验证码,就想着简单点弄个图片验证码好了.看了几篇文章还是有点儿懵.然后耐着性子把一篇关于验证码的文章分析了一下.果然还是得有耐心才行啊. ╰( ̄ω ̄o) 虽然大致是做出来了 ...
- Django之头像实时展示到input框、图片验证码、简单发邮件
一.以注册功能来看头像实时展示 首先先来写一个简单的页面: <div class="container-fluid"><div class="row&q ...
- 教你将python画的图片编程保存(附简单例子)
介绍 python画图用的是matplotlib库,而且,保存一般也用这个库的函数savefig.将图片保存为jpg,pdf等均可. import matplotlib.pyplot as pltpl ...
- python—简单数据抓取四(利用超级鹰的ocr识别图片验证码模拟登录超级鹰网站、利用百度云的ocr识别自如租房网价格图片获取到自如网的价格)
学习目标: python学习二十四 -简单数据抓取四 学习内容: 1.利用超级鹰的ocr识别图片验证码模拟登录超级鹰网站 2.利用百度云的ocr识别自如租房网的价格图片,获取到自如网的价格数据 1.利 ...
最新文章
- html5 video speed control插件,Video Speed Controller
- 6-tips-for-managing-property-files-with-spring--转
- VS和matlab混合编程的推荐书籍!
- 平台电商类的增长策略:从用户激励到养成类游戏
- Python基础数据类型---bytes
- netty单片机通讯_SSM+Netty实现软硬件通信,真实项目案例
- Android Java和JavaScript互调
- 引导修复 不是活动的_河南省视频数据修复中心
- 【WPF学习笔记】[转]周银辉之WPF中的动画 晓风影天之wpf动画——new PropertyPath属性链...
- 论文阅读 - Video Swin Transformer
- 2021年中国移动游戏行业深度洞察报告
- JUNIT Hello World
- int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
- 人工智能在生活中的应用都有哪些?
- L298N模块详细使用教程
- 案例1-合并2个不同文件夹中的csv文件到另外一个目录,相同的文件名进行数据合并,不同的文件名直接移到新文件夹...
- 阿兰图灵和冯诺依曼,谁才是可称得起计算机之父呢?
- 普通期刊论文发表录用时间一般要多久
- 函数sum计算机怎么使用方法,sumif函数的使用方法
- 程序员如何提升英语水平