前几天有朋友问我怎么生成图片验证码,话不多说直接上代码。

支持.NET CORE开源。助力.NET Core社区发展。

 using System;
using System.IO;
using System.DrawingCore.Imaging;
using System.DrawingCore;
using CZFW.Framework.Model; namespace CZFW.Core.Security
{
/// <summary>
/// 图形验证码
/// </summary>
public class VerifyCode
{
public byte[] GetVerifyCode()
{
const int codeW = ;
const int codeH = ;
const int fontSize = ;
string chkCode = string.Empty;
//颜色列表,用于验证码、噪线、噪点
Color[] color = { Color.Black,Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
//字体列表,用于验证码
string[] font = { "Times New Roman" };
//验证码的字符集,去掉了一些容易混淆的字符
char[] character = { '', '', '', '', '', '', '', '', '', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//生成验证码字符串
for (int i = ; i < ; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
//写入Session、验证码加密
WebHelper.WriteSession("czfw_session_verifycode", DesEncrypt.Encrypt(chkCode.ToLower(), "MD5"));
//创建画布
Bitmap bmp = new Bitmap(codeW, codeH);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画噪线
for (int i = ; i < ; i++)
{
int x1 = rnd.Next(codeW);
int y1 = rnd.Next(codeH);
int x2 = rnd.Next(codeW);
int y2 = rnd.Next(codeH);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证码字符串
for (int i = ; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, fontSize);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * , );
}
//将验证码图片写入内存流,并将其以 "image/Png" 格式输出
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
catch (Exception)
{
return null;
}
finally
{
g.Dispose();
bmp.Dispose();
}
}
}
}

上面的是生成图片下面是在控制器中使用

  public ActionResult GetAuthCode()
{
return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
}

用File返回到web页面上就是一个验证码图片了。

Asp.Net Core 生成图形验证码的更多相关文章

  1. Net Core 生成图形验证码

    1. NetCore ZKweb       在我第一次绘制图形验证码时是采用的ZKweb的绘制库,奉上代码参考      public byte[] GetVerifyCode(out string ...

  2. C#生成图形验证码

    先看效果: 再上代码 public class CaptchaHelper { private static Random rand = new Random(); private static in ...

  3. PHP5 GD库生成图形验证码(汉字)

    PHP5 GD库生成图形验证码且带有汉字的实例分享. 1,利用GD库函数生成图片,并在图片上写指定字符imagecreatetruecolor 新建一个真彩色图像imagecolorallocate ...

  4. PHP5生成图形验证码(有汉字)

    利用PHP5中GD库生成图形验证码 类似于下面这样 1.利用GD库函数生成图片,并在图片上写指定字符 imagecreatetruecolor   新建一个真彩色图像      imagecolora ...

  5. ASP.NET Core 使用 Google 验证码(reCAPTCHA v3)代替传统验证码

    写在前面 友情提示: Google reCAPTCHA(v3下同) 的使用不需要"梯子",但申请账号的时候需要! Google reCAPTCHA 的使用不需要"梯子&q ...

  6. asp.net core 腾讯验证码的接入

    asp.net core 腾讯验证码的接入 Intro 之前使用的验证码服务是用的极验验证,而且是比较旧的,好久之前接入的,而且验证码服务依赖 Session,有点不太灵活,后来发现腾讯也有验证码服务 ...

  7. java生成图形验证码

    效果图 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.Buf ...

  8. ASP.NET中如何生成图形验证码

    通常生成一个图形验证码主要 有3个步骤: (1)随机产生一个长度为N的随机字符串,N的值可由开发可由开发人员自行设置.该字符串可以包含数字.字母等. (2)将随机生成的字符串创建成图片,并显示. (3 ...

  9. ASP.NET Core 使用 Google 验证码(Google reCAPTCHA)

    关心最多的问题,不FQ能不能用,答案是能.Google官方提供额外的域名来提供服务,国内可以正常使用. 一. 前言 验证码在我们实际的生活场景中非常常见,可以防止恶意破解密码.刷票.论坛灌水.刷注册等 ...

随机推荐

  1. Flink -- Java Generics Programming

    Flink uses a lot of generics programming, which is an executor Framework with cluster of executor ha ...

  2. 使用notebook 笔记(1)

    1 去开启远程访问notebook 注意事项 安装好Ipython notebook 之后,  开启服务的方式如下: ipython notebook --profile=nbserver --ip= ...

  3. Android学习——ViewPager的使用(一)

    这一节介绍使用ViewPager,加载ViewPager主要有三部分,数据源.适配器和ViewPager与适配器关联.其中数据源分为View对象和Fragment对象,这一节先来介绍View对象. 数 ...

  4. Siebel 集成中的“发布-订阅”与“阅读”

    将 Siebel 应用程序中存储的数据提供给企业中的其他应用程序时,通常需要遵循以下两种基本模式之一: 发布-订阅 阅读 “发布-订阅”是一种机制,根据该机制,一个系统(发布者)将更改或更新的数据提供 ...

  5. Oracle10g使用$ORACLE_HOME/rdbms/admin/awrrpt.sql报错

    Enter value for report_name: Using the report name awrrpt_1_591_593.htmlselect output from table(dbm ...

  6. Mrach 9 2017 Week 10 Thursday

    There is a kind of beauty in imperfection. 有一种美叫做不完美. Every thing, every one, in the world, is not p ...

  7. ABAP正则表达式 vs SPLIT INTO

    需求: 把如下通过"/"连接起来的三个字符串分别解析出来. 传统的做法见下图第98行的function module SKWF_UTIL_IO_FIND_BY_KEY: 这个fun ...

  8. Andriod ADB Interface驱动安装失败Configure USB Debug for Android

    介绍: Linux或Apple或OS X ,已经安装了USB驱动调试为Android的帮助,确认您的Android USB调试连接配置和正常工作. Windows下需要自己手动下载驱动安装或者通过下载 ...

  9. 二.Mybatis 增删改查

    Student.java package com.pojo; import java.util.Date; public class Student { int stuid; String stuNa ...

  10. 【luogu P2298 Mzc和男家丁的游戏】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2298 对于迷宫问题,bfs是比较好的选择. 直接bfs模板 #include <iostream> ...