Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29270 Discussions

How to copy a bitmap to the clipboard

michael_green
Beginner
1,553 Views

Hi All,

I would like to be able to copy an image that I have captured using BitBlt to the Windows clipboard. If I've got the image captured into a memory device context how do I relate that to the required second argument for CopyMemory, that is, the "pointer to the address of the block to copy"?

Many thanks for any help or insights.

Mike

0 Kudos
5 Replies
onkelhotte
New Contributor II
1,553 Views

Hi Mike,

if you are using a QuickWin Application, it is easy to copy a QuickWin Window into the clipboard. I can send you a subroutine that does that for you.

But I dont know how to copy a memory block into the clipboard. Im not sure if the clipboard has a unique address in memory.

Markus

0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,553 Views
Should be as simple as:
IF (OpenClipboard(NULL)) THEN
hBmp = GetCurrentObject(hMemDC, OBJ_BITMAP)
hData = SetClipboardData(CF_BITMAP, hBmp)
bSt = CloseClipboard()
END IF



0 Kudos
anthonyrichards
New Contributor III
1,553 Views

This works for me: Assume that the bitmap is ixwidth pixels wideby iywidth pixels high and assume it has been created and selected into a device context with handle hMemDC and drawn on. hWnd and hWndDC are the handles to the window and tothe window'sdevice context.

use user32
use dfwin
use dfwinty
use dflib
Implicit none ! all undeclared variables should be defined as INTEGER(4)
integer(4) hWnd, hWndDC
***************************************************************************
! Bitmap stuff...
INTEGER::hBmp, iSizeImage, cClrBits, iSt, hmemBmBits, hClipboard, hBitmap, hOldBitmap, ixwidth, iywidth, hMemDC,hWndDC
INTEGER(1)::bmBits(*); POINTER(pbmBits, bmBits)
TYPE(T_BITMAPINFO)::BI; POINTER(pBI,BI)
TYPE(T_BITMAP):: Bmp
!***************************************************************************
TYPE (T_RECT)rc

retint=getclientrect(hWnd,rc)
ixwidth=rc%right-rc%left
iywidth=rc%bottom-rc%top

hBitmap=CreateCompatibleBitmap(hWndDC,ixwidth,iywidth)
hOldBitmap=SelectObject(hMemDC, hBitmap)
!
! Somewhere in here is your code todraw on the device context hMemDC. If you have not created hMemDC, just change references to hMemDCin the following to hWndDC

!Get the color depth bits of the bitmap
cClrBits=GetDeviceCaps(hMemDC, BITSPIXEL)
iSizeImage=((ixwidth * cClrBits + 31) /8) * iywidth

!Allocate memory for BITMAPINFO structure. It consists of BITMAPINFOHEADER
!structure followed immediately in memory by array of color bits.
hmemBmBits = GlobalAlloc(GMEM_MOVEABLE+GMEM_DDESHARE, iSizeImage+SIZEOF(BI%bmiHeader))
pbmBits= GlobalLock(hmemBmBits)
pBI = pbmBits

!Fill in the BITMAPINFOHEADER.
BI%bmiHeader%biSize= SIZEOF(BI%bmiHeader)
BI%bmiHeader%biWidth = ixwidth
BI%bmiHeader%biHeight= iywidth
BI%bmiHeader%biPlanes= 1 !1 plane (e.g. icons have 2)
BI%bmiHeader%biBitCount= cClrBits !color depth
BI%bmiHeader%biCompression = BI_RGB !no compression
BI%bmiHeader%biSizeImage = iSizeImage
BI%bmiHeader%biXPelsPerMeter = 96*100/2.54+1
BI%bmiHeader%biYPelsPerMeter = 96*100/2.54+1
BI%bmiHeader%biClrImportant = 0

!Get the bitmap bits into BI. Actual bits start at this offset--v
iSt= GetDIBits(hMemDC, hBitmap, 0, iywidth, pbmBits+SIZEOF(BI%bmiHeader), pBI, DIB_RGB_COLORS)

!clean up, only if you have created a compatible bitmap in hMemDC
hBitmap=SelectObject(hMemDC, hOldBitmap)
retint=DeleteObject(hBitmap)
retint=DeleteDC(hMemDC)

hClipboard = OpenClipboard(hWnd)
retint= EmptyClipboard()

iSt= SetClipboardData(CF_DIB, hmemBmBits) !Copy the contents onto clipboard

iSt= CloseClipboard()!Clean up
iSt= GlobalUnlock(hmemBmBits)
iSt= GlobalFree(hmemBmBits)

0 Kudos
michael_green
Beginner
1,553 Views

Thanks Guys for the help.

Everything is working fine except for one small problem: I can't get EmptyClipboard to work. Here's the code I'm using:

if(OpenClipboard(null))then
iret =EmptyClipboard()
hBmp = GetCurrentObject(hMemDC,OBJ_BITMAP)
hData = SetClipboardData(CF_BITMAP,hBmp)
iret = CloseClipboard()
end if


After a couple of uses the clipboard tells me I must clear it before I can add any more. I have also found that a VF example program (one of Norman Lawrence's) causes exactly the same behaviour. Is there some setting of the clipboard on my machine that I must adjust, or is there something I just don't understand?

With many thanks in advance,

Mike

0 Kudos
anthonyrichards
New Contributor III
1,553 Views
Try OpenClipBoard with a valid handle, not NULL?
0 Kudos
Reply