1 using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web; namespace CasCall
{
public class service_code : System.Web.UI.Page
{
#region
public string RandCode()
{
int CodeCount = ;//生成4个随机数
string randomChar = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";
randomChar = randomChar.Replace(",", "");
string RandomCode = "";
System.Threading.Thread.Sleep();
char[] allCharArray = randomChar.ToCharArray();
int n = allCharArray.Length;
System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int i = ; i < CodeCount; i++)
{
int rnd = random.Next(, n);
RandomCode += allCharArray[rnd];
}
return RandomCode;
}
#endregion
#region 验证码
/// <summary>
/// 创建验证码图片
/// </summary>
/// <param name="randomcode">验证码</param>
public byte[] CreateImage(string randomcode)
{
int randAngle = ; //随机转动角度
int mapwidth = (int)(randomcode.Length * );
Bitmap map = new Bitmap(mapwidth, );//创建图片背景
Graphics graph = Graphics.FromImage(map);
graph.Clear(Color.White);//清除画面,填充背景
//graph.DrawRectangle(new Pen(Color.Silver, 0), 0, 0, map.Width - 1, map.Height - 1);//画一个边框 Random rand = new Random(); //验证码旋转,防止机器识别
char[] chars = randomcode.ToCharArray();//拆散字符串成单字符数组
//文字距中
StringFormat format = new StringFormat(StringFormatFlags.NoClip);
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
//定义颜色
Color[] c = { Color.Black, Color.Red, Color.Blue, Color.Green,
Color.Orange, Color.Brown, Color.DarkBlue }; //画图片的背景噪音线
for (int i = ; i < ; i++)
{
int x1 = rand.Next();
int x2 = rand.Next(map.Width - , map.Width);
int y1 = rand.Next(map.Height);
int y2 = rand.Next(map.Height); graph.DrawLine(new Pen(c[rand.Next()]), x1, y1, x2, y2);
} for (int i = ; i < chars.Length; i++)
{
int cindex = rand.Next();
int findex = rand.Next();
Font f = new System.Drawing.Font("Arial", , System.Drawing.FontStyle.Regular);//字体样式(参数2为字体大小)
Brush b = new System.Drawing.SolidBrush(c[cindex]);
Point dot = new Point(, );
float angle = rand.Next(-randAngle, randAngle);//转动的度数
graph.TranslateTransform(dot.X, dot.Y);//移动光标到指定位置
graph.RotateTransform(angle);
graph.DrawString(chars[i].ToString(), f, b, , , format);
graph.RotateTransform(-angle);//转回去
graph.TranslateTransform(, -dot.Y);//移动光标到指定位置
}
//生成图片
System.IO.MemoryStream ms = new System.IO.MemoryStream();
MemoryStream stream = new MemoryStream();
map.Save(stream, ImageFormat.Jpeg);
graph.Dispose();
map.Dispose();
return stream.ToArray(); }
#endregion
}
}

前台

  <label class="panel-body">验证码:</label>
<input type="text" id="yzm" name="yzm" class="form-control" />&nbsp;&nbsp;</td>
<img id="ValidCode" style="cursor: pointer;" src="/SignIn/YanZhengMa" alt="验证码" onclick="this.src='/SignIn/YanZhengMa?time=' + new Date().getTime();" />
<a href="#" id="Changes" data-toggle="modal" data-target="#mymodel">看不清,换一张</a>
JS
$("#Changes").click(function () {
document.getElementById("ValidCode").src = '/SignIn/YanZhengMa?time=' + new Date().getTime();
});

  后台调用

  /// <summary>
/// 生成验证码
/// </summary>
/// <returns></returns>
public ActionResult YanZhengMa()
{
service_code sc = new service_code();
string vVerificationCode = sc.RandCode();
Session["vcode"] = vVerificationCode.ToLower();
// Safe.SessionAdd("vcode", vVerificationCode.ToLower(), 2); //Session中保存验证码
sc.CreateImage(vVerificationCode);
return File(sc.CreateImage(vVerificationCode), @"image/jpeg");
}

 效果图

