PenAlignment.Outset无效

时间:2017-11-05 14:22:26

标签: .net graphics drawing

我正在使用Pen.Alignment = PenAlignment.Outset绘制一个矩形到PictureBox(具有绿色背景)。

这是代码:

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint

    Dim iTop As Integer = 1
    Dim iLeft As Integer = 1
    Dim iRight As Integer = 3
    Dim iBottom As Integer = 3

    Dim r As Rectangle = Rectangle.FromLTRB(iLeft, iTop, iRight, iBottom)

    Using nPen As Pen = New Pen(Color.Black)
        nPen.Alignment = PenAlignment.Outset
        e.Graphics.PageUnit = GraphicsUnit.Pixel
        nPen.Width = 1
        e.Graphics.DrawRectangle(nPen, r)
    End Using

End Sub

但是,PenAlignment没有任何效果。 这是输出:

enter image description here

不是用“outset”绘制矩形,而是直接绘制矩形所在的位置。我希望在矩形周围绘制黑线。

所以第一个黑点应该是0-0,而不是1-1。

这里可能出现什么问题?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题并没有得到答案所以我不得不解决它。

这就是我的所作所为:

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint

    'Main Shape Values
    Dim x = 100
    Dim y = 100
    Dim wdth = 300
    Dim hght = 200
    Dim shP = 2

    'Main Shape
    Dim ShapePen As New Pen(Color.Black, shP)
    Dim Shape As New Rectangle(x, y, wdth, hght)
    e.Graphics.DrawRectangle(ShapePen, Shape)

    'Values For the Alignment
    Dim P_Thck = 20
    Dim R_ThickL = (P_Thck / 2) + (shP / 2)
    Dim R_ThickS = R_ThickL * 2

    Dim AlignmentPen As New Pen(Color.Red, P_Thck)
    'Dim Align_Outset As New Rectangle(x - R_ThickL, y - R_ThickL, wdth + R_ThickS, hght + R_ThickS)
    Dim Align_Inset As New Rectangle(x + R_ThickL, y + R_ThickL, wdth - R_ThickS, hght - R_ThickS)

    'e.Graphics.DrawRectangle(AlignmentPen, Align_Outset)
    e.Graphics.DrawRectangle(AlignmentPen, Align_Inset)

End Sub

我使用两种形状。 一个是我的工作形状,另一个是我调整对齐图形的那个。

Inset

Outset