在c#中提供了Graphics类来封装GDI+绘图。
(1)DrawLine
该方法绘制一条连接由坐标对指定的两个点的线条。
(2)DrawLines
该方法绘制一系列连接一组 point结构的线段。
(3)DrawRectangle
绘制由坐标对,宽度和高度指定的矩形。
(4)DrawRectangles
绘制一系列由Rectangles结构指定的矩形。
(5)DrawArc
该方法绘制一段弧线,它表示一对坐标,宽度和高度指定的椭圆图形。
(6)DrawEillipse
该方法绘制一个由边框(该边框由一对坐标,宽度和高度)定义的椭圆。
(7)DrawPie
绘制一个扇形,该扇形由一个坐标对,宽度和高度以及两条射线所指定的椭圆。
代码如下:
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();
}
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.ClientSize = new System.Drawing.Size(487, 348);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "GDI+绘图";
this.Load += new System.EventHandler(this.Form1_Load_1);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false);
}
#endregion
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawLine(new Pen(Color.DarkCyan, 1.0f), 20, 50, 300, 80);
Point[] points ={
new Point(100,10),
new Point(10,100),
new Point(300,50),
new Point(230,120),
new Point(100,220),
new Point(this.Width/2,this.Height/2),
new Point(0,300)
};
g.DrawLines(new Pen(Color.DarkGreen, 2.0f), points);
RectangleF[] rects =
{
new RectangleF (0.0F,0.0F,100.0F,200.0F),
new RectangleF (100.0F,200.0F,250.0F,50.0F),
new RectangleF (200.0F,9.0F,50.0F,100.0F)
};
g.DrawRectangles(new Pen(Color.DarkRed, 5.0f), rects);
g.DrawArc(new Pen(Color.DarkOrange, 7.0f), 2, 50,100, 200, 55, 540);
g.DrawEllipse(new Pen(Color.DarkOrange, 7.0f),50,50, 100, 200);
g.DrawPie(new Pen(Color.DarkBlue, 7.0f),80, 50, 200, 200,50,140);
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
}
}
在开始自己做这个程序时,出现了一些问题。注意此时的事件是paint.改过之后,发现可以正常运行了。
欢迎做指导。