0

I'm working on a program which will allow users to take screenshots of full screen games, such as Call of Duty, Battlefield 3, etc.

However, I'm having two issues with my code.

First off, I'm having to press the shortcut twice, instead of once like my other shortcuts. This only occurs when the code below is actually in the handling sub. If I just have a messagebox show when the shortcut is pressed, it works every time.

Second, whenever I take a screenshot, it either comes out completely black, or takes a shot of my desktop, even though there is a game running in the foreground.

I have looked around, and it seems that printscreen is the only way to get a screenshot of a game through vb.net.

My code is below. Any help is appreciated.

    Private Sub game_Press(ByVal s As Object, ByVal e As Shortcut.HotKeyEventArgs) Handles gamewindow.press
    Dim gamewin As RECT
    If GetWindowRect(GetForegroundWindow, gamewin) Then
        Dim bmp As Bitmap
        Dim gfx As Graphics
        SendKeys.Send("%{PRTSC}")
        Dim getscrn As IDataObject = Clipboard.GetDataObject()
        Dim bmpsize As New Size(gamewin._Right - gamewin._Left, gamewin._Bottom - gamewin._Top)
        bmp = New Bitmap(CType(getscrn.GetData(GetType(System.Drawing.Bitmap)), Bitmap))

        gfx = Graphics.FromImage(bmp)
        gfx.CopyFromScreen(gamewin._Left, gamewin._Top, 0, 0, New Size(bmpsize.Width, bmpsize.Height), CopyPixelOperation.SourceCopy)
        Dim sr As New System.Threading.Thread(AddressOf uploadimage)
        sr.IsBackground = True
        sr.SetApartmentState(Threading.ApartmentState.STA)
        sr.Start(bmp)
        gfx.Dispose()
    End If
End Sub
Justin
  • 93
  • 1
  • 11

1 Answers1

1

PrintScreen is handled by the Win32 environment, which handles the normal drawing of windows. So if the PrintScreen key makes its way to Win32 (isn't gobbled up by the game), it is probably going to take a screen capture of what it is currently drawing which, like you said, is either your desktop, or nothing (black).

You could try using GDI to BitBlt from the Screen's DC to a memory DC (as most screen capture programs do) but that's not guaranteed to work. Here's the big question. Does actually pressing Print Screen while you're in the game, and then going and pasting into MS paint actually work? If no, then you're probably out of luck, without digging into the directX side of things.

Paul-Jan pointed out this closely-related SO question: Take screenshot of DirectX full-screen application which recommends the use of Microsoft's Detours to hook/instrument the DirectX calls required to do a frame capture.

Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • What would you recommend then? Everything I have seen has said to use printscreen to handle the screenshot. – Justin May 01 '12 at 22:09
  • 1
    You obviously haven't seen it all then - http://stackoverflow.com/questions/1962142/take-screenshot-of-directx-full-screen-application – Paul-Jan May 02 '12 at 19:20