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

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

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

GDI+中的画笔

闭合的形状(例如,矩形或椭圆)由轮廓和内部组成。用钢笔绘制出轮廓,并用画笔填充其内部。GDI+提供了几种填充闭合形状内部的画笔类:SolidBrush、HatchBrush、TextureBrush、LinearGradientBrush和PathGradientBrush。所有这些类都是从Brush类继承的。

1.实心画笔

使用实心画笔时,需要使用SolidBrush类,该类主要用来使用定义的单色画笔填充图形形状,如矩形、椭圆、扇形、多边形和封闭路径等。

示例

实心画笔的使用

本示例中,当程序运行时,单击【实心画笔】按钮,在窗体中绘制一个使用指定颜色填充的椭圆。示例运行结果如图1所示。



图1 实心画笔的使用

Form1窗体中,在【实心画笔】按钮的Click事件中分别声明Graphics类和SolidBrush类的两个实例对象,调用Graphics对象的FillEllipse方法在窗体中绘制一个使用指定颜色填充的椭圆。【实心画笔】按钮的Click事件代码如下:

private void button1_Click(object sender, EventArgs e)

{

Graphics graphics = this.CreateGraphics();

SolidBrush mySolidBrush = new SolidBrush(Color.Green);

graphics.FillEllipse(mySolidBrush, 70, 20, 100, 50);

}

完整程序代码如下:

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

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

Graphics graphics = this.CreateGraphics();

SolidBrush mySolidBrush = new SolidBrush(Color.Green);

graphics.FillEllipse(mySolidBrush, 70, 20, 100, 50);

}

}

}

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

namespace _6_09

{

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

//

// button1

//

this.button1.Location = new System.Drawing.Point(82, 83);

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(238, 130);

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;

using System.Collections.Generic;

using System.Windows.Forms;

namespace _6_09

{

static class Program

{

///

/// 应用程序的主入口点。

///


[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

}

}

}

2.阴影画笔

使用阴影画笔时,需要使用HatchBrush类,该类位于System.Drawing.Drawing2D命名空间内,它主要使用阴影样式、前景色和背景色定义矩形画笔。


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





QQ:154298438
QQ:417480759