C# 比较不错的通用验证码的更多相关文章

  1. 一个不错的php验证码的类

    类的代码: <?php class Captcha { private $width; private $height; private $codeNum; private $code; pri ...

  2. PHP之验证码识别

    首先推荐几篇有关验证码识别的文章,觉得不错 php实现验证码的识别(初级篇) 关于bp神经网格识别验证码 一.思路 碰见一个验证码,如果我们想要识别它,我们需要的是做什么呢? 我们先观察几个验证码.. ...

  3. python 豆瓣验证码识别总结

    总结:  pytesseract 识别比较标准的图片  识别成功率   还是不错的. 验证码的图片识别 需要先处理好   再用pytesseract 识别 from PIL import Image  ...

  4. 关于网易云验证码V1.0版本的服务介绍

    服务介绍 易盾验证码是一个用于区分人和机器的通用验证码组件.传统的字符型验证码由于存在破解率高,用户体验不友好等问题,已不适用于现今的互联网环境.易盾验证码抛弃了传统字符型验证码展示-填写字符-比对答 ...

  5. [Hei.Captcha] Asp.Net Core 跨平台验证码实现

    写在前面 说起来比较丢脸.我们有个手机的验证码发送逻辑需要使用验证码,这块本来项目里面就有验证码绘制逻辑,.Net Framework的,使用的包是System.Drawing,我把这验证码绘制逻辑复 ...

  6. 用python读写excel(xlrd、xlwt)

    最近需要从多个excel表里面用各种方式整理一些数据,虽然说原来用过java做这类事情,但是由于最近在学python,所以当然就决定用python尝试一下了.发现python果然简洁很多.这里简单记录 ...

  7. 在Visual Studio 2012中使用VMSDK开发领域特定语言(一)

    前言 本专题主要介绍在Visual Studio 2012中使用Visualization & Modeling SDK进行领域特定语言(DSL)的开发,包括两个部分的内容.在第一部分中,将对 ...

  8. 在Visual Studio 2012中使用VMSDK开发领域特定语言1

    在Visual Studio 2012中使用VMSDK开发领域特定语言(一)   前言 本专题主要介绍在Visual Studio 2012中使用Visualization & Modelin ...

  9. 加锁并发算法 vs 无锁并发算法

    Heinz Kabutz 在上周举办了一次成功 JCrete研讨会,我在会上参加了对一种新的 StampedLock(于JSR166中 引入) 进行的评审.StampedLock (邮戳锁) 旨在解决 ...

随机推荐

  1. java异常——捕获异常+再次抛出异常与异常链

    [0]README 0.1) 本文描述+源代码均 转自 core java volume 1, 旨在理解 java异常--捕获异常+再次抛出异常与异常链 的相关知识: [1]捕获异常相关 1.1)如果 ...

  2. Android JAVA中的时间大小比较

    import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...

  3. 微信开发之八 页面获取周围beacon设备

    原文链接:https://blog.csdn.net/qq_37936542/article/details/78912197 --注意:微信测试号不具备这个功能 步骤一:绑定域名   注意不要加ht ...

  4. PatentTips - Well bias control circuit

    BACKGROUND OF THE INVENTION The present invention relates to a semiconductor integrated circuit devi ...

  5. Ntp配置文件详解

    1. http://www.ine.com/resources/ntp-authentication.htm 2. http://blog.chinaunix.net/uid-773723-id-16 ...

  6. SQLServer重建索引

    Use [数据库名称]Go DECLARE @DBCCString NVARCHAR(1000)DECLARE @TableName VARCHAR(100)DECLARE Cur_Index CUR ...

  7. 【转】A* A星 算法 C语言 实现代码

    http://blog.csdn.net/shanshanpt/article/details/8977512 关于A*算法,很早就想写点什么,可是貌似天天在忙活着什么,可事实又没有做什么,真是浮躁啊 ...

  8. Spring mvc redirect跳转路径问题

    SpringMVC重定向视图RedirectView小分析 前言 SpringMVC是目前主流的Web MVC框架之一. 本文所讲的部分内容跟SpringMVC的视图机制有关,SpringMVC的视图 ...

  9. 如何在一个div中使其子div居中

    网上其他地方已讲述过对其的不同实现方式,今天主要做一个详细的汇总,希望对大家有帮助. ps:我面试的时候就被问到过这个问题,当时都回答错了,蓝瘦. 假设父div的类名为father,子div的类名为s ...

  10. [Django] Creating an app, models and database

    To add a new app, first cd to the project. Then run: python manage.py startapp scrumboard After that ...