/************************窗体验证码背景图形设计及核心代码**********/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace 验证码
{
    public class ValidCode
    {
        #region Private Fields
        private const double PI = 3.1415926535897932384626433832795;
        private const double PI2 = 6.283185307179586476925286766559;
        //private readonly int _wordsLen = 4;
        private int _len;
        private CodeType _codetype;
        private readonly Single _jianju = (float)18.0;
        private readonly Single _height = (float)24.0;
        private string _checkCode;
        #endregion
        #region Public Property
        public string CheckCode
        {
            get
            {
                return _checkCode;
            }
            set
            {
                _checkCode = value;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// public constructors
        /// </summary>
        /// <param name="len"> 验证码长度 </param>
        /// <param name="ctype"> 验证码类型:字母、数字、字母+ 数字 </param>
        public ValidCode(int len, CodeType ctype)
        {
            this._len = len;
            this._codetype = ctype;
        }

        #endregion

        #region Public Field

        public enum CodeType { Words, Numbers, Characters, Alphas }

        #endregion

        #region Private Methods
        private string GenerateNumbers()
        {
            string strOut = "";
            System.Random random = new Random();
            for (int i = 0; i < _len; i++)
            {
                string num = Convert.ToString(random.Next(10000) % 10);
                strOut += num;
            }
            return strOut.Trim();
        }
        private string GenerateCharacters()
        {

            string strOut = "";

            System.Random random = new Random();

            for (int i = 0; i < _len; i++)
            {

                string num = Convert.ToString((char)(65 + random.Next(10000) % 26));

                strOut += num;

            }

            return strOut.Trim();

        }

        //

        private string GenerateAlphas()
        {

            string strOut = "";

            string num = "";

            System.Random random = new Random();

            for (int i = 0; i < _len; i++)
            {

                if (random.Next(500) % 2 == 0)
                {

                    num = Convert.ToString(random.Next(10000) % 10);

                }

                else
                {

                    num = Convert.ToString((char)(65 + random.Next(10000) % 26));

                }

                strOut += num;

            }

            return strOut.Trim();

        }

        private System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
        {

            System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);

            // 将位图背景填充为白色

            System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);

            graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);

            graph.Dispose();

            double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;

            for (int i = 0; i < destBmp.Width; i++)
            {

                for (int j = 0; j < destBmp.Height; j++)
                {

                    double dx = 0;

                    dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;

                    dx += dPhase;

                    double dy = Math.Sin(dx);

                    // 取得当前点的颜色

                    int nOldX = 0, nOldY = 0;

                    nOldX = bXDir ? i + (int)(dy * dMultValue) : i;

                    nOldY = bXDir ? j : j + (int)(dy * dMultValue);

                    System.Drawing.Color color = srcBmp.GetPixel(i, j);

                    if (nOldX >= 0 && nOldX < destBmp.Width

                     && nOldY >= 0 && nOldY < destBmp.Height)
                    {

                        destBmp.SetPixel(nOldX, nOldY, color);

                    }

                }

            }
            return destBmp;
        }

        #endregion

        #region Public Methods
        public Stream CreateCheckCodeImage()
        {
            string checkCode;

            switch (_codetype)
            {

                case CodeType.Alphas:

                    checkCode = GenerateAlphas();
                    break;
                case CodeType.Numbers:
                    checkCode = GenerateNumbers();
                    break;
                case CodeType.Characters:
                    checkCode = GenerateCharacters();
                    break;
                default:
                    checkCode = GenerateAlphas();
                    break;
            }

            this._checkCode = checkCode;
            MemoryStream ms = null;
            //
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return null;
            Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * _jianju)), (int)_height);
            Graphics g = Graphics.FromImage(image);
            try
            {
                Random random = new Random();
                g.Clear(Color.White);

                // 画图片的背景噪音线

                for (int i = 0; i < 18; i++)
                {

                    int x1 = random.Next(image.Width);

                    int x2 = random.Next(image.Width);

                    int y1 = random.Next(image.Height);

                    int y2 = random.Next(image.Height);

                    g.DrawLine(new Pen(Color.FromArgb(random.Next()), 1), x1, y1, x2, y2);

                }

                Font font = new System.Drawing.Font("Times New Roman", 14, System.Drawing.FontStyle.Bold);

                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);

                if (_codetype != CodeType.Words)
                {

                    for (int i = 0; i < checkCode.Length; i++)
                    {

                        g.DrawString(checkCode.Substring(i, 1), font, brush, 2 + i * _jianju, 1);

                    }

                }
                else
                {

                    g.DrawString(checkCode, font, brush, 2, 2);

                }

                // 画图片的前景噪音点

                for (int i = 0; i < 150; i++)
                {

                    int x = random.Next(image.Width);

                    int y = random.Next(image.Height);

                    image.SetPixel(x, y, Color.FromArgb(random.Next()));

                }

                // 画图片的波形滤镜效果

                if (_codetype != CodeType.Words)
                {

                    image = TwistImage(image, true, 3, 1);

                }

                // 画图片的边框线

                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

                ms = new System.IO.MemoryStream();

                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

            }

            finally
            {

                g.Dispose();

                image.Dispose();

            }

            return ms;

        }

        #endregion

    }

}

/************************ 验证码窗体设计代码加载**************/

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

