验证码图片生成

 

一、三张效果图

二、源码

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();
            }
        }
    }
}

 

 

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值