Question Filling a region with a picture

groover

Member
Joined
Oct 10, 2009
Messages
23
Programming Experience
1-3
Hi all,

I want to fill two triangles with a texturebrush containing a bitmap image.

The two triangles are drawn .

The left triangle in my code is filled perfect, but the right one does not get filled at all.

I can not figure out what is wrong with my code.



Here is the code:
VB.NET:
   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint

        e.Graphics.DrawImage(ball1, ulcorner)

        'draw arch orange
        Dim startRadius As Single = 60
        Dim endRadius As Integer = 60
        Dim startAngle As Single = 180
        Dim sweepAngle As Single = 180
        Dim rect As New Rectangle(0, 15, 604, 300)
        Dim arcpen As New Drawing.Pen(Color.Orange, 15)

        e.Graphics.DrawArc(arcpen, rect, startAngle, sweepAngle)

        'draw rectangle and line
        Dim boardPen As New Drawing.Pen(Color.BlueViolet, 10)
        Dim boardPen1 As New Drawing.Pen(Color.BlueViolet, 15)

        e.Graphics.DrawRectangle(boardPen1, 4, 1, 600, 800)
        e.Graphics.DrawLine(boardPen, 540, 200, 540, 800)

        ''draw left triangle
        Dim myPen As New Pen(Color.Blue, 5)

        Dim myGraphicsPath As New GraphicsPath


        'vertic. line, left triangle
        myGraphicsPath.AddLine(14, 700, 14, 794)

        e.Graphics.DrawLine(myPen, 14, 700, 14, 794)


        'hor.l line,left triangle
        myGraphicsPath.AddLine(14, 794, 200, 794)

        e.Graphics.DrawLine(myPen, 14, 794, 200, 794)


        'diag. line, left triangle
        myGraphicsPath.AddLine(14, 700, 200, 794)

        e.Graphics.DrawLine(myPen, 14, 700, 200, 794)

        'fill right triangle
        Dim myRegion As New Region(myGraphicsPath)

        Dim myBrush As New TextureBrush(New Bitmap("C:\Users\Hans\documents\visual studio 2010\Projects\Jumpin Jack Flash\Jumpin Jack Flash\Resources\bally.png"))


        e.Graphics.FillRegion(myBrush, myRegion)

        'draw right triangle
        Dim myPen1 As New Pen(Color.Blue, 5)

        Dim myGraphicsPathr As New GraphicsPath

        'vertic. line, right triangle
        myGraphicsPathr.AddLine(534, 700, 534, 794)

        e.Graphics.DrawLine(myPen1, 534, 700, 534, 794)


        'hor.l line, right triangle
        myGraphicsPathr.AddLine(334, 794, 534, 794)

        e.Graphics.DrawLine(myPen1, 334, 794, 534, 794)


        'diag. line, right triangle
        myGraphicsPathr.AddLine(534, 700, 334, 794)

        e.Graphics.DrawLine(myPen1, 534, 700, 334, 794)

        'fill right triangle
        Dim myRegionr As New Region(myGraphicsPathr)

        e.Graphics.FillRegion(myBrush, myRegionr)

    End Sub

Can annyone help?



Thanks,



Groover
 
Hi Groover,

You are drawing one or two sides of the second triangle "backwards", so the path isn't closed. Reverse the order of the points where necessary in the Path.AddLine statements so that you create the path clockwise.

Alternatively, you could make the code a lot simpler if you draw your triangles like this:

VB.NET:
Dim Triangle1 As Point() = {[I]p1, p2, p3[/I]}
e.Graphics.FillPolygon(myBrush, Triangle1)
e.Graphics.DrawPolygon(myPen, Triangle1)
where p1, p2 and p3 are the corner points. Then there is no need to define a path or a region.

Vic
 

Latest posts

Back
Top