前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CSharp代码示例每日一讲: 在GDI+中使用填充Fill方法

CSharp代码示例每日一讲: 在GDI+中使用填充Fill方法

作者头像
程序你好
发布2018-07-20 16:39:42
1.3K0
发布2018-07-20 16:39:42
举报
文章被收录于专栏:程序你好程序你好

CSharp代码示例每日一讲,为刚刚学习编程的人准备,利用最简单的代码介绍CSharp编程知识!

画笔Pen被用来绘制图形、形状,画刷用来填充图形形状的内部。今天,我们将介绍图形类的填充方法。您只能填充某些图形形状;在Graphics类中只有少量的填充方法。

FillCloseCurve方法

FillCloseCurve填充曲线的内部,填充曲线的第一个参数是画刷,它可以是实心画刷solid brush, 影线画刷hatch brush, 或者渐变画刷gradient brush。第二个参数是绘制点的数组,第三和第四参数是可选的。第三个参数是FillMode填充模式,填充模式是个枚举类型,表示填充模式。FillMode枚举指定填充闭合路径的方式。它有两种模式:交替 alternate 或围绕winding。默认模式是交替的。填充模式只在曲线与自身相交时才重要。

要使用FillClosed填充一个封闭的曲线,应用程序首先创建一个画笔对象和一个指向该曲线的点数组。然后应用程序可以设置填充模式和张力(可选),最后调用FillClosedCurve方法进行填充。

示例:创建了一个PointF结构数组和一个SolidBrush对象,并调用FillClosedCurve。

使用FillClosedCurve填充闭合曲线

代码语言:javascript
复制
 private void Form1_Paint (object sender,System.Windows.Forms.PaintEventArgs e){
            // Create a pen
            Pen bluePen = new Pen (Color.Blue, 1);
            // Create an array of points
            PointF pt1 = new PointF(40.0F, 50.0F);
            PointF pt2 = new PointF(50.0F, 75.0F);
            PointF pt3 = new PointF(100.0F, 115.0F);
            PointF pt4 = new PointF(200.0F, 180.0F);
            PointF pt5 = new PointF(200.0F, 90.0F);
                        
            PointF[] ptsArray = {pt1, pt2, pt3, pt4, pt5};
            
            // Fill a closed curve
            float tension = 1.0F;
            FillMode flMode = FillMode.Alternate;
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            e.Graphics.FillClosedCurve(blueBrush,ptsArray,flMode,tension);
            
            // Dispose of object
            blueBrush.Dispose();
}

填充方法:

Method

Description

FillCloseCurve

Fills the interior of a closed cardinal spline curve defined by an array of Point structures.

FillEllipse

Fills the interior of an ellipse defined by a bounding rectangle specified by a pair of coordinates, a width and a height.

FillPath

Fills the interior of a GraphicsPath object.

FillPie

Fills the interior of a pie section defined by an ellipse specified by a pair of coordinates, a width, a height, and two radial lines.

FillPolygon

Fills the interior of a polygon defined by an array of points specified by Point structures.

FillRectangle

Fills the interior of a rectangle specified by a pair of a coordinates, a width, and a height.

FillRectangles

Fills the interiors of a series of rectangles specified by Rectangle structures.

FillRegion

Fills the interiors of a Region object.

FillEllipse方法

FillEllipse填充椭圆的内部。它使用画刷对象和矩形坐标。

要使用FillEllipse填充椭圆,应用程序将创建一个画刷和一个矩形,并调用FillEllipse。

下面代码创建三个笔刷,并调用FillEllipse用画刷填充椭圆。

代码语言:javascript
复制
private void Form1_Paint(object sender,System.Windows.Forms.PaintEventArgs e){
            Graphics g = e.Graphics;
            
            // Create brushes
            SolidBrush redBrush = new SolidBrush(Color.Red);
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            SolidBrush greenBrush = new SolidBrush(Color.Green);
            
            //Create a rectangle
            Rectangle rect = new Rectangle(80, 80, 50, 50);
            
            // Fill ellipses
            g.FillEllipse(greenBrush, 40.0F, 40.0F, 130.0F, 130.0F);
            g.FillEllipse(blueBrush, 60, 60, 90, 90);
            g.FillEllipse(greenBrush, 100.0F, 90.0F, 10.0F, 30.0F);
            
            // Dispose of objects
            blueBrush.Dispose();
            redBrush.Dispose();
            greenBrush.Dispose();
}

结果如下:

FillPath方法

