Resolved Panel drawing as form background

groover

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

I am drawing on a panel and use my drawing as background image on a form.

All the things I draw are on the panel are OK, but on the form a part is missing.

I guess it takes to long for this part to paint.

How can I make my code to wait until all the painting is done?

The part that is miissing is commented draw arc top.

Here is my code:

VB.NET:
 Private Sub Panel2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel2.Paint
        e.Graphics.DrawImage(ball1, ulcorner)

        If start = False Then

            Dim arcbrush As New TextureBrush(New Bitmap(My.Resources.bally))
            Dim silverbrush As New TextureBrush(New Bitmap(My.Resources.Crystalsilver))
            Dim topbrush As New SolidBrush(Color.DarkOliveGreen)

            'draw arc top

            Dim arcpen As New Drawing.Pen(arcbrush, 25)

            arcpen.Alignment = PenAlignment.Inset
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality

            topArch.StartFigure()
            topArch.AddLine(6, 6, 6, 115)
            topArch.AddArc(ArcRect, startAngle, sweepAngle)
            topArch.AddLine(603, 115, 603, 6)
            topArch.CloseFigure()
            reg = New Region(topArch)
            e.Graphics.FillRegion(topbrush, reg)
            e.Graphics.DrawPath(arcpen, topArch)

            '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.BlueViolet, 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(My.Resources.bally))
            e.Graphics.FillRegion(myBrush, myRegion)

            'draw right triangle
            Dim myPen1 As New Pen(Color.Blue, 5)
            myPen1.Alignment = PenAlignment.Center
            myPen.LineJoin = LineJoin.Round
            Dim myGraphicsPathr As New GraphicsPath

            'vertic. line, right triangle
            myGraphicsPathr.AddLine(534, 700, 534, 794)
            e.Graphics.DrawLine(myPen1, 534, 794, 534, 700)

            'hor.l line, right triangle
            myGraphicsPathr.AddLine(534, 794, 334, 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)

            'draw rightflipper first time

            Dim myPenF As New Pen(Color.OldLace, 5)
            Dim mybrushF As New SolidBrush(Color.OrangeRed)
            Dim p1 As New Point
            p1 = endr
            Dim p2 As New Point
            p2 = pivotr
            Dim p3 As New Point(pivotr.X + 10, pivotr.Y + 20)
            Dim p4 As New Point(endr.X, endr.Y + 10)
            Dim polygonR As Point() = {p1, p2, p3, p4}

            If flipperRightMoveU = False Then 'makes polygonR drawn for fist time
                e.Graphics.TranslateTransform(365, 718)
                e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                myPenF.LineJoin = LineJoin.Round
                e.Graphics.FillPolygon(mybrushF, polygonR)
                e.Graphics.DrawPolygon(myPenF, polygonR)

            End If

            ' Use panel as formbackgroundImage

            Dim panelBitmap As New Bitmap(Panel2.Width, Panel2.Height)
            Panel2.DrawToBitmap(panelBitmap, Panel2.Bounds)

            Me.BackgroundImage = panelBitmap
            Panel2.Hide()

        End If

    End Sub
Thanks in advance,

Groover
 
Last edited:
Why aren't you drawing to the Bitmap directly? (Graphics.FromImage)
 
Hi JohnH

I do not know how to do that, the image does not exist before it is drawn.

Can You give me an example to get the drawings into an image?

thanks ,

Groover
 
Hi JohnH,

I have it working now.

I always thought the FromImage Methode uses an existing image, but here How to: Create a Bitmap at Run Time I found the right example.

I got rid of the panel and call new sub from Form1_Load:

VB.NET:
 Private Sub FrmBackgroundImage()


        Dim FrmImageBitmap As New Bitmap(Me.Width, Me.Height)
        Dim imagefile As Graphics = graphics.FromImage(FrmImageBitmap)

        Dim arcbrush As New TextureBrush(New Bitmap(My.Resources.bally))

        If start = False Then
            Dim silverbrush As New TextureBrush(New Bitmap(My.Resources.Crystalsilver))
            Dim topbrush As New SolidBrush(Color.DarkOliveGreen)

            'draw arc top

            Dim arcpen As New Drawing.Pen(arcbrush, 25)

            arcpen.Alignment = PenAlignment.Inset
            imagefile.SmoothingMode = SmoothingMode.HighQuality

            topArch.StartFigure()
            topArch.AddLine(6, 6, 6, 115)
            topArch.AddArc(ArcRect, startAngle, sweepAngle)
            topArch.AddLine(603, 115, 603, 6)
            topArch.CloseFigure()
            reg = New Region(topArch)
            imagefile.FillRegion(topbrush, reg)
            imagefile.DrawPath(arcpen, topArch)

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

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

            ''draw left triangle
            Dim myPen As New Pen(Color.BlueViolet, 5)
            Dim myGraphicsPath As New GraphicsPath

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

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

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

            'fill right triangle
            Dim myRegion As New Region(myGraphicsPath)
            Dim myBrush As New TextureBrush(New Bitmap(My.Resources.bally))
            imagefile.FillRegion(myBrush, myRegion)

            'draw right triangle
            Dim myPen1 As New Pen(Color.Blue, 5)
            myPen1.Alignment = PenAlignment.Center
            myPen.LineJoin = LineJoin.Round
            Dim myGraphicsPathr As New GraphicsPath

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

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

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

            'fill right triangle
            Dim myRegionr As New Region(myGraphicsPathr)
            imagefile.FillRegion(myBrush, myRegionr)

            'draw rightflipper first time

            Dim myPenF As New Pen(Color.OldLace, 5)
            Dim mybrushF As New SolidBrush(Color.OrangeRed)
            Dim p1 As New Point
            p1 = endr
            Dim p2 As New Point
            p2 = pivotr
            Dim p3 As New Point(pivotr.X + 10, pivotr.Y + 20)
            Dim p4 As New Point(endr.X, endr.Y + 10)
            Dim polygonR As Point() = {p1, p2, p3, p4}

            If flipperRightMoveU = False Then 'makes polygonR drawn for fist time
                imagefile.TranslateTransform(365, 718)
                imagefile.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                myPenF.LineJoin = LineJoin.Round
                imagefile.FillPolygon(mybrushF, polygonR)
                imagefile.DrawPolygon(myPenF, polygonR)

            End If

            ' Use FrmImageBitmap as formbackgroundImage


            Me.BackgroundImage = FrmImageBitmap

        End If
    End Sub
Thank You very much,

Groover
 
I always thought the FromImage Methode uses an existing image
Yeah, the image returned from "New Bitmap" constructor is thereafter an existing image. Bitmap class derives from Image class.

You need to Dispose all graphics objects you create when done with them. (imagefile, arcbrush, silverbrush, topbrush, arcpen etc)
When you create an instance of a class that implements IDisposable interface you must call Dispose method before releasing last reference to that object. That also includes a Graphics instance returned by Graphics.FromImage method as explained in help page.
 
Back
Top