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 a bitmap from handle

Started by Vortex, November 10, 2024, 08:23:18 PM

Previous topic - Next topic

Vortex

Hello,

The procedure SaveBmpFromHandle saves a HBITMAP type handle to a file :

include SaveBmpFromHandle.inc

.data

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

.code

SaveBmpFromHandle PROC hBitmap:QWORD,FileName:QWORD

LOCAL BmpSize    :QWORD
LOCAL pd         :PICTDESC
LOCAL pPicture   :QWORD
LOCAL pStream    :QWORD
LOCAL hGlobal    :QWORD

    lea     rax,pd
    mov     PICTDESC.cbSizeofstruct[rax],SIZEOF PICTDESC
    mov     PICTDESC.picType[rax],PICTYPE_BITMAP
    mov     PICTDESC.bmp.hbitmap[rax],rcx
    mov     PICTDESC.bmp.hpal[rax],0

    invoke  OleCreatePictureIndirect,ADDR pd,\
            ADDR IID_IPicture,0,ADDR pPicture

    test    rax,rax
    jnz     finish

    invoke  CreateStreamOnHGlobal,0,TRUE,ADDR pStream

    test    rax,rax
    jnz     releasePict

    coinvk  pPicture,IPicture,SaveAsFile,pStream,TRUE,ADDR BmpSize

    test    rax,rax
    jnz     releaseStream

    invoke  GetHGlobalFromStream,pStream,ADDR hGlobal

    test    rax,rax
    jnz     releaseStream

    invoke  GlobalLock,hGlobal
 
    invoke  WriteFileToDisc,FileName,rax,BmpSize

releaseStream:

    coinvk  pStream,IStream,Release

releasePict:

    coinvk  pPicture,IPicture,Release

finish:

    ret
   
SaveBmpFromHandle ENDP

END

coinvoke macro for Masm64 :

coinvk MACRO ppv:REQ,interface:REQ,member:REQ,args:VARARG

    mov     rcx,ppv
    mov     r10,QWORD PTR [rcx]
    invoke  @CatStr(interface,<.>,member)[r10],rcx,args

ENDM

jj2007