Is there an easy way to put a DIB on the clipboard?

A

active

Is there an easy way to add a Dib to the clipboard.



I've tried these commands.

myData.SetData(DataFormats.Bitmap, True, MyBitmap)

SetClipboardData(CLIPFORMAT.CF_BITMAP, MyBitmap.GetHbitmap)

SetClipboardData(CLIPFORMAT.CF_DIB, MyBitmap.GetHbitmap)



Clipbrd may menu the DIB but even when it does it can not display it.

It's not necessary that I add the DIB but if it's easy I'd like to.





Thanks
 
M

Michael Phillips, Jr.

CLIPFORMAT.CF_DIB is a handle based clipboard format.

The handle is allocated with GlobalAlloc and the memory must be moveable.

The DIB's memory layout is as follows:
1) BITMAPINFOHEADER
2) ColorTable or Bitfields array, if any(i.e, predicated on bpp of image)
3) Bitmap's bits

The easiest way to put a DIB on the clipboard is as follows:
1) Load a Bitmap as a MemoryStream
2) Use GlobalAlloc to allocate memory. The allocated size is the sizeof the
BITMAPINFOHEADER
plus the sizeof the colortable. if any, and the size of the bitmap's
bits.
3) Seek past the BITMAPFILEHEADER in the MemoryStream(i.e.,14 bytes)
4) Copy the BITMAPINFOHEADER, ColorTable, if any, and the bits to the block
of memory allocated in step 2.
You must use GlobalLock and the Interop's Marshal copy routines.
5) After you have finished. Be sure to use GlobalUnlock. Pass the HGLOBAL
memory handle to the clipboard.
SetClipboardData(CLIPFORMAT.CF_DIB, hGlobal). Where hGlobal is an
IntPtr.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top