FillPath填充图形路径的内部,为此,应用程序创建画刷和图形对象,并调用FillPath,该方法以画刷和图形路径作为参数。

创建GraphicsPath和SolidBrush对象,并调用FillPath,代码如下:

代码语言:javascript
复制
private void Form1_Paint(object sender,
            System.Windows.Forms.PaintEventArgs e)        {
            
                        // Create a solid brush
                        SolidBrush greenBrush = new SolidBrush(Color.Green);
            
                        // Create a graphics path
                        GraphicsPath path = new GraphicsPath();
            
                        // Add a line to the path
                        path.AddLine(20, 20, 103, 80);
            
                        // Add an ellipse to the path
                        path.AddEllipse(100, 50, 100, 100);
            
                        // Add three more lines
                        path.AddLine(195, 80, 300, 80);
                        path.AddLine(200, 100, 300, 100);
                        path.AddLine(195, 120, 300, 120);
            
                        // Create a rectangle and call AddRectangle
                        Rectangle rect = new Rectangle(50, 150, 300, 50);
                        path.AddRectangle(rect);
            
                        // Fill path
                        e.Graphics.FillPath(greenBrush, path);
            
                        // Dispose of object
                        greenBrush.Dispose();
           }
}

如图所示,填充方法填充图形路径的所有覆盖区域。

FillPie方法

FillPie用指定的画笔填充pie部分。它有四个参数:画刷,椭圆的矩形,开始和扫角。下面的代码调用FillPie。

代码语言:c#
复制
g.FillPie(new SolidBrush (Color.Red),
0.0F, 0.0F, 100, 60, 0.0F, 90.0F);

FillPolygon方法

FillPolygon用指定的画刷填充一个多边形。它需要三个参数:一个画刷、一个绘制点数组和一个填充模式。FillMode枚举定义路径内部的填充模式。它提供了两种填充模式:交替和环绕。默认模式是交替的。

在我们的应用中,我们将使用一个影线画刷。到目前为止,我们只看到了一把实心的刷子。实心笔刷是一种只有一种颜色的笔刷。HatchBrush类表示影线画刷,影线刷是一种带有影线风格和两种颜色的刷。

下面的代码使用FillPolygon用围绕模式填充一个多边形。

代码语言:c#
复制
Graphics g = e.Graphics;

// Create a solid brush
SolidBrush greenBrush = new SolidBrush (Color.Green);

// Create points for polygon
PointF p1 = new PointF (40.0F, 50.0F);
PointF p2 = new PointF (60.0F, 70.0F);
PointF p3 = new PointF (80.0F, 34.0F);
PointF p4 = new PointF (120.0F, 180.0F);
PointF p5 = new PointF (200.0F, 150.0F);
PointF[] ptsArray =
{
p1, p2, p3, p4, p5
};

// Draw polygon
e.Graphics.FillPolygon (greenBrush, ptsArray);

// Dispose of objects
greenBrush.Dispose();

填充矩形

用画刷填充矩形。该方法以画刷和矩形作为参数。填充矩形使用画刷填充指定的一系列矩形,并使用画刷和矩形数组。这些方法也有额外的选项。例如,如果您使用的是HatchStyle画笔,您可以指定背景和前景颜色。

注意:HatchBrush类是在System.Drawing中定义的。Drawing2D命名空间。

下面代码使用fill矩形填充两个矩形。一个长方形充满了一个影线画刷,另一个用一个实心刷子。

代码语言:c#
复制
private void Form1_Paint(object sender,
            System.Windows.Forms.PaintEventArgs e)
        {

 // Create brushes
 SolidBrush blueBrush = new SolidBrush(Color.Blue);

 // Create a rectangle
 Rectangle rect = new Rectangle(10, 20, 100, 50);

 // Fill rectangle
            e.Graphics.FillRectangle(new HatchBrush(HatchStyle.BackwardDiagonal, Color.Yellow,Color.Black), rect);
            e.Graphics.FillRectangle(blueBrush, new Rectangle(150, 20, 50, 100));

 // Dispose of object
            blueBrush.Dispose();
        }

FillRegion使用画刷填充指定区域。该方法以画刷和区域作为输入参数。

下面代码从矩形创建一个区域对象,并调用FillRegion来填充该区域。

代码语言:c#
复制
Rectangle rect = new Rectangle (20, 20, 150, 100);
Region rgn = new Region(rect);
e.Graphics.FillRegion(new SolidBrush (Color.Green), rgn);

希望本文能够帮助您理解GDI+中的填充方法。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-06-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序你好 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档