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

Dani AI

Generated

asked how to capture a bitmap of a panel that is hidden behind another panel. was correct to warn that a control that has never been rendered on-screen may not contain drawable pixels, and ’s DrawToBitmap-based idea is on the right track (note: in ’s example the Bitmap constructor uses Height then Width — that should be Width then Height).

A pragmatic, generally reliable approach for most WinForms controls is to ensure the control has an active handle/layout and then call DrawToBitmap into a correctly sized Bitmap. The following helper is a compact, safe pattern (dispose the returned Bitmap when finished):

Imports System.Drawing
Imports System.Drawing.Imaging

Public Function CaptureControlBitmap(ctrl As Control) As Bitmap
    If ctrl Is Nothing Then Throw New ArgumentNullException("ctrl")
    Dim w = Math.Max(1, ctrl.Width)
    Dim h = Math.Max(1, ctrl.Height)

    If Not ctrl.Created Then
        ctrl.CreateControl()
        ctrl.PerformLayout()
        ctrl.Refresh()
        Application.DoEvents()
    End If

    Dim bmp As New Bitmap(w, h, PixelFormat.Format32bppArgb)
    Try
        ctrl.DrawToBitmap(bmp, New Rectangle(0, 0, w, h))
        Return bmp
    Catch
        bmp.Dispose()
        Throw
    End Try
End Function

Troubleshooting and important caveats:

  • Some controls (ActiveX, WebBrowser, certain third-party windowed controls) do not render via DrawToBitmap. For those, PrintWindow (P/Invoke) or an off-screen host may be required.
  • To capture the appearance of a covered panel as if frontmost, capture the parent or the form (then crop) rather than the occluded panel alone. DrawToBitmap of a panel captures the panel’s own composition, not what’s drawn over it.
  • Temporarily moving a live control to an off-screen Form, or creating a runtime clone of the control tree on an off-screen Form, forces a full paint without disturbing the visible UI—but must be done on the UI thread and cleaned up afterward.
  • Dispose Graphics/Bitmap objects (Using blocks) to avoid GDI leaks.

This provides a practical starting point while keeping in mind the limits noted by and the z-order/size detail from .

Recommended Answers

All 2 Replies

Why this may be a fools errand is because the panel may not have content as VB and many other apps don't render the view if it's not on screen.

So do try the method noted at but given it's not onscreen it may not have content.

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.