Form Capture in VB.Net (ALT+PrintScr)

I am currently creating an application that takes a screenshot of a running process. So I have a running process called "notepad.exe" for example, and I would like the application to only take what is in that form, so nothing else around it.

Basically what I am after is doing the ALT+PrintScreen command, but though code rather then faking that the button has been pressed. (As I have seen on other forums and tutorials). What has been taken will then need to be placed within PictureBox1.

Thanks for your help,
Who is Participating?
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can use Native VB.Net code to do the screen capture part with Graphics.CopyFromScreen():

...BUT since you're dealing with an EXTERNAL application, you'll have to resort to APIs to first determine the location/size of the external window:
Public Class Form1
    Private Structure RECT
        Dim Left As Integer
        Dim Top As Integer
        Dim Right As Integer
        Dim Bottom As Integer
    End Structure
    Private Declare Function GetWindowRect Lib "user32" (ByVal handle As IntPtr, ByRef lpRect As RECT) As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim AppNameWithoutPathTheExePart As String = "NotePad"
        Dim ps() As Process = Process.GetProcessesByName(AppNameWithoutPathTheExePart)
        If ps.Length > 0 Then
            Dim p As Process = ps(0)
            Dim pRect As RECT
            GetWindowRect(p.MainWindowHandle, pRect)
            Dim width As Integer = pRect.Right - pRect.Left + 1
            Dim height As Integer = pRect.Bottom - pRect.Top + 1
            Dim bmp As New Bitmap(width, height)
            Dim G As Graphics = Graphics.FromImage(bmp)
            G.CopyFromScreen(New Point(pRect.Left, pRect.Top), New Point(0, 0), New Size(width, height))
            PictureBox1.Image = bmp
        End If
    End Sub
End Class

Open in new window

David L. HansenProgrammer AnalystCommented:
DarkSharkyAuthor Commented:
I dont really want to use an external API which is not part of the Visual Studio 2008. Or general

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

David L. HansenProgrammer AnalystCommented:
DarkSharkyAuthor Commented:
Thanks That works :D,  however one small problem.

If a form is "always on top" , and is covering the area where notepad is being used, then it will take a print screen of both the form (notepad) and the form ontop, therefore it isnt a "true" Alt+Printscreen, which does not do this.

Mike TomlinsonMiddle School Assistant TeacherCommented:
That's correct...but you stated:

    "...but though code rather then faking that the button has been pressed."

If you want TRUE Alt-PrintScreen functionality then press that button...  ;)
(this of course requires that your external application be given focus first)

There is another approach that involves sending the external window a WM_PRINT message via the SendMessage() API but it is poorly documented, doesn't work with all windows, and is messy to implement.

Simply put, you can't have it both ways.  =\
DarkSharkyAuthor Commented:
Food for thought....

When you talk about SendMessage() API and that some windows it wont work with, do you know what type or reasons why it will not work.


Mike TomlinsonMiddle School Assistant TeacherCommented:
I don't know the exact reason...other than Microsoft never "solidified" it as a good practice to respond to that message in the desired manner.  Therefore it is not commonly implemented by many people...and it apps don't do it by default.
DarkSharkyAuthor Commented:
In the end I went for the solution from Idle_Mind. Thanks all . :)
DarkSharkyAuthor Commented:
Some extra comments would be nice to other users, but I understood it fine :). Wasn't quite what i wanted, but it works :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.