Results 1 to 9 of 9

Thread: capture screen/desktop?

  1. #1

    Thread Starter
    Hyperactive Member anita2002's Avatar
    Join Date
    Dec 2001
    Location
    In u'r Heart
    Posts
    482

    capture screen/desktop?

    Hi all,
    Can anyone knows the API whith which we can capture the screen/desktop and store it as image file?
    Help me,
    Anita
    Can't imagine life without VB
    (Various Boyfriends)

  2. #2
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651
    Yes, i have attached a program which does just that.
    Attached Files Attached Files
    Gilly

  3. #3

    Thread Starter
    Hyperactive Member anita2002's Avatar
    Join Date
    Dec 2001
    Location
    In u'r Heart
    Posts
    482
    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)

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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:
    '-----------------------------------------------------------
    ------------
    ' 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"

    End Sub
    [/code]

  5. #5

    Thread Starter
    Hyperactive Member anita2002's Avatar
    Join Date
    Dec 2001
    Location
    In u'r Heart
    Posts
    482
    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)

  6. #6
    Fanatic Member skald2k's Avatar
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    535
    Convert it to a JPG? Using the Windows API, because XP can do so.
    - If at first you dont succeed, then give up, cause you will never will!

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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.
    Ahem... may I refer you to your first post?

    ...can capture the screen/desktop and store it as image file?


    Anyways, I suppose a JPG would be smaller, so try ths:

    VB Code:
    1. Private Sub mnuconvertBMPtoJPG_Click()
    2.    Dim tmpimage As imgdes    ' Image descriptors
    3.    Dim tmp2image As imgdes
    4.    Dim rcode As Long
    5.    Dim quality As Long
    6.    Dim vbitcount As Long
    7.    Dim bdat As BITMAPINFOHEADER ' Reserve space for BMP struct
    8.    Dim bmp_fname As String
    9.    Dim jpg_fname As String
    10.  
    11.    bmp_fname = "test.bmp"
    12.    jpg_fname = "test.jpg"
    13.  
    14.    quality = 75
    15.    ' Get info on the file we're to load
    16.    rcode = bmpinfo(bmp_fname, bdat)
    17.    If (rcode <> NO_ERROR) Then
    18.       MsgBox "Cannot find file", 0, "Error encountered!"
    19.       Exit Sub
    20.    End If
    21.    
    22.    vbitcount = bdat.biBitCount
    23.    If (vbitcount >= 16) Then  ' 16-, 24-, or 32-bit image is loaded into 24-bit buffer
    24.       vbitcount = 24
    25.    End If
    26.    
    27.    ' Allocate space for an image
    28.    rcode = allocimage(tmpimage, bdat.biWidth, bdat.biHeight, vbitcount)
    29.    If (rcode <> NO_ERROR) Then
    30.      MsgBox "Not enough memory", 0, "Error encountered!"
    31.      Exit Sub
    32.    End If
    33.    
    34.    ' Load image
    35.    rcode = loadbmp(bmp_fname, tmpimage)
    36.    If (rcode <> NO_ERROR) Then
    37.       freeimage tmpimage ' Free image on error
    38.       MsgBox "Cannot load file", 0, "Error encountered!"
    39.       Exit Sub
    40.    End If
    41.  
    42.    If (vbitcount = 1) Then ' If we loaded a 1-bit image, convert to 8-bit grayscale
    43.        ' because jpeg only supports 8-bit grayscale or 24-bit color images
    44.      rcode = allocimage(tmp2image, bdat.biWidth, bdat.biHeight, 8)
    45.      If (rcode = NO_ERROR) Then
    46.          rcode = convert1bitto8bit(tmpimage, tmp2image)
    47.          freeimage tmpimage  ' Replace 1-bit image with grayscale image
    48.          copyimgdes tmp2image, tmpimage
    49.      End If
    50.    End If
    51.  
    52.    ' Save image
    53.    rcode = savejpg(jpg_fname, tmpimage, quality)
    54.    freeimage tmpimage
    55.  
    56. End Sub
    57.  
    58. ........... Add these defines and declarations to your Global module ...........
    59. ' Image descriptor
    60. Type imgdes
    61.    ibuff As Long
    62.    stx As Long
    63.    sty As Long
    64.    endx As Long
    65.    endy As Long
    66.    buffwidth As Long
    67.    palette As Long
    68.    colors As Long
    69.    imgtype As Long
    70.    bmh As Long
    71.    hBitmap As Long
    72. End Type
    73.  
    74. Type BITMAPINFOHEADER
    75.     biSize As Long
    76.     biWidth As Long
    77.     biHeight As Long
    78.     biPlanes As Integer
    79.     biBitCount As Integer
    80.     biCompression As Long
    81.     biSizeImage As Long
    82.     biXPelsPerMeter As Long
    83.     biYPelsPerMeter As Long
    84.     biClrUsed As Long
    85.     biClrImportant As Long
    86. End Type
    87.  
    88. Declare Function bmpinfo Lib "VIC32.DLL" (ByVal Fname As String, bdat As BITMAPINFOHEADER) As Long
    89. Declare Function allocimage Lib "VIC32.DLL" (image As imgdes, ByVal wid As Long, ByVal leng As Long, ByVal BPPixel As Long) As Long
    90. Declare Function loadbmp Lib "VIC32.DLL" (ByVal Fname As String, desimg As imgdes) As Long
    91. Declare Sub freeimage Lib "VIC32.DLL" (image As imgdes)
    92. Declare Function convert1bitto8bit Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes) As Long
    93. Declare Sub copyimgdes Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes)
    94. Declare Function savejpg Lib "VIC32.DLL" (ByVal Fname As String, srcimg As imgdes, ByVal quality As Long) As Long

  8. #8

    Thread Starter
    Hyperactive Member anita2002's Avatar
    Join Date
    Dec 2001
    Location
    In u'r Heart
    Posts
    482
    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
    Can't imagine life without VB
    (Various Boyfriends)

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    ...and store it as image file?

    and then later,

    But just the problem is it is saving the image of desktop in .BMP format
    But never mind that. My ESP hasn't come in handy today.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width