Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to draw random patterns in a picture box that has a black panel over the surface hiding the drawing below. But when I make the panel disappear (panel.visible = false), the graphics in the picture box UNDER the panel are also erased. In the code below (most of which was grabbed from a VB10 book) the process works if I put the for...next loop creating the ellipses in the Private Sub picCanvas_MouseMove. The random ellipse patterns persist when the Hide Panel Button is clicked. But if I create the ellipse patterns in the Private Sub MakeGraphics_Click (click a button on the form), the ellipse patterns under the panel are erased when the panel.visible = false.

In my program I do not need the mouse down, etc events to draw squiggly lines, I only need to create the graphics when a form button is clicked.
I am probably missing something with representing the bmp where the images are stored but I am not sure what.

Thanks for any help!!!
Gary V

VB.NET
Public Class Form1
    Dim PenColor As New Pen(Color.Red, 2)
    Dim X As Long  ' x,y,w,h set the random position of the ellipses
    Dim Y As Long
    Dim W As Long
    Dim H As Long
    ' The Bitmap and Graphics objects we will draw on.
    Private m_Bitmap As Bitmap
    Private m_Graphics As Graphics
    ' Used for scribbling.
    Private m_Drawing As Boolean
    Private m_X As Integer
    Private m_Y As Integer

    ' Make the initial blank image.
    Private Sub Form1_Load() Handles MyBase.Load
        Call MakeNewBitmap()
    End Sub

    ' Make a new blank image.
    Private Sub mnuFileClear_Click() Handles mnuFileClear.Click
        Call MakeNewBitmap()
    End Sub

    ' Make a new Bitmap to fit the canvas.
    Private Sub MakeNewBitmap()
        ' Get the drawing surface's size.
        Dim wid As Integer = picCanvas.ClientSize.Width
        Dim hgt As Integer = picCanvas.ClientSize.Height

        ' Make a Bitmap and Graphics to fit.
        m_Bitmap = New Bitmap(wid, hgt)
        m_Graphics = Graphics.FromImage(m_Bitmap)

        ' Clear the drawing area.
        m_Graphics.Clear(Me.BackColor)

        ' Display the result.
        picCanvas.Image = m_Bitmap
    End Sub

    ' Start scribbling.
    Private Sub picCanvas_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseDown
        m_Drawing = True
        m_X = e.X
        m_Y = e.Y
      
    End Sub

    ' Continue scribbling.
    Private Sub picCanvas_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseMove
        If Not m_Drawing Then Exit Sub

        'For testing Purposes: Make some random elipses on screen on MouseMove

        For Temp = 1 To 8
            Randomize()
            X = 5 + (Rnd() * (m_Bitmap.Width - 30))
            Y = 5 + (Rnd() * (m_Bitmap.Height - 30))
            H = 2 + (Rnd() * 20)
            W = 2 + (Rnd() * 20)
            m_Graphics.DrawEllipse(PenColor, X, Y, W, H)
        Next

        m_Graphics.DrawLine(Pens.Black, m_X, m_Y, e.X, e.Y)
        m_X = e.X
        m_Y = e.Y
        ' Display the result.
        picCanvas.Refresh()
    End Sub

' Stop scribbling.
    Private Sub picCanvas_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseUp
        m_Drawing = False
    End Sub

    Private Sub MakeGraphics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MakeGraphics.Click
        'Make some random ellipses on screen when button is clicked
        m_Graphics = picCanvas.CreateGraphics
        For Temp = 1 To 8
            Randomize()
            X = 5 + (Rnd() * (picCanvas.Width - 30))
            Y = 5 + (Rnd() * (picCanvas.Height - 30))
            'Minimum length,width = 20, max = 220
            H = 2 + (Rnd() * 20)
            W = 2 + (Rnd() * 20)
            m_Graphics.DrawEllipse(PenColor, X, Y, W, H)
        Next
    End Sub

    Private Sub HidePanel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HidePanel.Click
        If Panel1.Visible = True Then Panel1.Visible = False Else Panel1.Visible = True
    End Sub
End Class
Posted
Updated 23-Jan-11 4:05am
v2
Comments
Manfred Rudolf Bihy 23-Jan-11 10:06am    
Added code tags.

The only difference I could spot is that in your button click handler you're not using the already initialized m_Grphics Graphics context but are creating a new one with m_Graphics = picCanvas.CreateGraphics. Since you didn't do that in your MouseMove event handler which you said yields the desired results I suspect that this is the issue.

Best Regards,
Manfred
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Jan-11 12:09pm    
This is a big mistake already (probably due to misconception).
Well spotted, my 5.
GaryV100 23-Jan-11 13:39pm    
Just curious, why is this a big mistake, whats the misconception????
Gary V
To clarify further, 'creategraphics' is the work of the devil. It creates a drawing surface which will disappear the next time the form is invalidated. You should always draw in your paint event, unless you're drawing somethign like a rubber band, that is, highlighting a selection or somethign else where what you're drawing is designed to be unstable and likely to disappear.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900