Board index » cppbuilder » TBitmap Handle

TBitmap Handle

Given I have as input a pre-loaded bitmap and associated HBITMAP handle,

how do I construct a TBitmap object. I tried doing

TBitmap *tbmp = new TBitmap();
tbmp->Handle = hBitmap;

But that did not fly very well. Seems there should be a special
constructor that
takes HBITMAP as parameter.

 

Re:TBitmap Handle


Huh,

maybe there is other better and shorter solution, but I would create
empty TBitmap with the same size of pre-loaded bitmap and perform
BitBlt.

HBITMAP new_bitmap_handle;
DIBSECTION dib;
Graphics::TBitmap new_bitmap = new Graphics::TBitmap();

GetObject(pre_loaded_handle, sizeof(DIBSECTION),&dib);

switch(dib.dsBmih.biBitCount)
    {
    case 24:
            new_bitmap->PixelFormat = pf24bit;
           break;
    case 8:
            new_bitmap->PixelFormat = pf8bit;
           break;
   // ...etc..etc.... all other formats
    }
new_bitmap->Width   =  (int) dib.dsBmih.biWidth;
new_bitmap->Height  =  (int) dib.dsBmih.biHeight;
//
// handle of new bitmap
//
new_bitmap_handle = new_bitmap->Handle;
//
// And with a little luck
//
HDC hsrcDc=CreateCompatibleDC(NULL);
 HDC hDstdc=CreateCompatibleDC(NULL);
SelectObject(hsrcDc, pre_loaded_handle);
SelectObject(hDstdc, new_bitmap_handle);
BitBlt(hDstdc, 0, 0, new_bitmap->Width, new_bitmap->Height, hsrcDc, 0, 0,
srcCOPY);
//
// After that you can get rid of pre-loaded bitmap
//

Maybe there is a simpler way.......???

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

Quote
Roger Dunn wrote:
> Given I have as input a pre-loaded bitmap and associated HBITMAP handle,

> how do I construct a TBitmap object. I tried doing

> TBitmap *tbmp = new TBitmap();
> tbmp->Handle = hBitmap;

> But that did not fly very well. Seems there should be a special
> constructor that
> takes HBITMAP as parameter.

Re:TBitmap Handle


Quote
> TBitmap *tbmp = new TBitmap();
> tbmp->Handle = hBitmap;

I do this all of the time.  It always works as long as I have a valid
HBITMAP.

Are you sure that the HBITMAP is valid?
How are you displaying the image?

--
Regards,
Scott
http://members.home.net/scottheiman/

Re:TBitmap Handle


It was complaining about invalid size. I will check this again.
Quote
Scott Heiman wrote:
> > TBitmap *tbmp = new TBitmap();
> > tbmp->Handle = hBitmap;

> I do this all of the time.  It always works as long as I have a valid
> HBITMAP.

> Are you sure that the HBITMAP is valid?
> How are you displaying the image?

> --
> Regards,
> Scott
> http://members.home.net/scottheiman/

Re:TBitmap Handle


Hi,

there may be problems on how bitmap is created. It must be doubleword
aligned
and it must be DIB. If you have DDB setting tbmp->Handle will not work.

Sinisa Petric
http://www.sigmapi-design.com

Quote
Roger Dunn wrote:
> It was complaining about invalid size. I will check this again.

> Scott Heiman wrote:

> > > TBitmap *tbmp = new TBitmap();
> > > tbmp->Handle = hBitmap;

> > I do this all of the time.  It always works as long as I have a valid
> > HBITMAP.

> > Are you sure that the HBITMAP is valid?
> > How are you displaying the image?

> > --
> > Regards,
> > Scott
> > http://members.home.net/scottheiman/

Other Threads