using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void lblColor1_Click(object sender, EventArgs e)
        {
            Label lbl = sender as Label;
            ColorDialog cd = new ColorDialog();
            DialogResult dr = cd.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                lbl.BackColor = cd.Color;
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            Random ran = new Random();
            string code = "";
            ; i < ; i++)
            {
                , );
                code += c;
            }
            ;
            Bitmap bmp = null;
            if (!(int.TryParse(txtLineNum.Text, out lineNum) && ckLine.Checked))
            {
                VerifyCode vc = new VerifyCode();
                bmp = vc.Draw();
            }
            else
            {
                VerifyCode vc = new VerifyCode(ckLine.Checked, lblColor1.BackColor, lblColor2.BackColor, int.Parse(txtLineNum.Text), code);
                bmp = vc.Draw();
            }
            picImg.Image = bmp;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication3
{
    public class VerifyCode
    {
        private bool _line = false;

        public bool Line
        {
            get { return _line; }
            set { _line = value; }
        }

        private Color _color1 = Color.Blue;

        public Color Color1
        {
            get { return _color1; }
            set { _color1 = value; }
        }
        private Color _color2 = Color.DarkRed;

        public Color Color2
        {
            get { return _color2; }
            set { _color2 = value; }
        }
        ;

        public int LineNum
        {
            get { return _lineNum; }
            set { _lineNum = value; }
        }

        private string _code = "ABCD";

        public string Code
        {
            get { return _code; }
            set { _code = value; }
        }

        public VerifyCode()
        {
        }

        public VerifyCode(bool ckline, Color color1, Color color2, int lineNum, string code)
        {
            this.Line = ckline;
            this.Color1 = color1;
            this.Color2 = color2;
            this.LineNum = lineNum;
            this.Code = code;
        }

        public Bitmap Draw()
        {
            if (this.Line)
            {
                return DrawImg(this.Line);
            }
            else
            {
                return DrawImg();
            }

        }

        private Bitmap DrawImg()
        {
            Bitmap bmp = , );
            Graphics grap = Graphics.FromImage(bmp);
            grap.Clear(Color.White);

            Font font = , FontStyle.Regular);
            Rectangle r = , , bmp.Width, bmp.Height);
            LinearGradientBrush lgb = new LinearGradientBrush(r, this.Color1, this.Color2, 1.2f, true);
            grap.DrawString(, );
            grap.Dispose();
            return bmp;
        }

        private Bitmap DrawImg(bool ckline)
        {
            Bitmap bmp = , );
            Graphics grap = Graphics.FromImage(bmp);
            grap.Clear(Color.White);

            //一半的干扰线
            Random random = new Random();
            ; i < ; i++)
            {
                int x1 = random.Next(bmp.Width);
                int x2 = random.Next(bmp.Width);
                int y1 = random.Next(bmp.Height);
                int y2 = random.Next(bmp.Height);

                );
                );
                ) ?  :  - int_Red - int_Green;
                int_Blue = (int_Blue > ) ?  : int_Blue;
                grap.DrawLine(new Pen(Color.FromArgb(int_Red, int_Green, int_Blue)), x1, y1, x2, y2);
            }

            Font font = , FontStyle.Regular);
            Rectangle r = , , bmp.Width, bmp.Height);
            LinearGradientBrush lgb = new LinearGradientBrush(r, this.Color1, this.Color2, 1.2f, true);
            grap.DrawString(, );
            grap.Dispose();
            //一半的干扰点
            ; i < ; i++)
            {
                int x1 = random.Next(bmp.Width);
                int x2 = random.Next(bmp.Height);

                );
                );
                ) ?  :  - int_Red - int_Green;
                int_Blue = (int_Blue > ) ?  : int_Blue;

                bmp.SetPixel(x1, x2, Color.FromArgb(int_Red, int_Green, int_Blue));
            }

            return bmp;
        }
    }
}

效果

最新文章

  1. CSS的常见属性
  2. java 经典程序 100 例
  3. [自娱自乐] 3、超声波测距模块DIY笔记(三)
  4. .Net一般处理程序来实现用户名的验证
  5. 《TCP/IP详解卷1:协议》第4章 ARP:地址解析协议-读书笔记
  6. Markdown语法和MWeb使用说明
  7. 函数buf_page_init
  8. Codeforces Round #333 (Div. 2) C. The Two Routes flyod
  9. [C#]AccessUtils
  10. Unique Binary Search Trees II
  11. android 处理图片之--bitmap处理
  12. Mvc里删除Cooki
  13. 谷歌浏览器訪问不了啦,oh shit!
  14. springMVC学习总结(三)数据绑定
  15. 打开word时出现the setup controller has encountered a problem during install解决办法
  16. Centos7 上安装配置 RabbitMQ
  17. python中深拷贝和浅拷贝
  18. tcpdump使用示例
  19. Smart Thread Pool (智能线程池)
  20. Linux系统编程:文件I/O编程

热门文章

  1. 点击a标签的文字后页面的跳转
  2. MFC单文档视图程序简介
  3. 使用Spring+MySql实现读写分离(一)关于windows下安装mysql5.6
  4. NAT技术与代理服务器
  5. Javascript高级编程学习笔记(26)—— 函数表达式(4)私有变量
  6. Web前端JQuery面试题(三)
  7. H5内联视频总结
  8. [Postman]发出SOAP请求(18)
  9. SQLServer脚本编写
  10. Python常用模块——目录