Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
OK, new question as a follow up problem from my previous question about Bit Map Images Under A Panel
The advice to change to m_Graphics = Graphics.FromImage(m_Bitmap) and to use the Paint event seems to work. I have 20 small panels over the picture box to hide the randomly generated graphics. But the graphics will only draw on the picCanvas picture box if the overlaying panels are there. No panels, no graphics show up. And if you put one small panel anywhere on the picCanvas, the graphics only draw under the panel. when I debug the program with a breakpoint and check m_Bitmap.Width and m_Bitmap.Heght they are equal to the size of the picCanvas picture box. Now, I can make this work for my needs but I don't understand it???

Any thoughts???

Here is the revised code:
VB
Public Class Form1
    Dim PenColor As New Pen(Color.Red, 2)
    Dim X As Long
    Dim Y As Long
    Dim W As Long
    Dim H As Long
    Dim NewFlag As Boolean
    ' The Bitmap and Graphics objects we will draw on.
    Private m_Bitmap As Bitmap
    Private m_Graphics As Graphics
    ' Make the initial blank image.
    Private Sub Form1_Load() Handles MyBase.Load
        NewFlag = True
        Call MakeNewBitmap()
    End Sub

    ' Make a new blank image.
    Private Sub mnuFileClear_Click() Handles mnuFileClear.Click
        Call MakeNewBitmap()
        Panel1.Visible = True
        'There are actually 20 Panels but I removed the code to make it more compact for the question
      
    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

    Private Sub MakeGraphics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MakeGraphics.Click
        NewFlag = True
         Make some random elipses on screen when button is clicked
        m_Graphics = Graphics.FromImage(m_Bitmap)


        For Temp = 1 To 25
            Randomize()
            X = 5 + (Rnd() * (m_Bitmap.Width - 30))
            Y = 5 + (Rnd() * (m_Bitmap.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 picCanvas_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picCanvas.Paint
        m_Graphics = Graphics.FromImage(m_Bitmap)


        
    End Sub
    Private Sub HidePanel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HidePanel.Click
        Dim R As Integer
        Randomize()
        R = 1 + Rnd() * 20
        Label1.Text = R

        If R = 1 Then Panel1.Visible = False
       'There are actually 20 Panels but I removed the code to make it more compact for the question
      
    End Sub

    
End Class
Posted
Updated 23-Jan-11 8:11am
v2
Comments
Manfred Rudolf Bihy 23-Jan-11 14:11pm    
Added code tags! :(

1 solution

You've not done what was advised. Don't keep a graphics object in memory. If you draw something within these panels, create a derived class that does your drawing in it's paint event. As for your main question, you're drawing within the panel, so I don't see how it could appear anywhere else, or did I miss something ?
 
Share this answer
 
Comments
GaryV100 23-Jan-11 16:34pm    
Bare with me...I am a beginner in VB10 trying to learn....
Not sure about the derived class but i will research that...
You think I am drawing in the panel??? I thought the MakeNewBitMap was creating the bmp using the picCanvas (PictureBox). Then when I create the random graphics in MakeGraphics_Click with a button click I am making them on the
m_Graphics = Graphics.FromImage(m_Bitmap). Would this not be putting the graphics on the picCanvas??? The graphics appear tobe on the picCanvas but only if the panel is there first.

I know this is pretty straight forward for many users but I am still trying to make sense of this. I have several books that I have researched but they have not cleared this for me...

Thanks again
Gary V

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