Private Declare Function PrintWindow Lib "user32.dll" (ByVal _
hwnd As IntPtr, ByVal hdcBlt As IntPtr, ByVal nFlags As _
UInt32) As Boolean
Dim WithEvents t As New Timer
Dim screenCapture As Bitmap
Dim otherForm As New Form
Private Sub CaptureScreen()
Me.Text = "capturing, " & Me.Location.X & "," & Me.Location.Y
screenCapture = New Bitmap(Me.Width, Me.Height)
Dim g As Graphics = Graphics.FromImage(screenCapture)
Dim hdc As IntPtr = g.GetHdc
Form1.PrintWindow(Me.Handle, hdc, Nothing)
g.ReleaseHdc(hdc)
g.Flush()
g.Dispose()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
uncomment to test capturing various hidden forms:
1- hidden off screen -> works!
Me.Location = New Point(-500, -500)
2- Minimizes - works but just takes a capture of the small minimized form...
Me.WindowState = FormWindowState.Minimized
3- big form laid over the top
otherForm.Size = New Size(4000, 4000)
otherForm.Show()
t.Interval = 1000
t.Start()
End Sub
Private Sub t_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles t.Tick
CaptureScreen()
If IO.File.Exists("C:\\ScreenCapBlah.bmp") Then
IO.File.Delete("C:\\ScreenCapBlah.bmp")
End If
screenCapture.Save("c:\\ScreenCapBlah.bmp")
Me.Text = ""
1 - move the form back
Me.Location = New Point(0, 0)
2- maximize again
Me.WindowState = FormWindowState.Maximized
3 - hide otherForm
otherForm.Hide()
t.Stop()
End Sub