• c# 画布验证码


    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 = "";
                for (int i = 0; i < 4; i++)
                {
                    char c = (char)ran.Next(65, 91);
                    code += c;
                }
                int lineNum = 0;
                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; }
            }
            private int _lineNum = 20;
    
            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 = new Bitmap((int)Math.Ceiling(this.Code.Length * 12.5) + 2, 22);
                Graphics grap = Graphics.FromImage(bmp);
                grap.Clear(Color.White);
    
                Font font = new Font("Arial", 12, FontStyle.Regular);
                Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
                LinearGradientBrush lgb = new LinearGradientBrush(r, this.Color1, this.Color2, 1.2f, true);
                grap.DrawString(this.Code, font, lgb, 0, 2);
                grap.Dispose();
                return bmp;
            }
    
            private Bitmap DrawImg(bool ckline)
            {
                Bitmap bmp = new Bitmap((int)Math.Ceiling(this.Code.Length * 12.5) + 2, 22);
                Graphics grap = Graphics.FromImage(bmp);
                grap.Clear(Color.White);
    
                //一半的干扰线
                Random random = new Random(); 
                for (int i = 0; i < this.LineNum/2; 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 int_Red = random.Next(256);
                    int int_Green = random.Next(256);
                    int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
                    int_Blue = (int_Blue > 255) ? 255 : int_Blue;
                    grap.DrawLine(new Pen(Color.FromArgb(int_Red, int_Green, int_Blue)), x1, y1, x2, y2); 
                }
    
                Font font = new Font("Arial", 12, FontStyle.Regular);
                Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
                LinearGradientBrush lgb = new LinearGradientBrush(r, this.Color1, this.Color2, 1.2f, true);
                grap.DrawString(this.Code, font, lgb, 0, 2);
                grap.Dispose();
                //一半的干扰点
                for (int i = 0; i < this.LineNum / 2; i++)
                {
                    int x1 = random.Next(bmp.Width);
                    int x2 = random.Next(bmp.Height);
    
                    int int_Red = random.Next(256);
                    int int_Green = random.Next(256);
                    int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
                    int_Blue = (int_Blue > 255) ? 255 : int_Blue;
    
                    bmp.SetPixel(x1, x2, Color.FromArgb(int_Red, int_Green, int_Blue));
                }
    
                
                return bmp;
            }
        }
    }

    效果

  • 相关阅读:
    盒子模型之边框border
    CSS优先级特性之权重叠加
    CSS三大特性:层叠性、继承性、优先级
    行高
    单行文本垂直居中
    !important
    【DP专题】——[USACO13OPEN]照片Photo
    1:n Oberserver模式
    025_递归算法详解
    字符串移动问题
  • 原文地址:https://www.cnblogs.com/feizianquan/p/10295863.html
Copyright © 2020-2023  润新知