验证码图片生成

来源:互联网 发布:日本制造 知乎 编辑:程序博客网 时间:2024/06/07 06:36


一、三张效果图


二、源码

using System;using System.Drawing;namespace ConsoleTest{    class Program    {        static void Main(string[] args)        {            CreateImages(GetRndStr(4));//4位验证码            //Console.ReadKey();        }        static string GetRndStr(int Count) //字符串验证码        {            string Vchar = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";            string checkCode = string.Empty;            Random rand = new Random();            for (int i = 0; i < Count; i++)            {                checkCode += Vchar.Substring(rand.Next(0, Vchar.Length), 1);            }            return checkCode;        }        static void CreateImages(string checkCode) //画图片的背景图,干扰        {            try            {                int step = 3;                int width = checkCode.Length * (22 + step);                Bitmap bitmap = new Bitmap(width, 35);//位图文件,扩展名可以是.bmp或者.dib。位图是Windows标准格式图形文件,它将图像定义为由点(像素)组成,每个点可以由多种色彩表示                Graphics g = Graphics.FromImage(bitmap);                g.Clear(Color.White);//清除背景色                Color[] colorArr = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };//定义颜色                string[] fontArr = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial" };//{"宋体" };                Random rand = new Random();                for (int i = 0; i < 10; i++) //画n条背景干扰线                {                    int x1 = rand.Next(bitmap.Width);                    int x2 = rand.Next(bitmap.Width);                    int y1 = rand.Next(bitmap.Height);                    int y2 = rand.Next(bitmap.Height);                    int colorIndex = rand.Next(colorArr.Length);                    g.DrawLine(new Pen(colorArr[colorIndex], 1), x1, y1, x2, y2);                }                for (int i = 0; i < checkCode.Length; i++) //将获取到的验证码画在图片上                {                    int colorIndex = rand.Next(colorArr.Length);                    int fontIndex = rand.Next(fontArr.Length);                    Brush brush = new SolidBrush(colorArr[colorIndex]);                    Font Font = new Font(fontArr[fontIndex], 14, FontStyle.Bold);                    int height = rand.Next(3,7); //返回的随机数的上界(随机数不能取该上界值)。                    g.DrawString(checkCode.Substring(i, 1), Font, brush, 11 + (i * (15 + step)), height); //DrawString(string s, Font font, Brush brush, float x, float y);                }                for (int i = 0; i < 5; i++) //画n条干扰线                {                    int x1 = rand.Next(bitmap.Width);                    int x2 = rand.Next(bitmap.Width);                    int y1 = rand.Next(bitmap.Height);                    int y2 = rand.Next(bitmap.Height);                    int colorIndex = rand.Next(colorArr.Length);                    g.DrawLine(new Pen(colorArr[colorIndex], 1), x1, y1, x2, y2);                }                g.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, bitmap.Width - 1, bitmap.Height - 1);//画图片边框                bitmap.Save(@"E:\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png");  //image.Save(@"C:\"); 报错,发生一般性错误,要指定文件名,不能只是目录。            }            catch (Exception ex)            {                string str = ex.ToString();            }        }    }}


原创粉丝点击