Board index » delphi » Loading BitMap from resource file

Loading BitMap from resource file

Hi
im using d3 and im trying to load a bitmap from a resource file using
the attached code.
I keep getting the resource not found error despite the fact  that
MyInstance and Rsrc are not nil;
H E L P............

procedure TForm1.Button1Click(Sender: TObject);
Var
  MyInstance: THandle;
  bmPicture: TBitMap;
  Rsrc: HRSRC;
begin
  bmPicture := TBitMap.Create;
  MyInstance := LoadLibrary('c:\program files\acdsee32\acdsee32.exe');
  Rsrc := FindResource(MyInstance, '#1', RT_ICON);
  bmPicture.LoadFromResourceID(MyInstance, Rsrc);
  Image1.Canvas.Draw(1, 1, bmPicture);
  bmPicture.Free;
  UnlockResource(Res);
  FreeLibrary(MyInstance);
end;

TNQ
attmail.com@dotans

 

Re:Loading BitMap from resource file


9Lives,

Try using LoadBitmap instead of the LoadFromResource call.

eg:
 .....
 bmPicture.Handle := LoadBitmap(MyInstance, '#1');
 ...

Note: you can also use the numeric identifier if you call it with
MakeIntResource, which for Delphi, is just a cast to a pchar.

Good Luck

Jeff

Re:Loading BitMap from resource file


Quote
In article <345EFDE5.45FD5...@attgold.net.il>, 9Lives wrote:
> im using d3 and im trying to load a bitmap from a resource file using
> the attached code.

My goodness, you must be a former C++ programmer; they always like to
make things far more difficult than necessary <g>. Replace your code
with this:

procedure TForm1.Button1Click(Sender: TObject);
Var
  Bitmap: TBitmap;
begin
  Bitmap := TBitmap.Create;
  try
    Bitmap.Handle := LoadBitmap(hInstance, 'BITMAP_NAME');
    Image1.Canvas.Draw(1, 1, Bitmap);
  finally
    Bitmap.Free;
  end;
end;

Also, ensure that a) the resource name is in ALL CAPS; b) that you
haven't used the default Delphi resource file (which has a file name of
<project name>.res) since this file is overwritten each time you
recompile; and c) you've included the resource file in your project via
the $R directive, like this:

  {$R MYRES.RES}

Cheers.

--
Rick Rogers (TeamB) | Fenestra Technologies

Re:Loading BitMap from resource file


Quote
In article <345EFDE5.45FD5...@attgold.net.il>, 9Lives wrote:
> Rsrc := FindResource(MyInstance, '#1', RT_ICON);

Unless your icon name is literally #1, you should probably be using:

 Rsrc := FindResource(MyInstance, MAKEINTRESOURCE(1), RT_ICON);

Regards
Ralph (TeamB)
Herrsching, Germany, Tue, 04 Nov 1997 14:05 +0100 CET

Re:Loading BitMap from resource file


Quote
> My goodness, you must be a former C++ programmer; they always like to
> make things far more difficult than necessary <g>

Hmm, I wonder if I should take offense to that. <g>  

BTW, he was loading from a dll so you need the hLib not hInstance.  Also, if
you use a numeric identifier you don't need all caps.  The nice thing about the
numeric id is that you can include a pas file in the rc file.  That way you get
to use the same source for id constants in both your program and the rc file.

Jeff

Re:Loading BitMap from resource file


In article <VA.00000b7e.0027b...@Rosetta.fenestra.com>, Rick Rogers [TeamB]
wrote:

Quote
> My goodness, you must be a former C++ programmer;

Hey. I resemble that remark<g>

Regards
Ralph (TeamB)
Herrsching, Germany, Tue, 04 Nov 1997 18:49 +0100 CET

Re:Loading BitMap from resource file


Quote
In article <345EFDE5.45FD5...@attgold.net.il>, 9Lives wrote:
> Rsrc := FindResource(MyInstance, '#1', RT_ICON);
>   bmPicture.LoadFromResourceID(MyInstance, Rsrc);

To add a bit confusion to the thread: your main error is that you use
FindResource where it has no place. It does not return a resource id but a
resource handle. So s{*word*99} it, if the resource has indeed a numeric id of 1
use bmPicture.LoadFromResourceID( MyInstance, 1 ); if it has a string id
use LoadFromresourceName instead.

Peter Below (TeamB)  100113.1...@compuserve.com)

Re:Loading BitMap from resource file


Quote
Peter Below wrote:
> In article <345EFDE5.45FD5...@attgold.net.il>, 9Lives wrote:
> > Rsrc := FindResource(MyInstance, '#1', RT_ICON);
> >   bmPicture.LoadFromResourceID(MyInstance, Rsrc);

> use bmPicture.LoadFromResourceID( MyInstance, 1 ); if it has a string id
> use LoadFromresourceName instead.

Hi all.
Quote
>this was my main problem since using that method raise a

  access violation error despite the fact that My Instance
  has a value.
Quote
>i get the resource file name and the resource number from

  the Registry (Shell key) so maybe the problem is in there.

what do u think????

Re:Loading BitMap from resource file


Quote
> >i get the resource file name and the resource number from
>   the Registry (Shell key) so maybe the problem is in there.

Are you sure you get a valid instance handle from LoadLibrary? You apply it
to an EXE and it is not designed to work with anything but DLLs. Perhaps that
is the problem. To get an icon from an EXE you should use the ExtractIcon or
ExtractIconEx functions.

Peter Below (TeamB)  100113.1...@compuserve.com)

Re:Loading BitMap from resource file


Quote
Peter Below wrote:
> > >i get the resource file name and the resource number from
> >   the Registry (Shell key) so maybe the problem is in there.

> To get an icon from an EXE you should use the ExtractIcon or
> ExtractIconEx functions.

TNQ Peter that was my problem, the program is working perfectly now
and your help is very mach appreciated.

Re:Loading BitMap from resource file


Peter,

Good eye.  I had to go back and look at the original question because I would
have bet money that he was using the function against a dll.  Your right, it
was an exe.

Jeff

Other Threads