|
|
Title | Let the user draw an area and fill it with a image using a TextureBrush in VB .NET |
Description | This example shows how to let the user draw an area and fill it with a image using a TextureBrush in VB .NET. The user draws a curve and then the program fills the area defined by the curve with an image. |
Keywords | TextureBrush, image, VB.NET, brush, fill, fill area |
Categories | VB.NET, Graphics |
|
|
The program's MouseDown event handler starts recording points. The MouseMove event handler continues recording points, draing lines between them.
The MouseUp event handler makes a TextureBrush using the image in PictureBox1. It then fills the polygon defined by the points the user drew.
|
|
Private m_Points() As Point
Private m_MaxPoint As Integer
Private m_Drawing As Boolean
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles _
MyBase.MouseDown
m_Drawing = True
Me.CreateGraphics().Clear(Me.BackColor)
m_MaxPoint = 0
ReDim m_Points(m_MaxPoint)
m_Points(m_MaxPoint).X = e.X
m_Points(m_MaxPoint).Y = e.Y
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles _
MyBase.MouseMove
If Not m_Drawing Then Exit Sub
m_MaxPoint += 1
ReDim Preserve m_Points(m_MaxPoint)
m_Points(m_MaxPoint).X = e.X
m_Points(m_MaxPoint).Y = e.Y
Me.CreateGraphics().DrawLine(Pens.Black, _
m_Points(m_MaxPoint - 1), m_Points(m_MaxPoint))
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles _
MyBase.MouseUp
m_Drawing = False
If m_MaxPoint < 1 Then Exit Sub
Dim gr As Graphics = Me.CreateGraphics()
Dim br As New TextureBrush(PictureBox1.Image)
gr.FillPolygon(br, m_Points)
gr.DrawPolygon(Pens.Black, m_Points)
End Sub
|
|
|
|
|
|