**
1、实现简单绘制直线、矩形、扇形
**
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 _01GDI简单绘制1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//一根笔 颜色 一张纸 两点 绘制直线
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Black);
Point p1 = new Point(30,30);
Point p2 = new Point(60, 60);
g.DrawLine(pen, p1, p2);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.DrawRectangle(new Pen(Brushes.Black, 2), new Rectangle(new Point(20, 20), new Size(80, 80)));
}
private void button3_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Black);
Rectangle rectangle = new Rectangle(new Point(10, 10), new Size(100, 100));
g.DrawPie(pen, rectangle, 30, 90);
}
}
}
Form1设计
**
2、实现点击更换二维码
**
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 _02GDI更换验证码
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
}
//点击
private void pictureBox1_Click(object sender, EventArgs e)
{
Random r = new Random();
string str = null;
for (int i = 0; i < 5; i++)
{
int dNum = r.Next(0, 10);
str += dNum;
}
Bitmap bitmap = new Bitmap(150, 30);
Graphics graphics = Graphics.FromImage(bitmap);
String[] font = { "楷体", "宋体", "黑体", "仿宋" };
Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Yellow };
for (int i = 0; i < 5; i++)
{
Point point1 = new Point(i * 20, 0);
graphics.DrawString(str[r.Next(0, 5)].ToString(), new Font(font[r.Next(0, 4)], 25), new SolidBrush(color[r.Next(0, 5)]), point1);
}
for (int i = 0; i < 50; i++)
{
Point pt1 = new Point(r.Next(0, bitmap.Width), r.Next(0, bitmap.Height));
Point pt2 = new Point(r.Next(0, bitmap.Width), r.Next(0, bitmap.Height));
graphics.DrawLine(new Pen(Brushes.Brown), pt1, pt2);
}
for (int i = 0; i < 500; i++)
{
bitmap.SetPixel(r.Next(0, bitmap.Width), r.Next(0, bitmap.Height), color[r.Next(0, 5)]);
}
pictureBox1.Image = bitmap;
}
}
}