• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

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
  • 2
  • 2
  • 2
1 Solution
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!

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


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 http://oldlook.experts-exchange.com/bin/Q.10257047 (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!


Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now