C#生成图形验证码
先看效果:
再上代码
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#生成图形验证码的更多相关文章
- PHP5 GD库生成图形验证码(汉字)
PHP5 GD库生成图形验证码且带有汉字的实例分享. 1,利用GD库函数生成图片,并在图片上写指定字符imagecreatetruecolor 新建一个真彩色图像imagecolorallocate ...
- PHP5生成图形验证码(有汉字)
利用PHP5中GD库生成图形验证码 类似于下面这样 1.利用GD库函数生成图片,并在图片上写指定字符 imagecreatetruecolor 新建一个真彩色图像 imagecolora ...
- java生成图形验证码
效果图 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.Buf ...
- ASP.NET中如何生成图形验证码
通常生成一个图形验证码主要 有3个步骤: (1)随机产生一个长度为N的随机字符串,N的值可由开发可由开发人员自行设置.该字符串可以包含数字.字母等. (2)将随机生成的字符串创建成图片,并显示. (3 ...
- Net Core 生成图形验证码
1. NetCore ZKweb 在我第一次绘制图形验证码时是采用的ZKweb的绘制库,奉上代码参考 public byte[] GetVerifyCode(out string ...
- python 生成图形验证码
文章链接:https://mp.weixin.qq.com/s/LYUBRNallHcjnhJb1R3ZBg 日常在网站使用过程中经常遇到图形验证,今天准备自己做个图形验证码,这算是个简单的功能,也适 ...
- (转)Android 之生成图形验证码
import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; impor ...
- 【Java】生成图形验证码
本章介绍一个能生成比较好看的图形验证码类 生成验证码工具类 package com.util; import java.awt.Color; import java.awt.Font; import ...
- Asp.Net Core 生成图形验证码
前几天有朋友问我怎么生成图片验证码,话不多说直接上代码. 支持.NET CORE开源.助力.NET Core社区发展. using System; using System.IO; using Sys ...
随机推荐
- Asp.net的服务器推技术 (Server Push)
在以往的和服务器端通信技术中,我们多数使用的是AJAX轮询式访问,也就是在Javascript中控制时间间隔,然后每隔一段时间就访问一次服务器,然后获得数据或通知.但是这种轮询方式的访问有90%是在做 ...
- D3D Deferred Shading
在3D图形计算中,deferred shading是一个基于屏幕空间的着色技术.之所以被称为deferred shading,是因为我们将场景的光照计算与渲染"deferred"到 ...
- sql脚本太大无法打开的解决办法
在sqlcmd中执行脚本文件的方法有2种: 方法1.在DOS中,可以调用sqlcmd命令,并用选项-i传入想要执行的文件名: sqlcmd -S "这里改成你的服务器名称" -U ...
- 学生信息管理 --- c语言实现
第一次写比较大的程序,昨晚看了 大话数据结构 有感-----同样求 1+2+3+4+...+100,我则是简单的从1+2+3+4+...+100. 而不是 (1+100) / 2 * 100;(高 ...
- 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板
一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...
- iOS正则匹配手机号
#pragma 正则匹配手机号 + (BOOL)validateMobile:(NSString *)mobileNum { /** * 手机号码 * 移动:134[0-8 ...
- XCODE快捷键个人总结
1.在方法名上用CMD+左键 可以查看完整的方法名
- DB2 DATE类型在显示的时候,带有00:00:00,去掉的方法,使用VARCHAR()函数
DROP VIEW DMS.V_AQ_INSURANCECLAIMS; CREATE VIEW DMS.V_AQ_INSURANCECLAIMS AS SELECT * FROM (SELECT T1 ...
- http://blog.csdn.net/luxiaoyu_sdc/article/details/7333024
http://blog.csdn.net/luxiaoyu_sdc/article/details/7333024 http://blog.csdn.net/kkdelta/article/detai ...
- (转载)C++ ofstream和ifstream详细用法
原文出自[比特网],转载请保留原文链接:http://soft.chinabyte.com/database/460/11433960.sh [导读] ofstream是从内存到硬盘,ifstream ...