路径填充未填满整个图形路径?

4
尝试填充中心区域,不确定哪里出了问题。

这里输入图片描述

Imports System.Drawing.Drawing2D

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim gp As GraphicsPath = CreatePath(New Rectangle(New Point(200, 200), New Size(250, 100)))
        e.Graphics.FillPath(New SolidBrush(Color.LightBlue), gp)
        e.Graphics.DrawPath(New Pen(Color.Black), gp)

    End Sub

    Private Function CreatePath(ByVal area As Rectangle) As GraphicsPath

        Dim gp As New GraphicsPath()
        Dim rect As Rectangle = New Rectangle(area.Location, New Size(area.Width, area.Height \ 3))
        gp.AddEllipse(rect)

        Dim hadj As Integer = area.Height \ 4
        gp.AddLine(New Point(area.X, area.Y + area.Height - hadj), New Point(area.X, area.Y + (rect.Height \ 3) + 8))

        Dim gh As Integer = area.Width \ 4
        Dim pts(4) As PointF
        pts(0) = New PointF(area.X, area.Y + area.Height - hadj)
        pts(1) = New Point(area.X + gh, area.Y + area.Height - (hadj \ 2))
        pts(2) = New Point(area.X + gh * 2, area.Y + area.Height - (hadj \ 3))
        pts(3) = New Point(area.X + gh * 3, area.Y + area.Height - (hadj \ 2))
        pts(4) = New Point(area.X + area.Width, area.Y + area.Height - hadj)
        gp.AddCurve(pts)

        gp.AddLine(New PointF(area.X + area.Width, area.Y + (rect.Height \ 3) + 9), New PointF(area.X + area.Width, area.Y + area.Height - hadj))
        Return gp
    End Function
End Class
2个回答

4
尝试以下操作。默认情况下,GraphicsPath使用交替填充模式,交替地添加/减去填充。Winding填充模式使用方向来添加(顺时针)或减去(逆时针)填充。
Private Function CreatePath(ByVal area As Rectangle) As GraphicsPath
    '* Set fill mode to winding (see FillMode Help)
    Dim gp As New GraphicsPath(FillMode.Winding)

    Dim rect As Rectangle = New Rectangle(area.Location, New Size(area.Width, area.Height \ 3))
    gp.AddEllipse(rect)

    '* Reorder points so that path goes clockwise (see FillMode.Winding Help)

    Dim hadj As Integer = area.Height \ 4
    'Left side bottom -> top
    'gp.AddLine(New Point(area.X, area.Y + area.Height - hadj), New Point(area.X, area.Y + (rect.Height \ 3) + 8))

    '* Right side top -> bottom (clockwise)
    '* NOTE: Top Y value was a bit too high, adjusted by -3.  Adjust calculation or maybe use floating point math and used rounding, ceil, or floor
    gp.AddLine(New PointF(area.X + area.Width, area.Y + (rect.Height \ 3) + 9 - 3), New PointF(area.X + area.Width, area.Y + area.Height - hadj))

    Dim gh As Integer = area.Width \ 4
    Dim pts(4) As PointF
    'pts(0) = New PointF(area.X, area.Y + area.Height - hadj)
    'pts(1) = New Point(area.X + gh, area.Y + area.Height - (hadj \ 2))
    'pts(2) = New Point(area.X + gh * 2, area.Y + area.Height - (hadj \ 3))
    'pts(3) = New Point(area.X + gh * 3, area.Y + area.Height - (hadj \ 2))
    'pts(4) = New Point(area.X + area.Width, area.Y + area.Height - hadj)
    '* Reordered points (clockwise)
    pts(0) = New Point(area.X + area.Width, area.Y + area.Height - hadj)
    pts(1) = New Point(area.X + gh * 3, area.Y + area.Height - (hadj \ 2))
    pts(2) = New Point(area.X + gh * 2, area.Y + area.Height - (hadj \ 3))
    pts(3) = New Point(area.X + gh, area.Y + area.Height - (hadj \ 2))
    pts(4) = New PointF(area.X, area.Y + area.Height - hadj)
    gp.AddCurve(pts)

    'Right side top -> bottom
    'gp.AddLine(New PointF(area.X + area.Width, area.Y + (rect.Height \ 3) + 9), New PointF(area.X + area.Width, area.Y + area.Height - hadj))

    '* Left side bottom -> top (clockwise)
    '* NOTE: Top Y value was a bit too high, adjusted by -2.  Adjust calculation or maybe use floating point math and used rounding, ceil, or floor
    gp.AddLine(New Point(area.X, area.Y + area.Height - hadj), New Point(area.X, area.Y + (rect.Height \ 3) + 8 - 2))

    Return gp
End Function

2
另一种方法是展示GraphicsPath对象的Reset()方法。
由于圆柱可以看作是两个椭圆和一个矩形的组合(透视创建了三维物体的错觉),因此这些形状首先被填充,然后外部部分(形状边框)被独立绘制。
如果需要,这将最终允许使用不同的画笔来绘制图形。
结果的示例:

GraphicsPath Cylinder


Private upperRect As Rectangle = New Rectangle(New Point(10, 10), New Size(180, 80))
Private lowerRect As Rectangle = New Rectangle(New Point(10, 100), New Size(180, 80))

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias

    Dim middleRect As Rectangle = CylinderMiddleRect(upperRect, lowerRect)

    Using gp As New GraphicsPath(FillMode.Winding)
        gp.AddEllipse(upperRect)
        gp.AddRectangle(middleRect)
        gp.AddEllipse(lowerRect)
        e.Graphics.FillPath(Brushes.LightBlue, gp)
        gp.Reset()

        gp.AddArc(lowerRect, 0, 180)
        gp.AddEllipse(upperRect)
        gp.AddLine(New Point(middleRect.X, middleRect.Y), New Point(upperRect.X, middleRect.Bottom))
        gp.CloseFigure()
        gp.AddLine(New Point(middleRect.Right, middleRect.Y), New Point(middleRect.Right, middleRect.Bottom))
        gp.CloseFigure()
        e.Graphics.DrawPath(Pens.Black, gp)
    End Using
End Sub

Private Function CylinderMiddleRect(upperRect As Rectangle, lowerRect As Rectangle) As Rectangle
    Dim distance = (lowerRect.Y + (lowerRect.Height \ 2)) - (upperRect.Y + (upperRect.Height \ 2))

    Return New Rectangle(New Point(upperRect.X, upperRect.Y + (upperRect.Height \ 2)),
                         New Size(upperRect.Width, distance))
End Function

非常好,比我的好多了。 - user117499
这只是抗锯齿:e.Graphics.SmoothingMode = SmoothingMode.AntiAlias :) - Jimi
是的,我知道那个。但是你的看起来更好。有没有办法接受一个输入矩形并基于它进行绘制? - user117499
实际上,我之所以在Paint事件外声明了upperRectlowerRect矩形,就是为了这个目的。然后,CylinderMiddleRect方法计算两个矩形之间的距离。这个距离用于构建内部矩形,其边提供连接线。因此,是的,您可以使用任何大小的两个矩形(有意义的)。在这里,upperRectlowerRect之间的差异仅在于Y位置。 - Jimi
啊,这些矩形除了位置不同其他都一样。 - user117499
是的,没错。Y 距离决定了圆柱体的形状,而矩形的高度提供了透视 - Jimi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接