0

我正在尝试使用中间带有文本的二甘醇拆分绘制一个具有黄色和绿色两种颜色的矩形。

问题是我希望中间的文本在绿色上方为白色,在黄色上方为黑色。

这里有一个例子:

在此处输入图像描述

我可以用黑色或白色绘制黄色、绿色和文本,但是如何屏蔽文本以获得所需的效果?

到目前为止,这是我的代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim colour1Brush As New SolidBrush(Color.Yellow)
    Dim colour2Brush As New SolidBrush(Color.Green)
    Dim g As Graphics = Me.CreateGraphics
    Dim rect As New Rectangle(New Point(50, 50), New Size(100, 50))
    With g
        'draw the background yellow
        .FillRectangle(colour1Brush, rect)
        'put in the black text
        DrawText(g, rect, "12", Brushes.Black)
        'draw the green triangle
        Dim triPoints(2) As Point
        triPoints(0) = New Point(rect.Left + rect.Width, rect.Top)
        triPoints(1) = New Point(rect.Left + rect.Width, rect.Top + rect.Height)
        triPoints(2) = New Point(rect.Left, rect.Top + rect.Height)
        .FillPolygon(colour2Brush, triPoints)
        'now we need white text that doesn't overwrite the existing black text
        '?????? DrawText(g, rect, "12", Brushes.White)
    End With
End Sub

Private Sub DrawText(ByRef g As Graphics, ByVal rect As Rectangle, ByVal text As String, ByVal brush As Brush)
    Dim fnt As New Font("Segoe UI", 8)
    Dim textSize As SizeF = g.MeasureString(text, fnt)
    Dim x As Integer = CInt(rect.Left + ((rect.Width / 2) - (textSize.Width / 2)))
    Dim y As Integer = CInt(rect.Height + ((rect.Height / 2) - (textSize.Height / 2)))
    g.DrawString(text, fnt, brush, New Point(x, y))
End Sub
4

1 回答 1

0

我设法通过以下方式做到这一点:

  • 创建一个包含绿色背景颜色和白色文本的临时位图。
  • 用特定颜色在一半上画一个三角形
  • 然后在位图上使用 MakeTransparent() 来删除我刚刚创建的三角形。
  • 然后我将新的位图覆盖到原来的正方形上。
于 2013-09-05T11:55:45.620 回答