A Panel will produce flickering problems in WindowsForms, I suggest you to place a PictureBox inside the Panel with the PictureBox.Dock = Fill
property set (or just use only the PictureBox instead of a Panel) then you could use for example my TakeScreenshotFromForm() function from my ElektroKit Framework.
A full working example:
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Public Class Form1 : Inherits Form
Friend WithEvents ScreenshotTimer As New System.Windows.Forms.Timer
Private Sub Test() Handles MyBase.Shown
Form2.Show()
With Me.ScreenshotTimer
.Interval = 100
.Enabled = True
.Start()
End With
Me.PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
End Sub
Private Sub ScreenshotTimer_Tick(sender As Object, e As EventArgs) Handles ScreenshotTimer.Tick
If Me.PictureBox1.BackgroundImage IsNot Nothing Then
Me.PictureBox1.BackgroundImage.Dispose()
End If
Me.PictureBox1.BackgroundImage = TakeScreenshotFromForm(Form2, includeMouse:=True)
End Sub
Public Shared Function TakeScreenshotFromForm(ByVal f As Form,
Optional ByVal includeMouse As Boolean = False,
Optional ByVal pixelFormat As PixelFormat = PixelFormat.Format24bppRgb) As Image
If Not f.Visible Then
Return Nothing
End If
Dim bmp As New Bitmap(f.Size.Width, f.Size.Height, pixelFormat)
Using g As Graphics = Graphics.FromImage(bmp)
g.InterpolationMode = InterpolationMode.Default
g.PixelOffsetMode = PixelOffsetMode.Default
g.CopyFromScreen(f.Location, New Drawing.Point(0, 0), f.Size)
' Draw the cursor in the image.
If includeMouse Then
Dim cursorSize As System.Drawing.Size = CType(f.Cursor.HotSpot, System.Drawing.Size)
cursorSize.Width -= ((f.Size.Width - f.ClientSize.Width) \ 2)
cursorSize.Height -= ((f.Size.Height - f.ClientSize.Height) - ((f.Size.Width - f.ClientSize.Width) \ 2))
Dim formPoint As Drawing.Point = f.PointToClient(Drawing.Point.Subtract(Control.MousePosition, cursorSize))
Cursors.Arrow.Draw(g, New Rectangle(formPoint.X, formPoint.Y, cursorSize.Width, cursorSize.Height))
End If
End Using
Return bmp
End Function
End Class
Instead of the methodology that I use, you can also use the Control.DrawToBitmap() method to properlly capture the Form when is not visible (totally invisible on the screen, or covered by other windows), but the resulting image will not contain some "information", such as a TextBox caret for example.
Dim bmp As New Bitmap(f.Bounds.Size.Width, f.Bounds.Size.Height, pixelFormat)
f.DrawToBitmap(bmp, New Rectangle(0, 0, f.Bounds.Size.Width, f.Bounds.Size.Height))
' ...