[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1002
  • Last Modified:

File mapping HBITMAP.


   I want to pass a HBITMAP from one process to another using memory mapped file. Given the HBITMAP object, is it possible to copy the entire object into the memory mapped buffer so that the other project can see it? How do I copy the entire HBITMAP into the memory buffer and how to retrieve it?


1 Solution
HBITMAP handle cannot be passed to another process. To pass all bitmap data you need device-independent bitmap (DIB). This means, bitmap must be created using CreateDIBSection function. Device-depended bitmap (DDP), created by CreateCompatibleBitmap function, cannot be saved or copied to memory block.
Advantage of DIB is that it allows direct access to bitmap bits (array of pixels). It may be accessed using GetBitmapBits and SetBitmapBits API.
Having DIB handle, you can save all bitmap data using one of the following ways:

1) If both sender and receiver know bitmap format (for example, RGB 24 bits per pixels), only bitmap bits may be copied to memory-mapped file.
For example, we have 24 bpp RGB DIB with 100*100 size. Sender creates DIB using CreateDIBSection and fills bitmap by some way. Memory block required for this bitmap bits must have 100*100*3 bytes size. Let's say that:
void* pMapping - pointer to shared memory block
HBITMAP hDIB - DIB handle.

To copy bitmap bits to memory block sender executes the following code:

GetBitmapBits(hDIB, 100*100*3, pMapping);

Receiver creates DIB by the same way as sender.
void* pMapping - pointer to shared memory block (the same as receiver)
HBITMAP hDIB - DIB handle (created, bit not filled).

To read bitmap bits from the shared memory block receiver executes the following code:

SetBitmapBits(hDIB, 100*100*3, pMapping);

2) If bitmap parameters (color depth, dimensions etc.) are unknown to receiver, it's better to write bitmap in BMP format to shared memory block. To do this sender should fill and write the following information:
- color palette (for RGB files it is not necessary);
- bitmap bits, as in p.1.

Receiver reads this memory block, creates it's own DIB according to information in BITMAPINFOHEADER, and fills bitmap bits like in p.1.

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now