I have tried in many places but all have gone in vain. I am hopeful of getting a solution in this forum.

I have a vb.net app in which there are many panels. I want to take a bitmap of a panel which is hidden behind the panel which is showing on the screen. Kindly let me know how to do this.

Regards

Renga

Recommended Answers

All 2 Replies

See if this works. Panel1 that has two controls: a label and a picturebox with an image; and Panel2 is on top of Panel1 and hidding it. Besides, form "HiddenPanel" has 2 buttons to show hidden panel1 contents in panel2, and to clear panel2.

Imports System.Drawing.Imaging

Public Class HiddenPanel
    Function getPanelsBitmap(pnl As Panel) As Bitmap
        Dim bmp As New Bitmap(pnl.Height, pnl.Width)
        Try
            Dim vCtrl(-1) As Control, iv As Int32
            Dim vZ(-1) As Int32
            For Each ctr As Control In pnl.Controls
                ReDim Preserve vCtrl(iv), vZ(iv)
                vCtrl(iv) = ctr
                ' save the control's z-index:
                vZ(iv) = ctr.Parent.Controls.GetChildIndex(ctr)
                iv += 1
            Next
            ' sort controls by it's z-index:

            Array.Sort(vZ, vCtrl)
            Array.Reverse(vCtrl)

            Dim gr As Graphics = Graphics.FromImage(bmp)
            For Each ctr In vCtrl
                Dim img As New Bitmap(ctr.Width, ctr.Height)
                ctr.DrawToBitmap(img, New Rectangle( _
                    0, 0, ctr.Width, ctr.Height))
                gr.DrawImage(img, ctr.Location)
            Next
            gr.Dispose()
        Catch ex As Exception
            Throw ex
        End Try
        Return bmp
    End Function

    Private Sub btnShow_Click(sender As System.Object, e As System.EventArgs) Handles btnShow.Click
        Try
            ' picturebox2 is contained in panel2 on top and hidding panel1:
            '
            PictureBox2.Image = getPanelsBitmap(Panel1)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
        PictureBox2.Image = Nothing
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.