using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web.UI.WebControls; public class ValidateCodeCreator
{
/// <summary>
/// 产生验证码
/// </summary>
/// <param name="codeLength">验证码长度</param>
/// <returns></returns>
public string CreateCode(int codeLength)
{
string so =
"1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
string[] strArr = so.Split(',');
string code = "";
Random rand = new Random();
for (int i = 0; i < codeLength; i++)
{
code += strArr[rand.Next(0, strArr.Length)];
}
return code;
} /// <summary>
/// 产生验证图片
/// </summary>
/// <param name="code"></param>
public byte[] CreateCodeImage(string code)
{
Bitmap image = new Bitmap(50, 20);
Graphics g = Graphics.FromImage(image);
try
{
WebColorConverter ww = new WebColorConverter();
g.Clear((Color) ww.ConvertFromString("#FAE264"));
Random random = new Random();
//画图片的背景噪音线
for (int i = 0; i < 12; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2);
}
Font font = new Font("Arial", 13, FontStyle.Bold | FontStyle.Italic);
LinearGradientBrush brush = new LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Gray, 1.2f, true);
g.DrawString(code, font, brush, 0, 0);
//画图片的前景噪音点
//for (int i = 0; i < 10; i++)
//{
// int x = random.Next(image.Width);
// int y = random.Next(image.Height);
// image.SetPixel(x, y, Color.White);
//}
//画图片的边框线
// g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Gif);
return ms.ToArray();
}
finally
{
g.Dispose();
image.Dispose();
}
}
}

  

C# 创建验证码图片的更多相关文章

  1. 基于PIL模块创建验证码图片

    def get_valid_img(request): # 方式2:基于PIL模块创建验证码图片 from PIL import Image, ImageDraw, ImageFont from io ...

  2. java web学习总结(九) -------------------通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  3. JavaWeb---总结(九)通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片  1 package gacl. ...

  4. javaweb学习总结(九)—— 通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  5. J2EE如何生成验证码图片和点击刷新验证码

    验证码图片生成步骤 创建BufferedImage对象. 获取BufferedImage的画笔,即调用getGraphics()方法获取Graphics对象. 调用Graphics对象的setColo ...

  6. java web 学习九(通过servlet生成验证码图片)

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  7. java web,生成验证码图片的技术

    偶然知道原来有些网站的验证码图片都是随机生成的,后来听人讲了一下,就做了这个小例子 生成图片,绘制背景,数字,干扰线用到了java.awt包,主要使用BufferedImage来生成图片,然后使用Gr ...

  8. Ajax动态刷新验证码图片

    一> 原理: 把用代码生成的图片存放到硬盘当中,然后在返回存储路径把图片通过图片标签的 src 属性 自动加载到浏览器中 二> 步骤 1. 首先用GDI+ 绘图 把验证码图片给绘制出来 2 ...

  9. 通过Servlet生成验证码图片

    原文出自:http://www.cnblogs.com/xdp-gacl/p/3798190.html 一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类, ...

随机推荐

  1. 微信企业号 出现redirect_uri unauthorized 50001 解决办法

    在企业号内获得用户信息时,需要对域名授权,如果不授权会提示: redirect_uri unauthorized  50001 错误. 通常,我们会在 输入我们的授权域名. 今天在企业号内又新建了一个 ...

  2. Carthage使用(cocoapods的替代)

    1.使用homebrew安装Carthage brew intsall carthage  Ps:没有安装Homebrew的话,进入传送门Homebrew.顺便提一句可以选择简体中文啊.   2.进入 ...

  3. MySQL 使用索引扫描来做排序

    MySQL有两种方式可以生成有序的结果:通过排序操作:或者按照索引顺序扫描:如果EXPLAIN 出来的结果的type列的值为“index”,则说明MySQL使用了索引扫描来做排序(不要和Extra列的 ...

  4. JavaScript 系列笔记(一)数据类型

    关于JS的数据类型 简单类型有五种:Undifined, Null, Boolean, Number, String 复杂类型有一种:Object 通过typeof 操作符来获取数据类型,此操作符返回 ...

  5. pyqt5按钮计数

    万事开头难,弄了好久才做了一个简单的小程序,点击按钮就显示数字,点一下,自增1. 首先用qt设计师设计一个窗体.标签名为label,按钮名为btn,然后存储为a.ui 在shell中用命令pyuic5 ...

  6. resumable.js —— 基于 HTML 5 File API 的文件上传组件 支持续传后台c#实现

    在git上提供了java.nodejs.c#后台服务方式:在这里我要用c#作为后台服务:地址请见:https://github.com/23/resumable.js 我现在visual studio ...

  7. PowerShell String对象方法 1

    PowerShell String对象方法 1 8 6月, 2013  在 Powershell tagged 字符串 / 对象 / 文本 by Mooser Lee 从之前的章节中,我们知道Powe ...

  8. TaintDroid:智能手机监控实时隐私信息流跟踪系统(四)

    6      应用程序研究 款流行的应用程序是怎么使用用户敏感数据的.选取的应用程序可以根据相应的权限通过Internet获得各种各样的用户数据.我们研究发现三分之二的这些数据暴露了用户详细的地理位置 ...

  9. BZOJ1116: [POI2008]CLO

    1116: [POI2008]CLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 565  Solved: 303[Submit][Status] ...

  10. bzoj 1196

    http://www.lydsy.com/JudgeOnline/problem.php?id=1196 二分+并查集 一共有2*M条路径,我们首先将这2*M条路径按费用排序. 然后二分最大费用的公路 ...