News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

copy image from clipboard to memory

Started by loki_dre, April 15, 2008, 05:58:45 PM

Previous topic - Next topic

loki_dre

how can I copy image from clipboard to memory?

hutch--

loki,

Here is a masm32 procedure to get TEXT from the clipboard. It may be useful to you in working out how to get a bitmap from the clipboard.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

GetClipboardText proc

    push ebx
    push esi
    push edi

    ; --------------------------------------------------------
    ; if the return value is not zero, deallocate the returned
    ; memory handle with GlobalFree() or the macro "free" when
    ; the data is no longer required.
    ; --------------------------------------------------------
    invoke OpenClipboard,NULL                   ; open clipboard
    .if rv(IsClipboardFormatAvailable,CF_TEXT) != 0 ; if text available
      invoke GetClipboardData,CF_TEXT           ; get pointer to text
      mov ebx, eax
      invoke StrLen,eax                         ; get text length
      mov esi, eax
      mov edi, alloc(esi)                       ; allocate that much memory
      cst edi, ebx
      invoke CloseClipboard                     ; close the clipboard
      mov eax, edi                              ; return memory handle
      jmp bye
    .else                                       ; else
      xor eax, eax                              ; set return to zero
      invoke CloseClipboard                     ; close the clipboard
      jmp bye
    .endif

  bye:
    pop edi
    pop esi
    pop ebx

    ret

GetClipboardText endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

loki_dre

hmmm....
I i'm pretty sure that the call:

invoke GetClipboardData, CF_BITMAP

will return the HBITMAP or handle?!?!??! into eax
any idea how to read or convert this format?

NightWare

look at BITMAPINFO or BITMAPINFOHEADER structures, depending of your need.

jj2007

Here is a very simple example ruthlessly copied from here:

if ( OpenClipboard() )
{

//Get the clipboard data
HBITMAP handle = (HBITMAP)GetClipboardData(CF_BITMAP);
CBitmap * bm = CBitmap::FromHandle(handle);

CClientDC cdc(this);
CDC dc;
dc.CreateCompatibleDC(&cdc);
dc.SelectObject(bm);
cdc.BitBlt(0,0,200,200,&dc,0,0,SRCCOPY);

CloseClipboard();

}


The translation to MASM syntax is straightforward.

loki_dre

how do I do the conversion (HBITMAP) from (HBITMAP) GetClipboardData(uFormat);

Darrel

I use GetClipboardData with CF_DIB. This returns a BITMAPINFO struct followed by the bitmap bits.

Regards,   :8)

Darrel

loki_dre

got any code to share?
my BMP output doesn't look right....

jj2007

Quote from: loki_dre on April 18, 2008, 03:47:44 AM
got any code to share?
my BMP output doesn't look right....

Here is some that works, at least with the UseDC fork.

include \masm32\include\masm32rt.inc

;--- This is Iczelion's Win32 Tutorial No 3.

WinMain proto :HINSTANCE,:HINSTANCE,:LPSTR,:DWORD

.data

ClassName db "SimpleWinClass",0
AppName  db "Our First Window",0

.data?
   
hInstance HINSTANCE ?
CommandLine LPSTR ?
sx dd ?
sy dd ?

.code
   
start:
invoke GetModuleHandle, NULL
mov    hInstance,eax
invoke GetCommandLine
mov    CommandLine,eax
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
invoke ExitProcess,eax

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND
mov   wc.cbSize,SIZEOF WNDCLASSEX
mov   wc.style, CS_HREDRAW or CS_VREDRAW
mov   wc.lpfnWndProc, OFFSET WndProc
mov   wc.cbClsExtra,NULL
mov   wc.cbWndExtra,NULL
push  hInstance
pop   wc.hInstance
mov   wc.hbrBackground, COLOR_BACKGROUND + 1 ;COLOR_WINDOW+1
mov   wc.lpszMenuName, NULL
mov   wc.lpszClassName, OFFSET ClassName
invoke LoadIcon, NULL, IDI_APPLICATION
mov   wc.hIcon, eax
mov   wc.hIconSm, eax
invoke LoadCursor, NULL, IDC_ARROW
mov   wc.hCursor,eax
invoke GetSystemMetrics, SM_CXSCREEN ; get screen width in pixels
mov sx, eax
invoke GetSystemMetrics, SM_CYSCREEN ; get screen height in pixels
sub eax, 30
mov sy, eax
invoke RegisterClassEx, addr wc
INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,
          WS_OVERLAPPEDWINDOW,
0,0, sx, sy,
NULL, NULL, hInst, NULL
mov hwnd,eax
invoke ShowWindow, hwnd,SW_SHOWNORMAL
invoke UpdateWindow, hwnd
.while 1
invoke GetMessage, ADDR msg, NULL, 0, 0
.break .if eax==0
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
.endw
mov     eax,msg.wParam
ret
WinMain endp

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL hBm:HBITMAP, ps:PAINTSTRUCT, dc:HDC, cdc:HDC
  mov eax, uMsg
  .if eax == WM_DESTROY
invoke PostQuitMessage,NULL
  .elseif eax==WM_PAINT
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
invoke OpenClipboard, 0
.if eax
invoke IsClipboardFormatAvailable, CF_BITMAP
.if eax
invoke GetClipboardData, CF_BITMAP
.if eax==0
invoke MessageBox, 0, chr$("bm"), addr AppName, MB_OK
.endif
mov hBm, eax
useDC EQU 1
if useDC
invoke GetDC, hWnd
else
invoke BeginPaint, hWnd, addr ps
endif
.if eax==0
invoke MessageBox, 0, chr$("bp"), addr AppName, MB_OK
.endif
mov dc, eax
invoke CreateCompatibleDC, dc
.if eax==0
invoke MessageBox, 0, chr$("cdc"), addr AppName, MB_OK
.endif
mov cdc, eax
invoke SelectObject, eax, hBm
push eax
invoke BitBlt, dc, 0, 0, sx, sy, cdc, 0, 0, SRCCOPY
.if eax==0
invoke MessageBox, 0, chr$("blt"), addr AppName, MB_OK
.endif
pop eax
invoke SelectObject, cdc, eax
if useDC
invoke ReleaseDC, hWnd, dc
else
invoke EndPaint, hWnd, addr ps
endif
.if eax==0
invoke MessageBox, 0, chr$("ep"), addr AppName, MB_OK
.endif
.else
invoke MessageBox, 0, chr$("No picture on clipboard"), addr AppName, MB_OK
.endif
invoke CloseClipboard
.endif
xor eax, eax
inc eax
  .else
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
  .endif
  ret
WndProc endp
end start