Board index » cppbuilder » Howto create a Bitmap only with a pointer of Bitmap-Bits

Howto create a Bitmap only with a pointer of Bitmap-Bits

Hello,
I have a pointer to a buffer with bitmap data. Id like to create a HBITMAP
from this. How does it works?
Thank you, Michael!
 

Re:Howto create a Bitmap only with a pointer of Bitmap-Bits


Hi,

You have to create DIBSECTION (mydib) and store your pointer
into dib structure (mydib.dsBm.bmBits).
But, your buffer must be constructed as bitmap data acceptable for dib.
These rules must be fullfilled:

1. Image scanlines in dib are inverted, i.e. bottom image scanline is first
scanline in dib,
    top image scanline is last.
2. Scanlines are doubleword aligned (width%4 = 0). If width of your image does
not
    fullfill that criteria, you must add aditionall "pixels" to adjust scanline
to that criteria.
3. channels in image (if we speek about 24-bits depth) are also inverted, i.e.
    RGB bytes are now BGR bytes.

Hope this helps,
Sinisa
http://www.sigmapi-design.com

Quote
Michael Kunze wrote:
> Hello,
> I have a pointer to a buffer with bitmap data. Id like to create a HBITMAP
> from this. How does it works?
> Thank you, Michael!

Re:Howto create a Bitmap only with a pointer of Bitmap-Bits


Quote
> I have a pointer to a buffer with bitmap data. Id like to create a

HBITMAP

-----------------------------------------------------------------------
This solution is brought to you by Joe Hecht's TExcellent products,
solving Form.Print and bitmap printing problems. Joe Hecht's TExcellent
products can be found at: http://www.code4sale.com/joehecht
-----------------------------------------------------------------------

See the Windows API function CreateDIBitmap()

Joe

Re:Howto create a Bitmap only with a pointer of Bitmap-Bits


Hi,

CreateDIBitmap function creates  device-dependent bitmap (DDB) from DIB.
This function requires handle to some device context.
But  nown,  you  need to have pointers to  BITMAPINFO and BITMAPINFOHEADER
and pointer to initialization data.

To create BITMAP (HBITMAP) from some buffer the you can simply call
CreateBitmap or CreateBimtapIndirect.
In last case :

HBITMAP CreateBitmapIndirect(
  CONST BITMAP *lpbm    // bitmap data

and lpbm (BITMAP structure) is

typedef struct tagBITMAP {
  LONG   bmType;
  LONG   bmWidth;
  LONG   bmHeight;
  LONG   bmWidthBytes;
  WORD   bmPlanes;
  WORD   bmBitsPixel;
  LPVOID bmBits;

Quote
} BITMAP, *PBITMAP;

And again, LPVOID bmBits points to some buffer that must be constructed by
previously mentioned rules.

So, code would be something like this:

HBITMAP my_new_bitmap;
BITMAP my_bmp;
my_bmp.Width  = my_width;
my_bmp.Height = my_height;
...
... etc...
....
my_bmp.bmBits = my_buffer_of_pixels;
my_new_bitmap = CreateBitmapIndirect(&my_bmp);

This should work ,but  I really don't have time to try it.
Sinisa

P.S.
Check out
http://msdn.microsoft.com/library/psdk/gdi/bitmaps_87eb.htm

Quote
"Joe C. Hecht" wrote:
> > I have a pointer to a buffer with bitmap data. Id like to create a
> HBITMAP

> -----------------------------------------------------------------------
> This solution is brought to you by Joe Hecht's TExcellent products,
> solving Form.Print and bitmap printing problems. Joe Hecht's TExcellent
> products can be found at: http://www.code4sale.com/joehecht
> -----------------------------------------------------------------------

> See the Windows API function CreateDIBitmap()

> Joe

Other Threads