2013-08-27 2 views
0

중간에 텍스트가있는 diaganol 스플릿을 사용하여 노란색과 녹색의 두 가지 색상을 가진 사각형을 그리려고합니다.GDI +를 사용하여 드로잉 할 때 영역을 마스크하는 방법

문제는 노란색 위에있을 때 녹색과 검정색 위에 흰색 인 가운데에있는 텍스트를 원합니다.

여기 예 :

enter image description here

내가 그릴 수 있습니다 노란색, 녹색과 검은 색 또는 흰색으로 하나의 텍스트,하지만 어떻게 내가 원하는 효과를 얻을 수있는 텍스트를 마스크 할 수 있습니까?

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 

답변

0

나는이 작업을 수행하기 위해 관리해야 :

  • 흰색 텍스트와 녹색 배경 색상을 포함 임시 비트 맵을 생성

    여기에 지금까지 내 코드입니다.
  • 특정 색으로 반 이상 삼각형을 그립니다.
  • 그런 다음 비트 맵에서 MakeTransparent()를 사용하여 방금 만든 삼각형을 제거하십시오.
  • 그런 다음 원래 비트 맵에 새 비트 맵을 오버레이합니다.
관련 문제