Desktop Screen Capture with API Functions


It is easy to do a screen capture of the entire desktop. This method uses the API call GetDCEx from user32.dll. API functions from gdi32.dll are used to complete the task.

'
nomainwin
savefile$=""    'save file name
WindowWidth=600:WindowHeight=500
menu #1, "&File", "&Save As...",[saveAs],"E&xit",[quit]

open "Desktop Screen Capture" for graphics as #1
    #1 "trapclose [quit]"

    calldll #user32, "GetDCEx",_    'extended from GetDC
        0 as ulong,_        'handle of window, 0 = desktop
        0 as ulong,_        'clipping region, no used here
        _DCX_PARENTCLIP as long,_  'flags
        hDCx as ulong       'Device Context handle

    calldll #gdi32, "CreateCompatibleDC",hDCx as ulong, hDst as ulong

    calldll #gdi32, "CreateCompatibleBitmap", _
        hDCx as ulong, DisplayWidth as long, DisplayHeight as long,_
        hBitmap as ulong

    calldll #gdi32, "SelectObject", _
        hDst as ulong, hBitmap as ulong, oldObj as ulong

    calldll #gdi32, "BitBlt", hDst as ulong,0 as long, 0 as long,_
    DisplayWidth as long, DisplayHeight as long, hDCx as long, 0 as long, 0 as long,_
    _SRCCOPY as long, re as long

    'select original bmp into hDst DC
    calldll #gdi32, "SelectObject", hDst as ulong, oldObj as ulong, re as ulong

    'the screen capture is displayed for demonstration purposes
    loadbmp "capture",hBitmap
    #1 "down; cls; drawbmp capture 0 0;flush"
    wait

[quit]
    if hBitmap<>0 then
        calldll #gdi32,"DeleteObject",hBitmap as ulong, re as long
        unloadbmp ("capture")
    end if
    calldll #gdi32, "DeleteDC", hDst as ulong,result as long
    calldll #user32, "ReleaseDC",0 as ulong, hDC as ulong, r as long
    close #1:end


[saveAs]
    filedialog "Save As... ","*.bmp",savefile$
    if savefile$="" or hBitmap=0 then
        notice "Not able to save."
        wait
    end if
    b$=".bmp"
    if lower$(right$(savefile$,4))<>b$ then savefile$=savefile$+b$
    bmpsave "capture",savefile$
wait


Home

Source Code

Utilities

Internet

Games

Graphics

Media Demos

Snippets

DLL's

API Resources

Freeware

LB 4 Companion

Mastering LB 3

LB Workshop

Game Workshop

Links

Index