Author Topic: Saving clipboard to bitmap file  (Read 3053 times)

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 2768
Saving clipboard to bitmap file
« on: April 24, 2020, 01:53:37 AM »
Here is an example to save the clipboard to a bitmap file. Hit PRINT SCREEN before running SaveClipboard :

Code: [Select]
SaveClipBoard.exe filename.bmp
Code: [Select]
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

  • Moderator
  • Member
  • *****
  • Posts: 2768
Re: Saving clipboard to bitmap file
« Reply #1 on: June 13, 2020, 06:14:01 AM »
With thanks to Hutch, here is another version simulating the keyboard hit PRINT SCREEN :

Code: [Select]
    invoke      keybd_event,VK_SNAPSHOT,0,\
                KEYEVENTF_EXTENDEDKEY,0

    invoke      SleepEx,250,0

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 2768
Re: Saving clipboard to bitmap file
« Reply #2 on: June 14, 2020, 11:00:18 PM »
Another version using Hutch's code example :

Code: [Select]
    invoke      CloseClipboard
    invoke      EmptyClipboard
    invoke      keybd_event,VK_SNAPSHOT,0,\
                KEYEVENTF_EXTENDEDKEY,0
    invoke      SendMessage,HWND_BROADCAST,\
                WM_CHAR,VK_SNAPSHOT,0

jj2007

  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Re: Saving clipboard to bitmap file
« Reply #3 on: June 15, 2020, 12:30:01 AM »
Code: [Select]
    invoke      SendMessage,HWND_BROADCAST,\
                WM_CHAR,VK_SNAPSHOT,0

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

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10572
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Saving clipboard to bitmap file
« Reply #4 on: June 15, 2020, 12:36:51 AM »
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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 2768
Re: Saving clipboard to bitmap file
« Reply #5 on: June 15, 2020, 06:12:21 AM »
Hi Jochen,

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