News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Saving clipboard to bitmap file

Started by Vortex, April 24, 2020, 01:53:37 AM

Previous topic - Next topic

Vortex

Here is an example to save the clipboard to a bitmap file. Hit PRINT SCREEN before running SaveClipboard :

SaveClipBoard.exe filename.bmp

include     SaveClipBoard.inc

.data
IID_IPicture  GUID <7BF80980h,0BF32h,101Ah,<8Bh,0BBh,00h,0AAh,00h,30h,0Ch,0ABh>>

message     db 'SaveClipBoard.exe filename.bmp',0

.data?
buffer      db 512 dup(?)

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC uses esi

LOCAL hBmp      :DWORD
LOCAL pBitmap   :DWORD
LOCAL hGlobal   :DWORD
LOCAL pcbSize   :DWORD
LOCAL pStream   :DWORD
LOCAL pd        :PICTDESC

    lea         esi,buffer
    invoke      ParseCmdLine,esi
    cmp         eax,2
    je          @f

    invoke      StdOut,ADDR message
    ret
@@:
    invoke      GetDesktopWindow
    invoke      OpenClipboard,eax
    invoke      GetClipboardData,CF_BITMAP
    mov         hBmp,eax
    invoke      CloseClipboard

    mov         pd.cbSizeofstruct,SIZEOF PICTDESC   ; initialize the PICTDESC structure
    mov         pd.picType,PICTYPE_BITMAP
    push        hBmp
    pop         pd.bmp.hbitmap
   
    invoke      OleCreatePictureIndirect,ADDR pd,\
                ADDR IID_IPicture,TRUE,ADDR pBitmap
                                                    ; create the OLE image

    invoke      CreateStreamOnHGlobal,NULL,TRUE,ADDR pStream
   
                                                    ; create the destination stream
                                                    ; to save the icon
    lea         eax,pcbSize
   
    coinvoke    pBitmap,IPicture,SaveAsFile,pStream,\
                TRUE,eax
               
    invoke      GetHGlobalFromStream,pStream,\
                ADDR hGlobal
               
    invoke      GlobalLock,hGlobal                  ; get the address pointing
                                                    ; the icon in memory
   
    invoke      WriteFileToDisc,DWORD PTR [esi+4],\
                eax,pcbSize
                                                    ; save the bitmap to disc
    coinvoke    pBitmap,IPicture,Release
    coinvoke    pStream,IStream,Release

    ret
   
main ENDP

END start

Vortex

With thanks to Hutch, here is another version simulating the keyboard hit PRINT SCREEN :

    invoke      keybd_event,VK_SNAPSHOT,0,\
                KEYEVENTF_EXTENDEDKEY,0

    invoke      SleepEx,250,0

Vortex

Another version using Hutch's code example :

    invoke      CloseClipboard
    invoke      EmptyClipboard
    invoke      keybd_event,VK_SNAPSHOT,0,\
                KEYEVENTF_EXTENDEDKEY,0
    invoke      SendMessage,HWND_BROADCAST,\
                WM_CHAR,VK_SNAPSHOT,0

jj2007

Quote from: Vortex on June 14, 2020, 11:00:18 PM
    invoke      SendMessage,HWND_BROADCAST,\
                WM_CHAR,VK_SNAPSHOT,0

Is sending a char to all open applications a good idea?

hutch--

Seems to work fine and it did not blow up the computer.  :tongue: Since only the OS can respond to the VK_SNAPSHOT, I doubt anything problematic is going to happen.

Vortex

Hi Jochen,

The message is a generalized one, that's true but it works. It's a practical method.