Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)qq
published in(发表于) 2014/7/11 9:16:39
c#中GDI+图形图像:GDI+中的多边形使用方法

c#中GDI+图形图像:GDI+中的多边形使用方法|实例

GDI+中的多边形

绘制多边形,需要Graphics对象、Pen对象和Point(或PointF)对象数组。Graphics对象提供DrawPolygon方法,Pen对象存储用于呈现多边形的线条属性,例如,宽度和颜色等,Point(或PointF)对象数组存储多边形的各个顶点。Pen对象和Point(或PointF)对象数组作为参数传递给DrawPolygon方法,该方法为可重载方法,其常用格式有以下两种。

(1)绘制由一组Point结构定义的多边形。

语法:

public void DrawPolygon (

Pen pen,

Point[] points)

参数说明如下。

pen:Pen对象,它确定多边形的颜色、宽度和样式。

points:Point结构数组,这些结构表示多边形的顶点。

(2)绘制由一组PointF结构定义的多边形。

语法:

public void DrawPolygon (

Pen pen,

PointF[] points)

参数说明如下。

pen:Pen对象,它确定多边形的颜色、宽度和样式。

points:PointF结构数组,这些结构表示多边形的顶点。

示例

绘制多边形

本示例实现的是,当程序运行时,单击【绘制多边形】按钮,在窗体中绘制一个多边形。示例运行结果如图1所示网站源代码



图1 绘制多边形

程序代码如下。

Form1窗体中,在【绘制多边形】按钮的Click事件中分别声明Graphics类和Pen类的两个对象,然后实例化6个Point对象,用来作为多边形的顶点,声明一个Point结构数组,并将已经实例化的6个Point对象赋值给该数组,最后调用Graphics对象的DrawPolygon方法绘制一个多边形。【绘制多边形】按钮的Click事件代码如下:

private void button1_Click(object sender, EventArgs e)

{

Graphics graphics = this.CreateGraphics();

Pen myPen = new Pen(Color.Red, 3);

Point point1 = new Point(80, 20);

Point point2 = new Point(40, 50);

Point point3 = new Point(80, 80);

Point point4 = new Point(160, 80);

Point point5 = new Point(200, 50);

Point point6 = new Point(160, 20);

Point[] myPoints ={ point1, point2, point3, point4, point5, point6 };

graphics.DrawPolygon(myPen, myPoints);

}

完整程序代码如下:

★ ★★★★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_05

{

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.Red, 3);

Point point1 = new Point(80, 20);

Point point2 = new Point(40, 50);

Point point3 = new Point(80, 80);

Point point4 = new Point(160, 80);

Point point5 = new Point(200, 50);

Point point6 = new Point(160, 20);

Point[] myPoints ={ point1, point2, point3, point4, point5, point6 };

graphics.DrawPolygon(myPen, myPoints);

}

}

}

★ ★★★★Form1.Designer.cs窗体设计文件完整程序代码网站源代码★★★★★

namespace _6_05

{

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(77, 93);

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);


If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759