0

真ん中にテキストがあるディアガノール分割を使用して、黄色と緑の2色の長方形を描画しようとしています。

問題は、中央のテキストが緑の上にある場合は白になり、黄色の上にある場合は黒になることです。

ここでの例:

ここに画像の説明を入力

黄色、緑、およびテキストを黒または白で描画できますが、テキストをマスキングして目的の効果を得るにはどうすればよいでしょうか?

これまでの私のコードは次のとおりです。

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 に答える