namespace 验证码
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ValidCode validCode = new ValidCode(5, ValidCode.CodeType.Numbers);
            // 图片加载验证码     
            picValidCode.Image = Bitmap.FromStream(validCode.CreateCheckCodeImage());
            lb_validCode.Text = validCode.CheckCode;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!this.txtValidCode.Text.Equals(lb_validCode.Text))
            {
                MessageBox.Show(" 请输入正确的验证码!", this.Text);
                this.txtValidCode.Focus();
                return;
            }
        }
        /// <summary>
        /// 双击图片重新刷新验证码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void picValidCode_DoubleClick(object sender, EventArgs e)
        {
            ValidCode validCode = new ValidCode(5, ValidCode.CodeType.Numbers);
            // 图片加载验证码     
            picValidCode.Image = Bitmap.FromStream(validCode.CreateCheckCodeImage());
            lb_validCode.Text = validCode.CheckCode;
        }
    }
}

c#窗体虚线图形验证码设计的更多相关文章

  1. MATLAB学习笔记(十一)——MATLAB图形用户界面设计

    (一)菜单设计 一.建立用户菜单 1.概况: 用户菜单一般含有一级菜单和二级菜单,乃至多级菜单.每一级菜单又包含多个菜单项.建立菜单可以使用uimenu函数. 2.uimenu函数调用: %建立一级菜 ...

  2. MATLAB图形界面设计(下)

    文章参考Blue Mountain https://www.cnblogs.com/BlueMountain-HaggenDazs/p/4307777.html 一.菜单设计 1.建立菜单项 (1)建 ...

  3. selenium基础-图形验证码

    selenium基础-图形验证码 一.图形验证码作用 设计的初衷其实就是为了防自动化,防止一些人利用自动工具恶意攻击网站 二.图形验证码是由客户端生成还是由服务器端生成的? 图形验证码是由服务器端生成 ...

  4. C# WinForm 使用SMS接口发送手机验证码+图形验证码+IP限制

    https://blog.csdn.net/IT_xiao_guang_guang/article/details/104299983 前言   1.发送手机验证码用的是网建的SMS接口(http:/ ...

  5. 【无私分享:ASP.NET CORE 项目实战(第十四章)】图形验证码的实现

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 很长时间没有来更新博客了,一是,最近有些忙,二是,Core也是一直在摸索中,其实已经完成了一个框架了,并且正在准备在生产环境中 ...

  6. eclipse里打开SWT项目找不到source/design的图形UI设计界面

    因为前天重新装了个新版的eclipse, 结果今天打开一个SWT的项目,突然找不到source/design的图形UI设计的两个切换按钮 我把SWT组件重新装了还是找不到.结果后来发现是因为重装ecl ...

  7. HTML5拓扑图形组件设计之道(一)

    HT for Web(http://www.hightopo.com/guide/readme.html)提供了涵盖通用组件.2D拓扑图形组件以及3D引擎的一站式解决方案,正如Hightopo官网所表 ...

  8. HT图形组件设计之道(四)

    在<HT图形组件设计之道(二)>我们展示了HT在2D图形矢量的数据绑定功能,这种机制不仅可用于2D图形,HT的通用组件甚至3D引擎都具备这种数据绑定机制,此篇我们将构建一个3D飞机模型,展 ...

  9. HT图形组件设计之道(三)

    上篇我们通过定制了CPU和内存展示界面,体验了HT for Web通过定义矢量实现图形绘制与业务数据的代码解耦及绑定联动,这类案例后续文章还会继续以便大家掌握更多的矢量应用场景,本篇我们先切换个话题, ...

随机推荐

  1. 无需写try/catch,也能正常处理异常 (转)

    原文地址: http://www.cnblogs.com/artech/archive/2012/10/28/automatic-exception-handling-aspnet.html 对于企业 ...

  2. Ambari 不能配置 Kafka 监听host的问题

    问题:Ambari下Kafka多IP监听配置 环境:Ambari 1.7.0 , Hadoop 2.2 Kafka 0.8.1.2.2.0.0 现象: Ambari 中是不能配置Kafka的host. ...

  3. .net工作准备系列--01前言

    注:学习参考朱毅编著的进入IT名企必读200题. 内容重在自我学习与巩固. 前言: 章节划分 01应聘须知 02基础知识(重点) 03进阶知识 04重点应用(aspnet第一部分) 04重点应用(as ...

  4. Openlayers简介

    OpenLayers 是由MetaCarta公司开发的,用于WebGIS客户端的 JavaScript包,目前的最高版本是2.5 V,通过BSD License 发行.它实现访问地理空间数据的方法都符 ...

  5. NOIP模拟赛-护花

    [题目描述] 约翰留下他的N(N<=100000)只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽 ...

  6. CSS3中-webkit-overflow-scrolling: touch 的使用方法详解

    -webkit-overflow-scrolling 属性控制元素在移动设备上是否使用滚动回弹效果. auto 使用普通滚动, 当手指从触摸屏上移开,滚动会立即停止. touch 使用具有回弹效果的滚 ...

  7. 移动web app 中的meta 标签

    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scal ...

  8. Ubuntu下使用SVN

      SVN作为日常开发中不可缺少的工具,今天终于开始在Ubuntu下使用了. 1.首先需要安装SVN.Ubuntu下的SVN安装十分简单,sudo apt-get install subversion ...

  9. MongoDB(七)MongoDb数据结构

    首先,向数据库插入一条bjson数据 首先是定义文档,然后使用admin用户名密码登录,进入test数据库,向test数据库中插入此文档("表名称和表中的记录") 插入结果,查看m ...

  10. Unity IOC简单认知

    看了不少IOC的文章.简单概念梳理下. 1.依赖,依赖倒置,控制反转(IOC),依赖注入 解释是仿照http://www.cnblogs.com/qqlin/archive/2012/10/09/27 ...