Screen Image Capture

Description: Returns an Image that is the full virtual screen screenshot (for multi-monitor setups). Requires the System.Drawing and System.Windows.Forms namespaces.
Tested Platform: .NET 4.8, Visual Studio 2022, Windows 10
Language: VB.NET
' Returns an image object that is a screenshot of the current virtual screen.

Public Function CaptureScreenImage() As Image
    ' Get virtual screen dimensions (for multiple monitors)
    Dim screenWidth As Integer = SystemInformation.VirtualScreen.Width
    Dim screenHeight As Integer = SystemInformation.VirtualScreen.Height
    Dim screenTop As Integer = SystemInformation.VirtualScreen.Top
    Dim screenLeft As Integer = SystemInformation.VirtualScreen.Left

    ' Setup a bitmap according to the virtual screen dimensions
    Dim Bitmap As Bitmap = New Bitmap(screenWidth, screenHeight)

    Dim g As Graphics = Graphics.FromImage(Bitmap)
    g.CopyFromScreen(screenLeft, screenTop, 0, 0, New Size(screenWidth, screenHeight))

    Return CType(Bitmap, Image)
End Function

Posted: March 20, 2023

Return to the snippets listing