Board index » delphi » Loading bitmaps from resources

Loading bitmaps from resources

In article <42mf65$...@pepper.research.nokia.fi>,
   harri.kiljan...@research.nokia.com (Harri Kiljander) wrote:

Quote
>It seems that I'm doing something wrong but what.
>I'm trying to load a bitmap from resource but it won't work:
>LoadBitmap returns 0. I've tried with 2, 16 and 256 color
>bitmaps and there's no difference. I've checked my EXE
>with a resource editor and the bitmap is there (first
>it kept on vanishing since I had it in my application's
>primary resource file which seems to be overwritten
>by Delphi every time I save the project).

>var hb: HBitmap;
>begin
>    ...
>    hb := LoadBitmap(HInstance, 'Foobar');    { returns 0 }
>    TheForm.Image96x59.Picture.Bitmap.Handle := hb;
>    TheForm.Image96x59.Repaint;
>    ...

Harri

First, you need to create your bitmap object before using it. Second, put the
bitmap name in upper case. It's also a good idea to put all this in a
try...finally block so resources are freed even if the bitmap can't be
created. This should work:

var
   theBitmap: TBitmap;
begin
   theBitmap := TBitmap.Create;
   try
      theBitmap.Handle := LoadBitmap(hInstance,'FOOBAR');
      theForm.Image96x59.Picture.Graphic := theBitmap;
      {refresh image etc}
   finally
      theBitmap.Free;
   end;
end;      

HTH

Ian
i...@dircon.co.uk

 

Re:Loading bitmaps from resources


It seems that I'm doing something wrong but what.
I'm trying to load a bitmap from resource but it won't work:
LoadBitmap returns 0. I've tried with 2, 16 and 256 color
bitmaps and there's no difference. I've checked my EXE
with a resource editor and the bitmap is there (first
it kept on vanishing since I had it in my application's
primary resource file which seems to be overwritten
by Delphi every time I save the project).

var hb: HBitmap;
begin
    ...
    hb := LoadBitmap(HInstance, 'Foobar');    { returns 0 }
    TheForm.Image96x59.Picture.Bitmap.Handle := hb;
    TheForm.Image96x59.Repaint;
    ...

BTW; is it possible to approach Borland directly with problems
like this? The local support here in Finland is practically nil.

--
Harri Kiljander         email:  harri.kiljan...@research.nokia.com
Nokia Research Center   tel:    +358-0-4376-6415

Other Threads