当前位置:网站首页>WinForm generates random verification code
WinForm generates random verification code
2022-07-28 11:23:00 【Zhuanxiangge】
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace Generate random verification code
{
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);
// Write pictures locally Use it if you want Delete here if you don't want to save
FileStream file = new FileStream(Path.Combine(path,filename), FileMode.OpenOrCreate);
file.Write(memoryStream.GetBuffer(), 0, memoryStream.GetBuffer().Length);
file.Close();// Close file
// Display in picture area
Image image = Image.FromStream(memoryStream);
pictureBox1.Image = image;
pictureBox1.Size = new Size(image.Width, image.Height);
}
/// <summary>
/// Generate random verification code numbers + Letter
/// </summary>
/// <param name="codelen"> Length of verification code </param>
/// <returns> Return verification code </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)));// Generating random numbers
}
else
{
strCheckCode.Append((char)('A' + (char)(number % 26)));// Generate random letters
}
}
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);// Create a captcha 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 Set the font , Font size , The font
// Set the start color and end color of the graphic gradient , Gradient angle
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;
// Draw the foreground noise of the picture
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()));
}
// Draw the border line of the picture
graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
// Save the picture as stream Stream return
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms;
}
finally
{
graphic.Dispose();
image.Dispose();
}
}
}
}
边栏推荐
- WPF依赖属性(wpf 依赖属性)
- 使用共用体union及指针测试大小端
- 构建快捷开发IDE:VisualSVN+Sublime+Visual Studio 2013+QuickEasyFTPServer
- I use the applet container to improve the efficiency of mobile R & D by 5 times!
- Jianzhi offer 09. realize queue with two stacks
- [MySQL from introduction to proficiency] [advanced chapter] (IX) precautions for InnoDB's b+ tree index
- RHEL 6.4 安装svn和apache
- [JS advanced] JS functions, overloads, anonymous functions, scopes and scope chains_ 03
- web安全入门-Radius协议应用
- Eslint, eslint Chinese document
猜你喜欢

Summary of the second semester of junior year

1天涨粉81W,打造爆款短视频的秘诀是什么?

Bc35 NB module at instruction development summary

Inventory: 144 free learning websites, the most complete collection of resources in the whole network

Inventory: exciting data visualization chart
![[MySQL from introduction to proficiency] [advanced chapter] (IX) precautions for InnoDB's b+ tree index](/img/dc/2c11852929cc2ad4a2e44b87e6f812.png)
[MySQL from introduction to proficiency] [advanced chapter] (IX) precautions for InnoDB's b+ tree index

Installation points and precautions of split angle probe

18张图,直观理解神经网络、流形和拓扑

Technology sharing | quick intercom integrated dispatching system

CRM+零代码:轻松实现企业信息化
随机推荐
低代码(aPaas)为什么最近又火了?
使用 Terraform 在 AWS 上快速部署 MQTT 集群
vim命令下显示行号[通俗易懂]
go status.go 状态码定义
Preliminary understanding of float
Nodejs: set up the express service, set up the session and realize the exit operation
开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
Cortex-M内核管理全局中断的三种方式
LiteSpeed Web服务器中安装SSL证书
mysql的左连接和右连接(内连接和自然连接的区别)
[MySQL from introduction to proficiency] [advanced chapter] (IX) precautions for InnoDB's b+ tree index
重新刷新你对Redis集群的理解
[FPGA tutorial case 41] image case 1 - reading pictures through Verilog
[MySQL from introduction to proficiency] [advanced chapter] (x) MyISAM's indexing scheme & advantages and disadvantages of indexing
Array related knowledge points
Learn how to do e-commerce data analysis (with operation analysis index framework)
Ec20/ec25 4G module at instruction development summary
2021-03-24
Why should coding and modulation be carried out before transmission
Nodejs: return value of mongodb after successful insertion