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