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

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

c#中GDI+图形图像:GDI+中的直线和矩形使用方法

GDI+中的直线和矩形

1.绘制直线

绘制直线时,可以调用Graphics类中的DrawLine方法,该方法为可重载方法,它主要用来绘制一条连接由坐标对指定的两个点的线条,其常用格式有以下两种。

(1)绘制一条连接两个Point结构的线。

语法:

public void DrawLine (

Pen pen,

Point pt1,

Point pt2)

参数说明如下。

pen:Pen对象,它确定线条的颜色、宽度和样式。

pt1:Point结构,它表示要连接的第一个点。

pt2:Point结构,它表示要连接的第二个点。

(2)绘制一条连接由坐标对指定的两个点的线条。

语法:

public void DrawLine (

Pen pen,

int x1,

int y1,

int x2,

int y2)

DrawLine方法中各参数及说明如表1所示。



表1 DrawLine方法参数及说明

示例

绘制直线

本示例实现的是,当程序运行时,单击【绘制直线】按钮,在窗体中的指定位置绘制一条直线。示例运行结果如图1所示。



图1 绘制直线

程序代码如下。

Form1窗体中,在【绘制直线】按钮的Click事件中分别声明Graphics类和Pen类的两个实例对象,然后调用Graphics对象的DrawLine方法在窗体中绘制一条直线。【绘制直线】按钮的Click事件代码如下网站源代码

private void button1_Click(object sender, EventArgs e)

{//本教程来自:HTTP://www.isstudy.com

Graphics graphics = this.CreateGraphics();

Pen myPen = new Pen(Color.Blue, 2);

graphics.DrawLine(myPen, 50, 30, 170, 30);

}

完整程序代码如下:

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

{

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

graphics.DrawLine(myPen, 50, 30, 170, 30);

}

}

}

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

namespace _6_01

{

partial class Form1

{

///



/// 必需的设计器变量。

///


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

//本教程来自:HTTP://www.isstudy.com

// button1

//

this.button1.Location = new System.Drawing.Point(63, 61);

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

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(212, 106);

this.Controls.Add(this.button1);

this.MaximizeBox = false;

this.Name = "Form1";

this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;

}

}

★ ★★★★Program.cs主程序文件完整程序代码网站源代码★★★★★

using System;


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





QQ:154298438
QQ:417480759