当前位置:网站首页>Winform 生成随机验证码
Winform 生成随机验证码
2022-07-28 09:33:00 【篆香阁】
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace 生成随机验证码
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string path = Application.StartupPath;
string filename = "code.png";
private void button1_Click(object sender, EventArgs e)
{
string data= MakeCode(4);
textBox1.Text= data;
MemoryStream memoryStream= CheckCodeImage(data);
//将图片写入本地 想存就用 不想存就删掉这里
FileStream file = new FileStream(Path.Combine(path,filename), FileMode.OpenOrCreate);
file.Write(memoryStream.GetBuffer(), 0, memoryStream.GetBuffer().Length);
file.Close();//关闭文件
//显示在图片区
Image image = Image.FromStream(memoryStream);
pictureBox1.Image = image;
pictureBox1.Size = new Size(image.Width, image.Height);
}
/// <summary>
/// 生成随机验证码数字+字母
/// </summary>
/// <param name="codelen">验证码长度</param>
/// <returns>返回验证码</returns>
public static string MakeCode(int codelen)
{
if (codelen < 1)
{
return string.Empty;
}
int number;
StringBuilder strCheckCode = new StringBuilder();
Random random = new Random();
for (int index = 0; index < codelen; index++)
{
number = random.Next();
if (number % 2 == 0)
{
strCheckCode.Append((char)('0' + (char)(number % 10)));//生成随机数字
}
else
{
strCheckCode.Append((char)('A' + (char)(number % 26)));//生成随机字母
}
}
return strCheckCode.ToString();
}
public static MemoryStream CheckCodeImage(string CheckCode)
{
if (string.IsNullOrEmpty(CheckCode))
{
return null;
}
Bitmap image = new Bitmap((int)Math.Ceiling((CheckCode.Length * 12.5)), 22);
Graphics graphic = Graphics.FromImage(image);//创建一个验证码图片
try
{
Random random = new Random();
graphic.Clear(Color.White);
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
for (int index = 0; index < 25; index++)
{
x1 = random.Next(image.Width);
x2 = random.Next(image.Width);
y1 = random.Next(image.Height);
y2 = random.Next(image.Height);
graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));//Font设置字体,字号,字形
//设置图形渐变色的起始颜色与终止颜色,渐变角度
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
graphic.DrawString(CheckCode, font, brush, 2, 2);
int X = 0; int Y = 0;
//绘制图片的前景噪点
for (int i = 0; i < 100; i++)
{
X = random.Next(image.Width);
Y = random.Next(image.Height);
image.SetPixel(X, Y, Color.FromArgb(random.Next()));
}
//画图片的边框线
graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//将图片保存为stream流返回
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms;
}
finally
{
graphic.Dispose();
image.Dispose();
}
}
}
}
边栏推荐
- This wechat plug-in is very easy to use
- Today, I want to talk about the data types of MySQL database
- Standing on the shoulders of big men, you can see further
- MQ learning
- 能够遍历一个文件夹下的所有文件和子文件夹
- WPF布局之控件随着窗口等比放大缩小,适应多分辨率满屏填充应用
- FixedWindowRollingPolicy简介说明
- (iros 2022) monocular visual inertial odometer based on event camera
- Window源码解析(三):Window的更新机制
- 分支与循环(1)
猜你喜欢

这种动态规划你见过吗——状态机动态规划之股票问题(中)

学个锤子 | .Net零基础逆向教程 第三课(壳与作业)

NET 3行代码实现文字转语音功能

关于CLR GC调优的一些问题

JS提升:实现flat平铺的底层原理

pycharm使用conda调用远程服务器

Pulse style | exclusive interview with Committee -- Tencent engineer Zhang Dawei calls you to eat "crab"
![[log] what does a log do? What is a log factory? Configuration and use of log4j? log4j. Properties file configuration, log4j jar package coordinates](/img/ae/096b558bc1342447205b442a244aae.png)
[log] what does a log do? What is a log factory? Configuration and use of log4j? log4j. Properties file configuration, log4j jar package coordinates

Linux操作系统(Centos7)安装MySQL

Basic operation of MATLAB
随机推荐
判断字符串是不是回文
Database advanced learning notes -- storage functions
Standing on the shoulders of big men, you can see further
Window source code analysis (II): the adding mechanism of window
FixedWindowRollingPolicy简介说明
PHP连接mysql原生代码
Time series analysis 41 - time series prediction tbats model
Seektiger eco pass STI new progress, log in to ZB on April 14
7.27 最小生成树阶段性测试题解
include 与 require include_once 与 require_once 的区别
Symbolic operation of MATLAB
Real time editor of MATLAB
Create SSL certificate using OpenSSL
Array collation commonly used in PHP
Software testing and quality learning notes 2 - black box testing
New features of ES6
Linux操作系统(Centos7)安装MySQL
ES6新特性
Learn a hammer.Net zero foundation reverse tutorial lesson 3 (shell and homework)
[collection] linear algebra let me think - Summary of chapter topics