Capturing a form to memory

Hi guys,
I am looking for a function that can capture a form even though it is hided. I have function right now which can capture the form. The thing is that it needs to be visisble before I can capture it. Please help me ASAP. Can it be done without
being visible?? The code is as follows :

Public Function CaptureWindow(ByVal hWndSrc As Long, _
 ByVal Client As Boolean, ByVal LeftSrc As Long, _
 ByVal TopSrc As Long, ByVal WidthSrc As Long, _
 ByVal HeightSrc As Long) As Picture
Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim r As Long
Dim hDCSrc As Long
Dim hPal As Long
Dim hPalPrev As Long
Dim RasterCapsScrn As Long
Dim HasPaletteScrn As Long
Dim PaletteSizeScrn As Long

' Depending on the value of Client get the proper device context
If Client Then
    hDCSrc = GetDC(hWndSrc)
    hDCSrc = GetWindowDC(hWndSrc)
End If
' Create a memory device context for the copy process
hDCMemory = CreateCompatibleDC(hDCSrc)
' Create a bitmap and place it in the memory DC
hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
hBmpPrev = SelectObject(hDCMemory, hBmp)
' Get screen properties
RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
HasPaletteScrn = RasterCapsScrn And RC_PALETTE       ' Palette
PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of
                                        ' palette
' If the screen has a palette make a copy and realize it
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    ' Create a copy of the system palette
    LogPal.palVersion = &H300
    LogPal.palNumEntries = 256
    r = GetSystemPaletteEntries(hDCSrc, 0, 256, _
    hPal = CreatePalette(LogPal)
    ' Select the new palette into the memory DC and realize it
    hPalPrev = SelectPalette(hDCMemory, hPal, 0)
    r = RealizePalette(hDCMemory)
End If
' Copy the on-screen image into the memory DC
r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, _
            LeftSrc, TopSrc, vbSrcCopy)
' Remove the new copy of the  on-screen image
hBmp = SelectObject(hDCMemory, hBmpPrev)
' If the screen has a palette get back the palette that was
' selected in previously
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    hPal = SelectPalette(hDCMemory, hPalPrev, 0)
End If
' Release the device context resources back to the system
r = DeleteDC(hDCMemory)
r = ReleaseDC(hWndSrc, hDCSrc)
' Call CreateBitmapPicture to create a picture object from the
' bitmap and palette handles.  Then return the resulting picture
' object.
Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
End Function
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The ANSWER is NO. It simply cannot be done. In order to capture something you must be able to access a device context. For VB forms the only way you can access a device context is to make them visible.

The same thing happens when your form is partially obscured. You'll end up capturing the obscuring window rather than the form. Nothing you can do about it!

If your form contained something like a picture box (Anything with a .picture (type OLEPicture) property) then you could extract the picture on that control via its bitmap handle, but that is a different story and it won't help you the slightest bit in this case.

Sad but true.
ismohamedAuthor Commented:
Hi Caraf,
How can I add the form into the picture box? Do you have a function for that?
Here's an old "neat" trick to do what you want...

First Hide the form. If it's already hidden, that's ok.  Then set the top of the form like this:

   Form1.Top = Screen.Height * 2

This will put the form completly off the screen.  Then show it... To the user, it's still "hidden", but it's visible to capture routines...

After you've captured the screen, hide the form and put the top of the form back where it was...

Hope this helps!


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!


Nice one. Does that really work?


You don't happen to know how to get a screen shot of the user's desktop from a Windows NT screen saver? See (It's a PAQ because I thought the answer given would solve the problem, but alas...)
ismohamedAuthor Commented:
Thanks, it did help!
Thanks for the points! Glad I could help!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.