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:

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 Unhnd_Exception

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.