Originally posted by gilly Yes, i have attached a program which does just that.
Thanks Gilly,
But this program is not running. It is asking me for a dll file. Besides there is no source code, its an exe file.
Can anyone have an idea about capturing the desktop/screen with API? And can anyone give me the source code for it?
Help,
Anita
Can't imagine life without VB
(Various Boyfriends)
The following code uses a standard VB form
(FDesktop) containing a PictureBox (Picture1) and a module
(MDesktop). Create a new form, name it FDesktop and add a
PictureBox control to the form. (Please be aware that some
line wrapping is bound to occur)
Code:
'-----------------------------------------------------------
------------
' This sample project shows the use of a few GDI functions
to capture an
' image of the desktop and any currently open windows.
'-----------------------------------------------------------
------------
Option Explicit
' the sleep API is used to give the screen time to refresh
before capture
Private Declare Sub Sleep Lib "kernel32" _
( _
ByVal dwMilliseconds As Long _
)
Private Sub Form_Load()
' printer mode is changed to landscape to allow printing
' of the form
Printer.Orientation = vbPRORLandscape
' set various properties of the picture box control
With Picture1
.AutoRedraw = True
.BorderStyle = 0 'none
.ScaleMode = vbPixels
End With
End Sub
Private Sub Form_Resize()
' when the form is resized the picture box must also be
resized to
' fill the form
Picture1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
End Sub
Private Sub Picture1_Click()
' this hides the form so the desktop can be captured -
the DoEvents
' and Sleep API allow the form to be hidden and any
windows which were
' hidden by the form to be refreshed before the desktop
is captured
Me.Visible = False
DoEvents
Sleep (1)
CopyDesktop Picture1
Me.Visible = True
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As
Integer, X As Single, Y As Single)
' a right click will cause the form (picture of desktop)
to be printed
If Button And 2 Then Me.PrintForm
End Sub
[code location="MDesktop"]
Option Explicit
' the declarations...
Private Declare Function GetDC Lib "user32" _
( _
ByVal hWnd As Long _
) _
As Long
Private Declare Function GetDesktopWindow Lib "user32" ()
As Long
Private Declare Function ReleaseDC Lib "user32" _
( _
ByVal hWnd As Long, _
ByVal hDC As Long _
) _
As Long
Private Declare Function StretchBlt Lib "gdi32" _
( _
ByVal hDC 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 nSrcWidth As Long, _
ByVal nSrcHeight As Long, _
ByVal dwRop As Long _
) _
As Long
' constant for use with the StrechBlt API
Private Const SRCCOPY = &HCC0020
Public Sub CopyDesktop(pb As PictureBox)
Dim hWndDesktop As Long
Dim hDCDesktop As Long
Dim lRet As Long
Dim rWidth As Single, rHeight As Single
' get the screen dimensions
rWidth = pb.ScaleX(Screen.Width, vbTwips, vbPixels)
rHeight = pb.ScaleY(Screen.Height, vbTwips, vbPixels)
' get the desktop's handle
hWndDesktop = GetDesktopWindow
' get the desktop's DC
hDCDesktop = GetDC(hWndDesktop)
' blit the desktop image to the picture box - StrechBlt
is used to
' dynamically fit the desktop image to the picture box.
lRet = StretchBlt(pb.hDC, 0, 0, pb.ScaleWidth,
pb.ScaleHeight, _
hDCDesktop, 0, 0, rWidth, rHeight,
SRCCOPY)
' release the desktop's DC
Call ReleaseDC(hWndDesktop, hDCDesktop)
' refresh the picture box to cause it to redraw
pb.Refresh
' save the picture to a bmp file (change to a file of
' your choice)
SavePicture pb.Image, "c:\dt.bmp"
Thanks Mendhak,
U'r code is working fine. But just the problem is it is saving the image of desktop in .BMP format. And its taking more than 1 MB space for a single image.
So is there any way to reduce the size of image?
Anyone please help,
Anita
Can't imagine life without VB
(Various Boyfriends)