【问题标题】:How to mask off a area when drawing using GDI+使用 GDI+ 绘图时如何屏蔽区域
【发布时间】:2013-08-27 09:11:00
【问题描述】:

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

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

这里有一个例子:

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

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

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

【问题讨论】:

    标签: .net winforms graphics gdi+ masking


    【解决方案1】:

    我已经通过以下方式做到了:

    • 创建包含绿色背景颜色和白色文本的临时位图。
    • 用特定颜色在一半上画一个三角形
    • 然后在位图上使用 MakeTransparent() 来移除我刚刚创建的三角形。
    • 然后我将新的位图叠加到原来的正方形上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 2021-11-23
      • 2010-09-26
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      相关资源
      最近更新 更多