ASP.NET图形验证码的生成

效果:

调用方法:

int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列
string vcode = QAPI.VerifImage.CreateVCode(r, 4);//产生验证码字符
pictureBox1.Image = QAPI.VerifImage.CreateVerifImage(vcode, r, true, true, TextRenderingHint.AntiAlias);//生成
//-----ASP.NET中上面最后一句改成:
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
Response.ContentType = "image/png";

源码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace QAPI
{
    class VerifImage
    {
        [DllImport("kernel32.dll")]
        private static extern void CopyMemory(int[] Destination, byte[] add, int Length);
        static Color[] Backcolor = { Color.SeaShell, Color.LightCyan, Color.MistyRose, Color.AliceBlue, Color.Wheat, Color.PowderBlue };
        static int[] thCor = { 127, 255, 0, 127, 127, 255, 127, 255, 0, 127, 127, 255 };
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <param name="vcode">验证码字符,仅限4个长度的英文和数字</param>
        /// <param name="lstR">随机种子数组</param>
        /// <param name="Aliasing">抗锯齿开关</param>
        /// <param name="Backline">背景线条开关</param>
        /// <param name="rth">字符打印质量模式</param>
        /// <returns>返回Bitmap对象</returns>
        public static Bitmap CreateVerifImage(string vcode, int[] lstR,
                                        bool Aliasing = false,
                                        bool Backline = true,
                                        TextRenderingHint rth = TextRenderingHint.SystemDefault)
        {
            Bitmap map = new Bitmap(100, 40);//创建图片背景
            using (Graphics g = Graphics.FromImage(map))
            {
                g.Clear(Backcolor[lstR[0] % 6]);
                if (Aliasing && Backline)//抗锯齿,用于背景线条
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
                }
                int cr = 0; int cg = 0; int cb = 0;
                if (Backline)//背景线条
                    for (int i = 0; i < 3; i++)//绘制背景线条,横竖各3条
                    {
                        cr = new Random(lstR[0] + i * 3).Next(150, 255);
                        cg = new Random(lstR[1] + i * 5).Next(150, 255);
                        cb = new Random(lstR[2] + i * 7).Next(150, 255);
                        Pen pb = new Pen(Color.FromArgb(cr, cg, cb), i % 2 + 1);
                        Point p1 = new Point(new Random(lstR[3] + i * 11).Next(0, 100), 0);
                        Point p2 = new Point(new Random(lstR[4] + i * 13).Next(0, 100), 40);
                        g.DrawLine(pb, p1, p2);
                        cr = new Random(lstR[0] + i * 13).Next(150, 255);
                        cg = new Random(lstR[1] + i * 11).Next(150, 255);
                        cb = new Random(lstR[2] + i * 7).Next(150, 255);
                        pb = new Pen(Color.FromArgb(cr, cg, cb), i % 2 + 1);
                        p1 = new Point(0, new Random(lstR[3] + i * 5).Next(0, 40));
                        p2 = new Point(100, new Random(lstR[4] + i * 3).Next(0, 40));
                        g.DrawLine(pb, p1, p2);
                    }
                g.TextRenderingHint = rth;
                Font f;
                int left = 0; int top = 0;
                for (int i = 0; i < vcode.Length; i++)
                {
                    int asc = (int)vcode[i];
                    int min = asc > 57 ? 16 : 24;//大写英文和数字同字号大小有差异
                    int max = asc > 57 ? 28 : 32;
                    int sz = new Random(lstR[i]).Next(min, max);
                    f = new Font("Arial", sz);
                    asc = (lstR[i] % 3) * 4;//字体颜色
                    cr = new Random(lstR[i]).Next(thCor[asc], thCor[asc + 1]);
                    cb = new Random(lstR[i + 1]).Next(thCor[asc + 2], thCor[asc + 3]);
                    cg = new Random(lstR[i + 2]).Next(0, 155);
                    Color cor = Color.FromArgb(cr, cg, cb);
                    if (i < 2)
                        left = (i * 25) - 5 + (sz % 6);
                    else
                        left = i * 24 - (sz % 6);
                    top = -4 + (sz % 12);
                    g.DrawString(vcode[i].ToString(), f, new SolidBrush(cor), left, top);
                }
            }
            return map;
        }
        /// <summary>
        /// 生成随机英文数字的随机字符
        /// </summary>
        /// <param name="lstR">随机种子数组</param>
        /// <param name="mode">生成模式</param>
        /// <returns>长度4的字符串</returns>
        public static string CreateVCode(int[] lstR, int mode = 0)
        {
            //去掉容易错判的字母 i,l,o,z
            char[] c = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
                         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
                         '2', '3', '4', '5', '6', '7', '8', '9', '1', '0'};
            int p = 0;
            int length = 0;
            switch (mode)
            {
                case 0: p = 44; length = 10; break;         //只是数字
                case 1: p = 0; length = 22; break;          //只是小写字母
                case 2: p = 22; length = 22; break;         //只是大写字母
                case 3: p = 0; length = 44; break;          //大小写混合
                case 4: p = 22; length = 30; break;         //大写字母+数字(不含1和0)
                default: p = 0; length = c.Length; break;   //所有混合
            }
            string s = c[lstR[0] % length + p].ToString() +
                       c[lstR[1] % length + p].ToString() +
                       c[lstR[2] % length + p].ToString() +
                       c[lstR[3] % length + p].ToString();
            return s;
        }
        public static int[] RandomList()
        {
            string guid = System.Guid.NewGuid().ToString("N");//f61fb920bb56454a86dfb91162717d87
            byte[] byt = System.Text.Encoding.ASCII.GetBytes(guid);
            int[] r = new int[8];
            CopyMemory(r, byt, 32);
            return r;
        }
            //CreateVerifImage()最后一个参数的可能值
            //System.Drawing.Text.TextRenderingHint.TextRenderingHint.SingleBitPerPixel;
            //System.Drawing.Text.TextRenderingHint.TextRenderingHint.AntiAlias;
            //System.Drawing.Text.TextRenderingHint.TextRenderingHint.SystemDefault;
            //System.Drawing.Text.TextRenderingHint.TextRenderingHint.ClearTypeGridFit;
            //System.Drawing.Text.TextRenderingHint.TextRenderingHint.SingleBitPerPixelGridFit;
    }
}

 

posted @ 2016-06-12 15:00  JustXIII  阅读(808)  评论(0编辑  收藏  举报