Place a small picture box on your form and set its properties as under.[ol][li]AutoRedraw = True
[li]BorderStyle = 0 (None)
[li]Visible = False[/ol]Now place a command button (Command1) on your form and insert the following code in it.
___
Option Explicit
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) 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
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Command1_Click()
SavePicture CaptureImage(hWnd), "C:\image.bmp"
End Sub
Function CaptureImage(hWnd As Long) As Picture
Dim hDC As Long, R As RECT
GetWindowRect hWnd, R
hDC = GetWindowDC(hWnd)
Picture1.Cls
Picture1.Width = (R.Right - R.Left) * Screen.TwipsPerPixelX
Picture1.Height = (R.Bottom - R.Top) * Screen.TwipsPerPixelY
BitBlt Picture1.hDC, 0, 0, R.Right - R.Left, R.Bottom - R.Top, hDC, 0, 0, vbSrcCopy
ReleaseDC hWnd, hDC
Set CaptureImage = Picture1.Image
End Function
___
Run the program and click the button, it will save the image of the form to file C:\Image.bmp.
Try this code. Rest of the things remain intact.
___
Option Explicit
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As Any) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC 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
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Command1_Click()
SavePicture CaptureImage(hwnd), "C:\image.bmp"
End Sub
Function CaptureImage(hwnd As Long) As Picture
Dim hDC As Long, R1 As RECT, R2 As RECT
GetWindowRect hwnd, R1
R2 = R1
hDC = GetWindowDC(hwnd)
Picture1.Cls
Picture1.Move 0, 0, ScaleWidth, ScaleHeight
ScreenToClient hwnd, R2
BitBlt Picture1.hDC, 0, 0, R1.Right - R1.Left, R1.Bottom - R1.Top, hDC, -R2.Left, -R2.Top, vbSrcCopy
ReleaseDC hwnd, hDC
Set CaptureImage = Picture1.Image
End Function
that works but it doesn't capture the controls.
Ive found an .ocx that works. Thanks for the help, if you do know how to make it so i can see the controls, i would be intrested in knowing. thx
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.