Any Ideas on how to get a picture of just a certain program widow and not the entire Desktop?

This is the Code I used to get a pic of the Desktop

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim bounds As Rectangle
        Dim screenshot As System.Drawing.Bitmap
        Dim graph As Graphics
        bounds = Screen.PrimaryScreen.Bounds
        screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        graph = Graphics.FromImage(screenshot)
        graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
        Form2.PictureBox1.Image = screenshot
        Timer1.Enabled = False
        Form2.Show()
        Me.Opacity = 100


    End Sub

Any help would be awesome. :twisted:

Dani AI

Generated

Two practical options depending on what you need:

  • Fast and simple: grab the window’s on‑screen pixels (get its screen rectangle, then copy that rectangle). This is quick but only captures what’s actually visible on the desktop right now — it won’t grab covered or minimized windows. (learn.microsoft.com)

  • Robust (but more complex): ask the target window to render itself into a device context (PrintWindow / WM_PRINT). That lets many apps render even when partially occluded, but behavior varies by application (some layered or GPU‑accelerated windows won’t render) and the API is synchronous so it can block if the target is unresponsive. (learn.microsoft.com)

Implementation notes and a quick pattern to try (ties to prior replies)

  • For a visible window, obtain its bounds (the Win32 GetWindowRect / DWM extended frame bounds are the usual choices) and use a single Graphics.CopyFromScreen call to copy that rectangle. ’s suggestion to locate the foreground window is the right idea when you want the active window. (learn.microsoft.com)
  • If you need a capture that works when the window is occluded or you want the non-client area, try PrintWindow first, then fall back to asking the window to paint via WM_PRINT or simply bring it to front and use CopyFromScreen. (learn.microsoft.com)

Example: call PrintWindow from VB.NET (provide hwnd and the width/height you measured with GetWindowRect)

<DllImport("user32.dll", SetLastError := True)>
Private Shared Function PrintWindow(hWnd As IntPtr, hdcBlt As IntPtr, nFlags As Integer) As Boolean
End Function

Public Function CaptureWindow(hwnd As IntPtr, width As Integer, height As Integer) As Bitmap
    Dim bmp As New Bitmap(width, height, Imaging.PixelFormat.Format32bppArgb)
    Using g As Graphics = Graphics.FromImage(bmp)
        Dim hdc = g.GetHdc()
        PrintWindow(hwnd, hdc, 0)
        g.ReleaseHdc(hdc)
    End Using
    Return bmp
End Function

DPI and troubleshooting: account for per‑monitor DPI (process DPI awareness or GetDpiForWindow) when converting bounds and pixel sizes; test on the target app because some apps ignore WM_PRINT or use accelerated rendering and will not produce useful output. (learn.microsoft.com)

Summary: start with CopyFromScreen for visible windows (fast), move to PrintWindow/WM_PRINT for occluded windows, and use the SendKeys/Alt+PrintScreen trick only as a last manual fallback as suggested.

Recommended Answers

All 4 Replies

Do some research (if you're not familiar with it already) into the SendKeys function. We've had mixed results with it but when it works it's an absolute gem.

You could use it do send an 'Alt'+'PrintScrn' command to the system which would only capture the active window and none of the surrounding area.

I've never tried using Sendkeys to do a screengrab but I don't see why it wouldn't be able to do it. There may be another way, but you'd have to wait for someone else to step in and speak up to find out how.

Let us know how you get on,

Rob.

Member Avatar for Member #857553

You can use this class to get the rectangle of the foreground window.

Imports System.Runtime.InteropServices

Public Class ForeGroundWindow

    <StructLayout(LayoutKind.Sequential)> _
   Private Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure

    Public Shared Function GetRect() As Rectangle
        Dim r As New RECT
        GetWindowRect(GetForegroundWindow(), r)

        Return Rectangle.FromLTRB(r.Left, r.Top, r.Right, r.Bottom)

    End Function

    <System.Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function GetForegroundWindow() As Long
    End Function

    <DllImport("User32")> _
    Private Shared Function GetWindowRect(ByVal hwnd As Long, ByRef lpRect As RECT) As Boolean
    End Function

End Class

change your bounds variable to

bounds = ForeGroundWindow.GetRect()
if bounds = rectangle.empty then exit sub

See if this helps.
2 Forms: Form1(1 Button, 1 PictureBox)

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not Form2.Visible Then : Form2.Show() : Exit Sub : End If
        PictureBox1.Image = getSnapshot(Form2)
    End Sub

    Private Function getSnapshot(ByVal snapshotForm As Form) As Bitmap
        With snapshotForm
            .FormBorderStyle = Windows.Forms.FormBorderStyle.None
            Dim bmp As New Bitmap(.Width + 15, .Height + 25, Imaging.PixelFormat.Format32bppArgb)
            Dim g As Graphics = Graphics.FromImage(bmp)
            g.CopyFromScreen(New Point(.Location.X, .Location.Y), New Point(0, 0), bmp.Size)
            .FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
            Return bmp
        End With
    End Function
End Class
Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TransparencyKey = Me.BackColor : Me.TopMost = True : Me.MinimizeBox = False : Me.MaximizeBox = False
        Me.Text = "Snapshot View" : Me.Icon = SystemIcons.Information : Me.ShowInTaskbar = False
    End Sub
End Class

This will take a snapshot of Form2's Location and Size and will use Form2 as the snapshot window for your Screen Capture.

Thanks guys, I Will try those out and see how it gets along. :)

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.