c#中GDI+图形图像:GDI+中的椭圆和弧使用方法|实例
GDI+中的椭圆和弧
1.绘制椭圆
绘制椭圆时,可以调用Graphics类中的DrawEllipse方法,该方法为可重载方法,它主要用来绘制边界由Rectangle结构指定的椭圆,其常用格式有以下两种。
(1)绘制边界由Rectangle结构指定的椭圆。
语法:
public void DrawEllipse (
Pen pen,
Rectangle rect)
参数说明如下。
pen:Pen对象,它确定曲线的颜色、宽度和样式。
rect:Rectangle结构,它定义椭圆的边界。
(2)绘制一个由边框(该边框由一对坐标、高度和宽度指定)指定的椭圆。
语法:
public void DrawEllipse (
Pen pen,
int x,
int y,
int width,
int height)
DrawEllipse方法中各参数及说明如表1所示。
表1 DrawEllipse方法参数及说明
示例
绘制椭圆
本示例实现的是,当程序运行时,单击【绘制椭圆】按钮,在窗体中的指定位置绘制一个指定边框大小的椭圆。示例运行结果如图1所示。
图1 绘制椭圆
程序代码如下。
Form1窗体中,在【绘制椭圆】按钮的Click事件中分别声明Graphics类和Pen类的两个实例对象,然后定义一个Rectangle结构,以用来控制椭圆的边框大小,最后调用Graphics对象的DrawEllipse方法在窗体中绘制一个指定边框大小的椭圆。网站源代码【绘制椭圆】按钮的Click事件代码如下:
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Blue, 3);
Rectangle myRectangle = new Rectangle(70, 20, 100, 60);
graphics.DrawEllipse(myPen, myRectangle);
}
完整程序代码如下:
★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _6_03
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Blue, 3);
Rectangle myRectangle = new Rectangle(70, 20, 100, 60);
graphics.DrawEllipse(myPen, myRectangle);
}
}
}
★ ★★★★Form1.Designer.cs窗体代码文件完整程序代码★★★★★
namespace _6_03
{
partial class Form1
{
/// 本教程来自:HTTP://www.isstudy.com
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(78, 90);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "绘制椭圆";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//本教程来自:HTTP://www.isstudy.com
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(231, 135);
this.Controls.Add(this.button1);
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.ResumeLayout(false);