.Net创建随机验证码附带图片

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;

namespace ZC.Common
{
    public class ValidateCode
    {
        public  static  string CreateValidateNumber(int length)
        {
            int[] randMembers = new int[length];
            int[] validateNums = new int[length];
            System.Text.StringBuilder validateNumberStr = new System.Text.StringBuilder();
            //生成起始序列值
            int seekSeek = unchecked((int)DateTime.Now.Ticks);
            Random seekRand = new Random(seekSeek);
            int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000);
            int[] seeks = new int[length];
            for (int i = 0; i < length; i++)
            {
                beginSeek += 10000;
                seeks[i] = beginSeek;
            }
            //生成随机数字
            for (int i = 0; i < length; i++)
            {
                Random rand = new Random(seeks[i]);
                int pownum = 1 * (int)Math.Pow(10, length);
                randMembers[i] = rand.Next(pownum, Int32.MaxValue);
            }
            //抽取随机数字
            for (int i = 0; i < length; i++)
            {
                string numStr = randMembers[i].ToString();
                int numLength = numStr.Length;
                Random rand = new Random();
                int numPosition = rand.Next(0, numLength - 1);
                validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
            }
            //生成验证码
            for (int i = 0; i < length; i++)
            {
                validateNumberStr.Append(validateNums[i].ToString());
            }
            return validateNumberStr.ToString();
        }

public static  byte[] CreateValidateGraphic(string validateNum)
        {
            using (Bitmap image = new Bitmap((int)Math.Ceiling(validateNum.Length * 12.5+10), 22))
            {
                using (Graphics g = Graphics.FromImage(image))
                {
                    //生成随机生成器
                    Random random = new Random();
                    //清空图片背景色
                    g.Clear(System.Drawing.Color.White);
                    //画图片的干扰线
                    for (int i = 0; i < 25; 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(System.Drawing.Color.Silver), x1, y1, x2, y2);
                    }
                    System.Drawing.Font font = new System.Drawing.Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
                    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
                     System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 1.2f, true);
                    g.DrawString(validateNum, font, brush, 3, 2);
                    //画图片的前景干扰点
                    for (int i = 0; i < 100; i++)
                    {
                        int x = random.Next(image.Width);
                        int y = random.Next(image.Height);
                        image.SetPixel(x, y, System.Drawing.Color.FromArgb(random.Next()));
                    }
                    //画图片的边框线
                    g.DrawRectangle(new Pen(System.Drawing.Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                    //保存图片数据
                    using (MemoryStream stream = new MemoryStream())
                    {
                        image.Save(stream, ImageFormat.Jpeg);
                        return stream.ToArray();
                    }
                }
            }
        }
    }
}

时间: 2024-10-03 05:31:00

.Net创建随机验证码附带图片的相关文章

随机验证码、图片验证码和邮箱发送用户验证码

随机验证码.图片验证码和邮箱发送用户验证码 一.随机验证码 随机验证码的生成比较简单一般在注册用户的时候与邮箱或者手机信息接口相结合实现发送验证码功能,随机验证码只需要使用python内置的random随机数函数,调用random模块:import random,具体实现代码块 #随机验证码 def authCode(): code = '' for i in range(6): current = random.randrange(0,6)#randrange随机生成0-6的数字,但不包括6

黑马day04 画一个汉字的随机验证码的图片

下面的程序详细介绍了如何画一个随机验证码发送到客户端即浏览器进行显示: 1.画随机验证码的代码: package cn.itheima.response; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import j

java实现随机验证码的图片

链接地址:http://blog.sina.com.cn/s/blog_407a68fc010006qo.html 1.一共需要2个常用java文件(RandomCode.java和RandomCodeCtrl.java): (a.)RandomCode.java是个普通的java文件:内容如下: import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedIm

python模块之PIL模块(生成随机验证码图片)

PIL简介 什么是PIL PIL:是Python Image Library的缩写,图像处理的模块.主要的类包括Image,ImageFont,ImageDraw,ImageFilter PIL的导入 首先需要安装一下pillow包 pip install pillow 然后就可以调用PIL里的类了 from PIL import Image from PIL import ImageFont from PIL import ImageDraw from PIL import ImageFilt

php实现动态随机验证码机制(CAPTCHA)

php实现动态随机验证码机制 验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自动区分计算机和人类的图灵测试)的缩写,是一种区分用户是计算机还是人的公共全自动程序.可以防止:恶意破解密码.刷票.论坛灌水,有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试,实际上用验证码是现在很多网站通行的方式,我们利用比较简易的方式实现了这个功能. 这个

python模块PIL-获取带噪点噪线的随机验证码

from PIL import Image from PIL import ImageDraw from PIL import ImageFont from io import BytesIO import random class ValidCodeImg: """ 生成一个经过降噪后的随机验证码的图片 :return 生成图片的bytes类型的数据 """ def __init__(self, width=175, height=35, co

php笔记之GD库图片创建/简单验证码

燕十八 公益PHP培训 课堂地址:YY频道88354001 学习社区:www.zixue.it php画图:比如说验证码,缩略图,加水印都要用到GD库,所以要开启gd2库,才能用 首先找到php.ini中extension=php_gd2.dll  去掉前边的分号,然后重启apache,创建图片就可以用了 /* 画图的一般步骤: 1.创建画布 2.创建颜料 3.画图或者写字 4.保存 5.销毁资源 */ //创建画布 $img=imagecreatetruecolor(300,200); //创

输出随机验证码图片

1 /** 2 * //输出随机验证码图片:CAPTCHA图像 3 */ 4 public class ServletDemo1 extends HttpServlet { 5 private static final long serialVersionUID = 1L; 6 7 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOEx

struts2生成随机验证码图片

之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: CreateImageAction: package com.xiaoluo.action; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedIm