Hi all,
Can anyone knows the API whith which we can capture the screen/desktop and store it as image file?
Help me,
Anita
Printable View
Hi all,
Can anyone knows the API whith which we can capture the screen/desktop and store it as image file?
Help me,
Anita
Yes, i have attached a program which does just that.
Thanks Gilly,Quote:
Originally posted by gilly
Yes, i have attached a program which does just that.
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
From vb-faq
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 location="MDesktop"]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
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"
End Sub
[/code]
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
Convert it to a JPG? Using the Windows API, because XP can do so.
Ahem... may I refer you to your first post?Quote:
Originally posted by anita2002
Thanks Mendhak,
U'r code is working fine. But just the problem is it is saving the image of desktop in .BMP format.
:rolleyes:Quote:
...can capture the screen/desktop and store it as image file?
Anyways, I suppose a JPG would be smaller, so try ths:
VB Code:
Private Sub mnuconvertBMPtoJPG_Click() Dim tmpimage As imgdes ' Image descriptors Dim tmp2image As imgdes Dim rcode As Long Dim quality As Long Dim vbitcount As Long Dim bdat As BITMAPINFOHEADER ' Reserve space for BMP struct Dim bmp_fname As String Dim jpg_fname As String bmp_fname = "test.bmp" jpg_fname = "test.jpg" quality = 75 ' Get info on the file we're to load rcode = bmpinfo(bmp_fname, bdat) If (rcode <> NO_ERROR) Then MsgBox "Cannot find file", 0, "Error encountered!" Exit Sub End If vbitcount = bdat.biBitCount If (vbitcount >= 16) Then ' 16-, 24-, or 32-bit image is loaded into 24-bit buffer vbitcount = 24 End If ' Allocate space for an image rcode = allocimage(tmpimage, bdat.biWidth, bdat.biHeight, vbitcount) If (rcode <> NO_ERROR) Then MsgBox "Not enough memory", 0, "Error encountered!" Exit Sub End If ' Load image rcode = loadbmp(bmp_fname, tmpimage) If (rcode <> NO_ERROR) Then freeimage tmpimage ' Free image on error MsgBox "Cannot load file", 0, "Error encountered!" Exit Sub End If If (vbitcount = 1) Then ' If we loaded a 1-bit image, convert to 8-bit grayscale ' because jpeg only supports 8-bit grayscale or 24-bit color images rcode = allocimage(tmp2image, bdat.biWidth, bdat.biHeight, 8) If (rcode = NO_ERROR) Then rcode = convert1bitto8bit(tmpimage, tmp2image) freeimage tmpimage ' Replace 1-bit image with grayscale image copyimgdes tmp2image, tmpimage End If End If ' Save image rcode = savejpg(jpg_fname, tmpimage, quality) freeimage tmpimage End Sub ........... Add these defines and declarations to your Global module ........... ' Image descriptor Type imgdes ibuff As Long stx As Long sty As Long endx As Long endy As Long buffwidth As Long palette As Long colors As Long imgtype As Long bmh As Long hBitmap As Long End Type Type BITMAPINFOHEADER biSize As Long biWidth As Long biHeight As Long biPlanes As Integer biBitCount As Integer biCompression As Long biSizeImage As Long biXPelsPerMeter As Long biYPelsPerMeter As Long biClrUsed As Long biClrImportant As Long End Type Declare Function bmpinfo Lib "VIC32.DLL" (ByVal Fname As String, bdat As BITMAPINFOHEADER) As Long Declare Function allocimage Lib "VIC32.DLL" (image As imgdes, ByVal wid As Long, ByVal leng As Long, ByVal BPPixel As Long) As Long Declare Function loadbmp Lib "VIC32.DLL" (ByVal Fname As String, desimg As imgdes) As Long Declare Sub freeimage Lib "VIC32.DLL" (image As imgdes) Declare Function convert1bitto8bit Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes) As Long Declare Sub copyimgdes Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes) Declare Function savejpg Lib "VIC32.DLL" (ByVal Fname As String, srcimg As imgdes, ByVal quality As Long) As Long
Ahem... may I refer you to your first post?
Anyways, I suppose a JPG would be smaller, so try ths:
B][/QUOTE]
Thanks Mendhak,
By the way, what do u want to refer? ( my first post???) what do u want?
Anita
Quote:
...and store it as image file?
and then later,
But never mind that. My ESP hasn't come in handy today. :DQuote:
But just the problem is it is saving the image of desktop in .BMP format