先看效果:

再上代码

public class CaptchaHelper
{
private static Random rand = new Random();
private static int previousAngle = 0; /// <summary>
/// 生成图形验证码
/// </summary>
/// <returns></returns>
public static byte[] Create(int codeLength, int width, int height, int fontSize, out String code)
{
String sCode = String.Empty;
//可用颜色列表
Color[] oColors = {
Color.FromArgb(118, 199, 192),
Color.FromArgb(97, 216, 206),
Color.FromArgb(84, 176, 168),
Color.FromArgb(117, 226, 217),
Color.FromArgb(43, 205, 191)
//Color.Red,
//Color.Blue,
//Color.Green,
//Color.Orange,
//Color.Brown,
//Color.Brown,
//Color.DarkBlue
};
//可用字体列表
string[] oFontNames = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
//可用字符列表
char[] oCharacter = {
'2','3','4','5','6','8','9',
'A','B','C','D','E','F','G','H','J','K','L','M','N','P','R','S','T','W','X','Y'
};
//Random oRnd = new Random();
Bitmap oBmp = null;
Graphics oGraphics = null;
int N1 = 0;
Point oPoint1 = default(Point);
Point oPoint2 = default(Point);
string sFontName = null;
Font oFont = null;
Color oColor = default(Color); //生成验证码字符串
for (N1 = 0; N1 <= codeLength - 1; N1++)
{
sCode += oCharacter[rand.Next(oCharacter.Length)];
} oBmp = new Bitmap(width, height);
oGraphics = Graphics.FromImage(oBmp);
oGraphics.Clear(Color.White);
try
{
for (N1 = 0; N1 <= 2; N1++)
{
//画噪线
oPoint1.X = rand.Next(width);
oPoint1.Y = rand.Next(height);
oPoint2.X = rand.Next(width);
oPoint2.Y = rand.Next(height);
oColor = oColors[rand.Next(oColors.Length)];
oGraphics.DrawLine(new Pen(oColor), oPoint1, oPoint2);
} float spaceWith = 0, dotX = 0;
if (codeLength != 0)
{
spaceWith = (width - fontSize * codeLength - 10) / codeLength;
} for (N1 = 0; N1 <= sCode.Length - 1; N1++)
{
//画验证字符串
sFontName = oFontNames[rand.Next(oFontNames.Length)];
oFont = new Font(sFontName, fontSize, FontStyle.Bold);
oColor = oColors[rand.Next(oColors.Length)]; dotX = Convert.ToSingle(N1) * fontSize + (N1 + 1) * spaceWith;
DrawCharacter(sCode[N1].ToString(), oGraphics, oFont, new SolidBrush(oColor), (int)dotX, fontSize, height);
} for (int i = 0; i <= 30; i++)
{
//画噪点
int x = rand.Next(oBmp.Width);
int y = rand.Next(oBmp.Height);
Color clr = oColors[rand.Next(oColors.Length)];
oBmp.SetPixel(x, y, clr);
} code = sCode;
//保存图片数据
MemoryStream stream = new MemoryStream();
oBmp.Save(stream, ImageFormat.Jpeg);
//输出图片流
return stream.ToArray();
}
finally
{
oGraphics.Dispose();
}
} private static void DrawCharacter(string txt, Graphics gr, Font font, Brush brush, int x, int charWidth, int height)
{
// 文字居中
using (StringFormat stringFormat = new StringFormat())
{
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
RectangleF rectf = new RectangleF(x, 0, charWidth, height); // 转换为 Path
using (GraphicsPath graphicsPath = new GraphicsPath())
{
graphicsPath.AddString(txt, font.FontFamily, (int)(font.Style), font.Size, rectf, stringFormat); // 旋转一个随机角度
float dx = (float)(x + charWidth / 2);
float dy = (float)(height / 2);
gr.TranslateTransform(-dx, -dy, MatrixOrder.Append);
int angle = previousAngle;
do
{
angle = rand.Next(-30, 30);
} while (Math.Abs(angle - previousAngle) < 20);
previousAngle = angle;
gr.RotateTransform(angle, MatrixOrder.Append);
gr.TranslateTransform(dx, dy, MatrixOrder.Append); // 画文字
gr.FillPath(brush, graphicsPath);
gr.ResetTransform();
}
}
}
}

  

C#生成图形验证码的更多相关文章

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

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

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

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

  3. java生成图形验证码

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

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

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

  5. Net Core 生成图形验证码

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

  6. python 生成图形验证码

    文章链接:https://mp.weixin.qq.com/s/LYUBRNallHcjnhJb1R3ZBg 日常在网站使用过程中经常遇到图形验证,今天准备自己做个图形验证码,这算是个简单的功能,也适 ...

  7. (转)Android 之生成图形验证码

    import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; impor ...

  8. 【Java】生成图形验证码

    本章介绍一个能生成比较好看的图形验证码类 生成验证码工具类 package com.util; import java.awt.Color; import java.awt.Font; import ...

  9. Asp.Net Core 生成图形验证码

    前几天有朋友问我怎么生成图片验证码,话不多说直接上代码. 支持.NET CORE开源.助力.NET Core社区发展. using System; using System.IO; using Sys ...

随机推荐

  1. linux gcc loudong

    五事九思 (大连Linux主机维护) 大连linux维护qq群:287800525 首页 日志 相册 音乐 收藏 博友 关于我     日志       spcark_0.0.3_i386.src.t ...

  2. ASP.NET 4.5新特性WebAPI从入门到精通

    在新出的MVC4中,增加了WebAPI,用于提供REST风格的WebService,新生成的WebAPI项目和典型的MVC项目一样,包含主要的Models.Views.Controllers等文件夹和 ...

  3. HDU4916 Count on the path(树dp??)

    这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...

  4. POJ 1870 Bee Breeding(找规律)

    题目链接 题意 : 给你一个蜂巢状图形,让你找出两个点之间的距离. 思路 : 在做这个题之前可以看一下2265,因为是一种题来着,规律就是我在2265里写的那样,然后就是求距离了,求距离的时候只需考虑 ...

  5. HBase保存的各个字段意义解释

    /×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...

  6. Android /data/data/app_file/目录下面安装apk无权限问题

    当识别SDCard的时候 String filePath = null; String state = Environment.getExternalStorageState(); if (state ...

  7. Good Bye 2014 D. New Year Santa Network 图论+期望

    D. New Year Santa Network   New Year is coming in Tree World! In this world, as the name implies, th ...

  8. Project Euler 110:Diophantine reciprocals II 丢番图倒数II

    Diophantine reciprocals II In the following equation x, y, and n are positive integers. For n = 4 th ...

  9. 一些时间的概念与区分(UTC、GMT、LT、TAI等)

    UT - 世界时 Universal Time世界时是最早的时间标准.在1884年,国际上将1s确定为全年内每日平均长度的1/8.64×104.以此标准形成的时间系统,称为世界时,即 UT1.1972 ...

  10. Why you have so few friends?

    Why you have so few friends?十个原因告诉你:为什么你的朋友那么少1. You Complain A Lot 你总是抱怨 If you’re constantly compl ...