/*
标题:C# GDI+ 简单画图小例子
* 作者:flydoos
* QQ群:C#/Java技术交流群(96020642)
* 博客:
http://flydoos.cnblogs.com
* 微博:
http://weibo.com/flydoos
* 日期:2011-09-22
*
* 因为都非常相似,其他的就不写了
* 例如:绘制一个填充的多边形 DrawPolygon、FillPolygon
* 自己看提示去写吧
*
*/
using System;
using System.Drawing;
using System.Windows.Forms;
namespace GDI_Demo
{
public
partial
class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
//
画直线
private
void btnDrawLine_Click(
object sender, EventArgs e)
{
Graphics g =
this.CreateGraphics();
Color red = Color.FromArgb(
255,
0,
0);
Pen blackPen =
new Pen(red,
3);
Point point1 =
new Point(
100,
100);
//
坐标(100,100)
Point point2 =
new Point(
500,
100);
//
坐标(500,100)
g.DrawLine(blackPen, point1, point2);
//
两个坐标连成直线
}
//
强制对象进行重绘
private
void btnInvalidate_Click(
object sender, EventArgs e)
{
Rectangle r =
new Rectangle(
10,
10,
200,
400);
//
从坐标(10,10)开始,定义一个宽度200,高度200的矩形区域
this.Invalidate(r);
}
//
清除整个绘图画面并以某种颜色填充
private
void btnClear_Click(
object sender, EventArgs e)
{
Graphics g =
this.CreateGraphics();
g.Clear(Color.Red);
}
//
画弧线
private
void btnDrawArc_Click(
object sender, EventArgs e)
{
Graphics g =
this.CreateGraphics();
Rectangle r =
new Rectangle(
10,
10,
200,
100);
Pen pen1 =
new Pen(Color.Black);
g.DrawArc(pen1,
50,
50,
200,
200,
180,
90);
//
起始坐标(50,50),宽度200,高度200,弧线的起始角度180°,弧线经过角度90°
g.DrawArc(pen1, r,
0,
135);
}
//
画椭圆、填充椭圆
private
void btnDrawEllipse_Click(
object sender, EventArgs e)
{
Graphics g =
this.CreateGraphics();
Rectangle r =
new Rectangle(
10,
10,
200,
100);
Pen pen1 =
new Pen(Color.Black);
//
椭圆
g.DrawEllipse(pen1,
50,
50,
200,
200);
//
起始坐标(50,50),宽度200,高度200
g.DrawEllipse(pen1, r);
//
填充椭圆
g.FillEllipse(
new SolidBrush(Color.Blue),
50,
50,
200,
200);
g.FillEllipse(
new SolidBrush(Color.DarkOrange), r);
}
//
画矩形、填充矩形
private
void btnRectangle_Click(
object sender, EventArgs e)
{
Graphics g =
this.CreateGraphics();
Rectangle r =
new Rectangle(
10,
10,
200,
100);
Pen pen1 =
new Pen(Color.Black);
//
矩形
g.DrawRectangle(pen1,
50,
50,
200,
200);
//
起始坐标(50,50),宽度200,高度200
g.DrawRectangle(pen1, r);
//
填充矩形
g.FillRectangle(
new SolidBrush(Color.Blue),
50,
50,
200,
200);
g.FillRectangle(
new SolidBrush(Color.DarkOrange), r);
}
private
void btnDrawPie_Click(
object sender, EventArgs e)
{
Graphics g =
this.CreateGraphics();
Rectangle r =
new Rectangle(
10,
10,
200,
100);
Pen pen1 =
new Pen(Color.Black);
//
椭圆
g.DrawPie(pen1,
50,
50,
200,
200,
225,
90);
//
起始坐标(50,50),宽度200,高度200,射线1=225,射线2=90
g.DrawPie(pen1, r,
225,
90);
//
填充椭圆
g.FillPie(
new SolidBrush(Color.Blue),
50,
50,
200,
200,
225,
90);
g.FillPie(
new SolidBrush(Color.DarkOrange), r,
225,
90);
}
}
}