C# 验证码

作者:陈太汉

C# 验证码

多功能注册码,注册码可以完全自定义,全部都是动态的,包括字体的颜色,大小,样式,还有内容

 

 

using System;
using System.Drawing;

namespace SecurityCode
{
   public class DrawMethod
    {
    
       /// <summary>
       /// 画图
       /// </summary>
       /// <param name="content"></param>
       /// <param name="size"></param>
       /// <param name="fileName"></param>
       public void Draw(string content,Size size,string fileName)
       {
           Image image = new Bitmap(size.Width,size.Height);
           Graphics g = Graphics.FromImage(image);
           DrawBorder(g,image.Size);
           DrawContent(g,content,image.Size,(int)FontSet.Font.Size);
           DrawRandom(g, image.Size);
           image.Save(@"D://" + fileName);
       }

       /// <summary>
       /// 画边框
       /// </summary>
       /// <param name="g"></param>
       /// <param name="size"></param>
       private void DrawBorder(Graphics g, Size size)
       {
           Pen pen = new Pen(SolidBrushSet.SolidBrush);
           Rectangle rect=new Rectangle(1,1,size.Width-4,size.Height-4);
           g.DrawRectangle(pen,rect);
       }

       //画字符串
       private void DrawContent(Graphics g, string content,Size size,int fontHeight)
       {
           Point point = new Point();
           int i = 0;
           point.Y = (size.Height - fontHeight) / 2;
           int distance = size.Width / (content.Length+1);
           foreach (char c in content)
           {
               point.X = i * distance + distance/2;
               g.DrawString(c.ToString(), FontSet.Font, SolidBrushSet.SolidBrush, point);
               i++;
           }
       }

       /// <summary>
       /// 画干扰
       /// </summary>
       /// <param name="g"></param>
       /// <param name="size"></param>
       private void DrawRandom(Graphics g, Size size)
       {
           Pen pen = new Pen(SolidBrushSet.SolidBrush);
           Random rand = new Random();
           for (int i = 0; i < 3; i++)
           {
               g.DrawLine(pen, rand.Next(size.Width), rand.Next(size.Width), rand.Next(size.Height), rand.Next(size.Height));
           }

           for (int i = 0; i < 10; i++)
           {
               Rectangle rect = new Rectangle(rand.Next(2, size.Width - 2), rand.Next(2, size.Height - 2), 1, 1);
               g.DrawRectangle(pen, rect);
           }
       }
       
    }
}

 

 

using System;

namespace SecurityCode
{
    /// <summary>
    /// 自动生成字符串
    /// </summary>
   public class CreateContent
    {
      private string so = "1234567890abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM";

       /// <summary>
       /// 生成内容
       /// </summary>
       /// <returns></returns>
      public string GetContent()
      {
          Random rand = new Random();
          string str = null;
          for (int i = 0; i < 6; i++)
          {
              str += so.Substring(rand.Next(62), 1);
          }
          return str;
      }
    }
}

 

 

using System;
using System.Drawing;

namespace SecurityCode
{
    /// <summary>
    /// 字体设置
    /// </summary>
    public class FontSet
    {
        public static Font Font
        {
            get
            {
                FontFamily fFamily = GetFontFamily();
                FontStyle fStyle = GetFontStyle();
                int emSize = GetFontSize();
                return new Font(fFamily, emSize, fStyle);
            }
        }

        private FontSet() { }


        /// <summary>
        /// 设置字体
        /// </summary>
        private static FontFamily GetFontFamily()
        {
            Random rand = new Random();
            return FontFamily.Families[rand.Next(FontFamily.Families.Length)];
        }

        /// <summary>
        /// 设置字体样式
        /// </summary>
        private static FontStyle GetFontStyle()
        {
            Random rand = new Random();
            int index = rand.Next(1, 4);
            index = 1 << index;
            return (FontStyle)index;
        }

        /// <summary>
        /// 设置字体大小
        /// </summary>
        public static int GetFontSize()
        {
            Random rand = new Random();
            return rand.Next(12, 14);
        }
    }
}

 

 

using System;
using System.Drawing;

namespace SecurityCode
{
    /// <summary>
    /// 画笔设置
    /// </summary>
   public class SolidBrushSet
    {
       /// <summary>
       /// 画笔
       /// </summary>
       public static SolidBrush SolidBrush
       {
           get {
               Color color= GetColor();
               return new SolidBrush(color);
           }
       }

       private SolidBrushSet(){}

       /// <summary>
       /// 随机生成画笔颜色
       /// </summary>
       /// <returns></returns>
       public static Color GetColor()
       {
           Random rand = new Random();
           int r = rand.Next(0,255);
           int g = rand.Next(0, 255);
           int b = rand.Next(0, 255);
           return Color.FromArgb(r,g,b);
       }
    }
}

 

 

 

   private void Test()
        {
            DrawMethod drawMethod = new DrawMethod();
            CreateContent createCont = new CreateContent();
            for (int i = 0; i < 10; i++)
            {
                string content = createCont.GetContent();
                Size size = new Size(100, 40);
                drawMethod.Draw(content, size, i.ToString() + ".jpg");
            }
        }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值