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