Taking a screenshot of an invisible form

I've been reading up on the conventional ways to take screenshots of normal forms, using the keybd_event api call.

What I would like to do is take a screenshot of an invisible form. This screenshot will be available on the company intranet through a web page. Does anyone have any ideas? or is this impossible..

Many thanks, and regards
[361 byte] By [HairyMonkeyMan] at [2007-11-18 19:03:31]
# 1 Re: Taking a screenshot of an invisible form
Originally posted by HairyMonkeyMan
What I would like to do is take a screenshot of an invisible form.

?? :confused: What would you expect to see ?!!! An invisible screenshot..?!!

Why is it invisible?
Dmorley at 2007-11-9 23:12:57 >
# 2 Re: Taking a screenshot of an invisible form
its invisible because it would be running in the background on my machine, and I dont want to have to look at it! It is like a sort of switchboard form that shows which computers are active in the office. That way, anyone can look at the intranet site to see who is online. I guess it is not entirely essential for the form to be invisible, I could just have it positioned off the screen... But I'm also interested to find out if this is possible.
HairyMonkeyMan at 2007-11-9 23:13:58 >
# 3 Re: Taking a screenshot of an invisible form
If it's a project that you control then make it visible while you take a screenshot, then make it invisible again for when you compile & run the application...

Don't think you can capture invisible objects...
Dmorley at 2007-11-9 23:15:02 >
# 4 Re: Taking a screenshot of an invisible form
No worries, thanks man!
HairyMonkeyMan at 2007-11-9 23:16:03 >
# 5 Re: Taking a screenshot of an invisible form
Just thought I'd give you some extra help on this one:

If you want to keep it hidden all the time, you can use BitBlt to capture the form:

Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

BitBlt Picture1.hdc, 0, 0, 1024, 768, Form1.hDC, 0, 0, vbSrcCopy

Then use the picturebox to save the image to file.

Also, a lot of people ask how to do a printscreen without the buggy method of key emulation. Here's my code:

Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

BitBlt Picture1.hdc, 0, 0, 1024, 768, GetDC(GetDesktopWindow()), 0, 0, vbSrcCopy

Also, in both cases, make sure the picturebox's AutoRedraw is set to false, I don't know why, but it seems to stop the code from doing its job.

Hope it's useful!
Burningmace at 2007-11-9 23:17:08 >
# 6 Re: Taking a screenshot of an invisible form
Seems to me, that if you can give the info as text, you would be better off just writing it to a file. Then you'd save all the bandwidth of an image.

Of course, if you don't have the source code, then you'd have to capture the image. In addition to what Burningmace posted, I'd suggest using GetWindowRect, and use the coordinates for BitBlt. That way, you would only be capturing the image of the window you want, instead of the entire screen. Also, don't forget to use ReleaseDC afterward, otherwise you'll have a memory leak.

It may be helpful to use SetWindowPos to bring up the window, and you could use the hWnd of the window to get a DC for it instead of the entire screen. Then you could use GetClientRect instead, which will help cut down the size of the image, since it leaves out the size of the titlebar. There are threads on how to save an image to jpg as well, which will greatly reduce bandwidth requirements.

In any case, you may find it necessary to use UpdateWindow to make it show up before using BitBlt. There may also be the need for DoEvents in there too.
WizBang at 2007-11-9 23:18:08 >