using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//一张纸 一根笔 一个人 两个点
}
//画直线
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics gp = this.CreateGraphics();
Pen p = new Pen(Color.Blue);
Point p1 = new Point(100, 100);
Point p2 = new Point(100, 300);
gp.DrawLine(p, p1, p2);
}
//画多线直线
private void button1_Click(object sender, EventArgs e)
{
//创建一个GDI+ 对象
Graphics g = this.CreateGraphics();
//创建画笔对象
Pen pen = new Pen(Color.Blue);
//创建两个点
Point p1 = new Point(100, 100);
Point p2 = new Point(30, 400);
//在画板中用pen在p1和p2两个点之间画一条直线
g.DrawLine(pen, p1, p2);
//pos为point数组,,在画板中画多条直线
Point p3 = new Point(300, 120);
Point p4 = new Point(15, 200);
Point[] pos = { p1, p2, p3, p4 };
g.DrawLines(pen, pos);
}
//画矩形
private void button2_Click(object sender, EventArgs e)
{
//创建GDI+对象
Graphics gp = this.CreateGraphics();
//创建矩形对象
左上角度座标
宽 高
Rectangle rec = new Rectangle(new Point(100, 10), new Size(100, 300));
gp.DrawRectangle(new Pen(Color.Blue), rec);
}
//填充矩形
private void button3_Click(object sender, EventArgs e)
{
//创建GDI+对象
Graphics gp = this.CreateGraphics();
//给定要填充的矩形对象
Rectangle rec = new Rectangle(new Point(100, 10), new Size(100, 300));
//填充颜色
获取系统颜色
给定要填充的矩形
gp.FillRectangle(Brushes.DarkGreen, rec);
}
//画扇形,并填充
private void button4_Click(object sender, EventArgs e)
{
//创建GDI+对象
Graphics gp = this.CreateGraphics();
//给定要填充的矩形对象
Rectangle rec = new Rectangle(new Point(100, 10), new Size(100, 300));
//绘制扇形 pen对象
矩形 右角 左角度
gp.DrawPie(new Pen(Color.Red), rec, 60, 60);
//填充扇形 获取系统对象
矩形 右角 左角度
gp.FillPie(Brushes.DarkGreen, rec, 60, 60);
}
//画文字
private void button5_Click(object sender, EventArgs e)
{
//创建GDI+对象
Graphics g = this.CreateGraphics();
//绘制文本 文本
字体样式: 字体 字号 样式粗?斜?... 获取系统颜色
绘制到的座标点
g.DrawString("中国您好!", new Font("华文行楷", 20, FontStyle.Italic), Brushes.Red, new Point(300, 300));
}
//验证码
private void pictureBox1_Click(object sender, EventArgs e)
{
//生成1到9之间的4个随机数
Random r = new Random();
string str = "";
for (int i = 0; i < 4; i++)
{
int rNumber = r.Next(0, 10);
//累加到空字符串中
str += rNumber;
}
//创建GDI+对象
//创建图片对象, 指定 宽 和 高
Bitmap bm = new Bitmap(115,30);;
//从指定的Image对象创建新的Grapics画板
Graphics g = Graphics.FromImage(bm);
//数组存放字体
string[] fonts = {"黑体","微软雅黑","隶书","楷体"};
//数组存放颜色
Color[] colors = {Color.Red,Color.Yellow,Color.Pink,Color.Purple};
//画字
for (int i = 0; i < 4; i++)
{
//指定座标
Point p = new Point(i*20,0);
//画文字
g.DrawString(str[i].ToString(), new Font(fonts[i], 20, FontStyle.Italic), new SolidBrush(colors[i]),p);
}
//画线
for (int j = 0; j < 16; j++)
{
Point p1 = new Point(r.Next(0,bm.Width),r.Next(bm.Height));
Point p2 = new Point(r.Next(0,bm.Width),r.Next(0,bm.Height));
g.DrawLine(new Pen(Color.Green), p1, p2);
}
//画像素点
for (int i = 0; i < 100; i++)
{
Point p1 = new Point(r.Next(0,bm.Width),r.Next(bm.Height));
Point p2 = new Point(r.Next(0,bm.Width),r.Next(0,bm.Height));
bm.SetPixel(p1.X, p1.Y, Color.Black);
}
//将bm这个图片直接镶嵌到pictureBox上
pictureBox1.Image = bm;
}
}
}