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 _02绘制基本图形
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Pen pen = new Pen(Brushes.Red, 5);
private void 直线ToolStripMenuItem_Click(object sender, EventArgs e)
{
graphics.DrawLine(pen, new Point(0, 0), new Point(100, 0));
}
private void 椭圆ToolStripMenuItem_Click(object sender, EventArgs e)
{
graphics.DrawEllipse(pen, new Rectangle(new Point(0, 0), new Size(200, 200)));
}
private void 多边形ToolStripMenuItem_Click(object sender, EventArgs e)
{
graphics.FillPolygon(Brushes.Cyan, new Point[] { new Point(120, 0), new Point(140, 100), new Point(240, 100), new Point(150, 140), new Point(170, 260), new Point(120, 170), new Point(70, 260), new Point(90, 140), new Point(0, 100), new Point(100, 100) });
}
private void 贝塞尔ToolStripMenuItem_Click(object sender, EventArgs e)
{
graphics.DrawBezier(pen, new Point(100, 100), new Point(200, 200), new Point(300, 100), new Point(400, 200));
}
Graphics graphics;
private void Form1_Load(object sender, EventArgs e)
{
graphics = panel1.CreateGraphics();
}
}
}
执行后:按钮点击事件开始画图
放一起:
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 _03网格
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Pen pen = new Pen(Brushes.Black, 1);
private void 网格ToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int i=0;i<=600/20;i++)
{
graphics.DrawLine(pen, new Point(0, 0 + i * 20), new Point(300, 0 + i * 20));
}
for (int i=0;i<=300/20;i++)
{
graphics.DrawLine(pen, new Point(0+i*20, 0), new Point(0+i*20, 600));
}
}
Graphics graphics;
private void Form1_Load(object sender, EventArgs e)
{
graphics = panel1.CreateGraphics();
panel1.Width = 301;
panel1.Height = 601;
panel1.BackColor = Color.Red;
}
}
}
效果图:
3.GDI绘制验证码
窗口设计:
源码:
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 _04GDI验证码
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SetCodeImage();
}
void SetCodeImage()
{
Random r = new Random();
int s = r.Next(0, 10);
int u = r.Next(0, 10);
int w = r.Next(0, 10);
char[] pattern = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
string e = NewMethod(r,pattern);
string code = s+e+u+w;
code = code.Trim();
if (String.IsNullOrEmpty(code))
{
return;
}
//制作一张图片
Bitmap image = new Bitmap((int)Math.Ceiling(code.Length * 10.0), 20);
Graphics graphics = Graphics.FromImage(image);
graphics.Clear(Color.White);
for (int i = 0; i < 4; i++)
{
int x1 = r.Next(image.Width);
int y1 = r.Next(image.Height);
int x2 = r.Next(image.Width);
int y2 = r.Next(image.Height);
graphics.DrawLine(new Pen(Color.Black, 2), new Point(x1, y1), new Point(x2, y2));
}
graphics.DrawString(code, new Font("黑体", 10, FontStyle.Bold), Brushes.Red, 2, 2);
//graphics.DrawLine(new Pen(Color.Cyan, 1), new Point(0, 0), new Point(0, 20));
//graphics.DrawLine(new Pen(Color.Cyan, 1), new Point(0, 0), new Point(code.Length * 10, 0));
//graphics.DrawLine(new Pen(Color.Cyan, 1), new Point(39, 0), new Point(39, 20));
//graphics.DrawLine(new Pen(Color.Cyan, 1), new Point(0, 19), new Point(39, 19));
graphics.DrawRectangle(new Pen(Color.Cyan), 0, 0, image.Width - 1, image.Height - 1);
this.BackgroundImage = image;
this.BackgroundImageLayout = ImageLayout.Center;
}
private static string NewMethod(Random r,char[] pattern)
{
string e="";
int rnd = r.Next(0, pattern.Length);
e += pattern[rnd];
return e;
}
private void button1_Click(object sender, EventArgs e)
{
SetCodeImage();
}
}
}
效果图:点击按钮更换验证码