1

i have a problem here. I have a form1 which i use to get settings en so on. But there is also a panel that i want to use to see whats inside form2.

Form2 = a fullscreen form without formborderstyles

This form2 shows information on a big screen and i need to see in my form1 (in this panel) the entire content of form2 like a screencapture thing.

Can someone help?

1 Answer 1

1

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.

enter image description here

A full working example:

enter image description here

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))
' ...
6
  • isn't this going to overload my memory after a while? and is this the same as a normal screenshot or is it something else?? @elektroStudios Commented Mar 22, 2016 at 12:54
  • @Thomas Dutoit No, the disposable objects are disposed everytime, however maybe you could note how the process is reserving more memory space after each call because the way that .Net manages the memory (anyways you can force a garbage collection at any time to try see a more proper memory consumption value, since is not very recommended to force it). What you mean for a "normal screenshot"?, the method that I shown takes an image (screenshot) of the specified Form, isn't that what you are asking for?, I hope this help!. Commented Mar 22, 2016 at 13:04
  • The alternative method explained at the bottom of my answer maybe is more a "raw image" than a screenshot because I'm not copying directlly from the screen, as I said using that alternative you will loose image information such as a focus rectangle or a caret, but on the other side, it is helpful to capture a Form when is not visible on the screen. Commented Mar 22, 2016 at 13:10
  • what i mean with normal screenshot, does your system create a screenshot like you press the screenshot button or is this an different thing? Commented Mar 22, 2016 at 13:16
  • The example that I exposed does a screenshot every time that the timer ticks to show what is happening in "real time" on Form2, but of course you can just do the screenshot once!. This is the important thing: Me.PictureBox1.BackgroundImage = TakeScreenshotFromForm(Form2, includeMouse:=False) Commented Mar 22, 2016 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.