Board index » delphi » Loading Bitmap from Resource file

Loading Bitmap from Resource file

How can I load a bitmap from project's resource file and show the bitmap
on form's image? I have to use Delphi 1.0 so I can't use
LoadFromResourceName or LoadFromResourceID methods.

This example is a simplified version of what I have
tried and it doesn't work. Syntax is correct but nothing happens.
I have added with Image Editor a bitmap called
'SQUARE' to resource file in project's directory (bm.res).
When I tried to add a bitmap to project's own resource file (sdiapp.res)
it didn't stay there even if I saved the bitmap.

procedure TSDIAppForm.Button1Click(Sender: TObject);
var bm: TBitmap;
begin
   bm := TBitmap.Create;
   bm.handle := LoadBitmap(Application.Handle, 'SQUARE');
   Image1.Picture.Graphic := bm;
end;

Thank you for any help
        Jukka Palom?ki

 

Re:Loading Bitmap from Resource file


Quote
> Jukka Palom?ki <jukka.palom...@utu.fi> wrote in article

<31E6C96F....@utu.fi>...

Quote
> How can I load a bitmap from project's resource file and show the bitmap
> on form's image? I have to use Delphi 1.0 so I can't use
> LoadFromResourceName or LoadFromResourceID methods.

> This example is a simplified version of what I have
> tried and it doesn't work. Syntax is correct but nothing happens.
> I have added with Image Editor a bitmap called
> 'SQUARE' to resource file in project's directory (bm.res).
> When I tried to add a bitmap to project's own resource file (sdiapp.res)
> it didn't stay there even if I saved the bitmap.

> procedure TSDIAppForm.Button1Click(Sender: TObject);
> var bm: TBitmap;
> begin
>    bm := TBitmap.Create;
>    bm.handle := LoadBitmap(Application.Handle, 'SQUARE');
>    Image1.Picture.Graphic := bm;
> end;

> Thank you for any help
>         Jukka Palom?ki

You kinda had the right idea when you added the bitmap to the project
resource file.  You should add it to a resource file (but NOT the project
resource file).  Then add a compiler directive to link the resource file
with your project.  If you named the file MYRES1.RES, then add {$R
MYRES1.RES}

Re:Loading Bitmap from Resource file


Quote
Keith Rome wrote:

> > Jukka Palom?ki <jukka.palom...@utu.fi> wrote in article
> <31E6C96F....@utu.fi>...
> > How can I load a bitmap from project's resource file and show the bitmap
> > on form's image? I have to use Delphi 1.0 so I can't use
> > LoadFromResourceName or LoadFromResourceID methods.

> > This example is a simplified version of what I have
> > tried and it doesn't work. Syntax is correct but nothing happens.
> > I have added with Image Editor a bitmap called
> > 'SQUARE' to resource file in project's directory (bm.res).
> > When I tried to add a bitmap to project's own resource file (sdiapp.res)

> > it didn't stay there even if I saved the bitmap.

> > procedure TSDIAppForm.Button1Click(Sender: TObject);
> > var bm: TBitmap;
> > begin
> >    bm := TBitmap.Create;
> >    bm.handle := LoadBitmap(Application.Handle, 'SQUARE');
> >    Image1.Picture.Graphic := bm;
> > end;

> > Thank you for any help
> >         Jukka Palom?ki

> You kinda had the right idea when you added the bitmap to the project
> resource file.  You should add it to a resource file (but NOT the project
> resource file).  Then add a compiler directive to link the resource file
> with your project.  If you named the file MYRES1.RES, then add {$R
> MYRES1.RES}

Another Thing!

Call
    bm.handle := LoadBitmap(HInstance, 'SQUARE');
instead!

HInstance is the global instance handle of the module your code is executing from.

--
                                                     ____  /"//"/|__  __    
Gary Olliffe <ga...@parallax.co.uk>                 /   /|/ // / / / / /|  
Snail Mail : Parallax Solutions Ltd., Stone Court, / __/ / // / /> " <|/    
             Siskin Drive, Coventy, CV3 4FJ       /_/|_|/ // / /_/|/ /|    
Tel        : +44 (0)1203 514549                   |_|/./_//_/ /|_|/|_|/    
                                                       |_||_|/ Solutions Ltd

Re:Loading Bitmap from Resource file


Quote
Gary Olliffe wrote:

> > You kinda had the right idea when you added the bitmap to the project
> > resource file.  You should add it to a resource file (but NOT the project
> > resource file).  Then add a compiler directive to link the resource file
> > with your project.  If you named the file MYRES1.RES, then add {$R
> > MYRES1.RES}

What is the exact place?

Quote

> Another Thing!

> Call
>     bm.handle := LoadBitmap(HInstance, 'SQUARE');
> instead!

> HInstance is the global instance handle of the module your code is executing from.

Ok, I tried like this:

{$R+ MYRES1.RES}
procedure TMyClass.DrawSquare(var bitmap: TBitmap);
var tmp: TBitmap;
begin
  tmp := tmp.Create;
  tmp.handle := LoadBitmap(HInstance, 'SQUARE');
  bitmap.Canvas.Draw(10, 10, tmp);
end;
{$R- MYRES1.RES}

And in the main form unit where is one Image-component and one Button-component:

procedure TSDIAppForm.Button1Click(Sender: TObject);
var bm: TBitmap;
    mc: TMyClass;
begin
  bm := TBitmap.Create;
  bm.width := 100;
  bm.height := 100;
  mc := TMyClass.Create;
  mc.DrawSquare(bm);
  Image1.Picture.Graphics := bm;
end;  

This doesn't work. Compiler thinks this is alright but nothing is drawn to Image1. If I use LoadFromFile
instead of LoadBitmap this would work.

I am not sure if this is the right way to use Resource File Directive but this seemed to be the only way. If I
put only {$R MYRES1.RES} anywhere I got message "Duplicate Resource Directive (MYRES1.RES)".

Thank you very many

  Jukka Palom?ki

Re:Loading Bitmap from Resource file


Quote
Gary Olliffe wrote:

> > You kinda had the right idea when you added the bitmap to the project
> > resource file.  You should add it to a resource file (but NOT the project
> > resource file).  Then add a compiler directive to link the resource file
> > with your project.  If you named the file MYRES1.RES, then add {$R
> > MYRES1.RES}

What is the exact place?

Quote

> Another Thing!

> Call
>     bm.handle := LoadBitmap(HInstance, 'SQUARE');
> instead!

> HInstance is the global instance handle of the module your code is executing from.

Ok, I tried like this:

{$R+ MYRES1.RES}
procedure TMyClass.DrawSquare(var bitmap: TBitmap);
var tmp: TBitmap;
begin
  tmp := tmp.Create;
  tmp.handle := LoadBitmap(HInstance, 'SQUARE');
  bitmap.Canvas.Draw(10, 10, tmp);
end;
{$R- MYRES1.RES}

And in the main form unit where is one Image-component and one Button-component:

procedure TSDIAppForm.Button1Click(Sender: TObject);
var bm: TBitmap;
    mc: TMyClass;
begin
  bm := TBitmap.Create;
  bm.width := 100;
  bm.height := 100;
  mc := TMyClass.Create;
  mc.DrawSquare(bm);
  Image1.Picture.Graphics := bm;
end;  

This doesn't work. Compiler thinks this is alright but nothing is drawn to Image1. If I use LoadFromFile
instead of LoadBitmap this would work.

I am not sure if this is the right way to use Resource File Directive but this seemed to be the only way. If I
put only {$R MYRES1.RES} anywhere I got message "Duplicate Resource Directive (MYRES1.RES)".

Thank you for any help

  Jukka Palom?ki

Re:Loading Bitmap from Resource file


Quote
Jukka Palom?ki wrote:
>At 12:59 16/07/96 -0700, you wrote:
>Gary Olliffe wrote:

>> > You kinda had the right idea when you added the bitmap to the project
>> > resource file.  You should add it to a resource file (but NOT the project
>> > resource file).  Then add a compiler directive to link the resource file
>> > with your project.  If you named the file MYRES1.RES, then add {$R
>> > MYRES1.RES}

>What is the exact place?

>> Another Thing!

>> Call
>>     bm.handle := LoadBitmap(HInstance, 'SQUARE');
>> instead!

>> HInstance is the global instance handle of the module your code is executing from.

>Ok, I tried like this:

>{$R+ MYRES1.RES}

In the "Interface" section seems to be the best place to put
the directive and it is definitely:

{$R MYRES.RES}

Quote
>procedure TMyClass.DrawSquare(var bitmap: TBitmap);
>var tmp: TBitmap;
>begin
>  tmp := tmp.Create;
>  tmp.handle := LoadBitmap(HInstance, 'SQUARE');
>  bitmap.Canvas.Draw(10, 10, tmp);
>end;

Don't need this (the compiler is not accepting it, it's ignoring it!)

- Show quoted text -

Quote
>{$R- MYRES1.RES}

>And in the main form unit where is one Image-component and one Button-component:

>procedure TSDIAppForm.Button1Click(Sender: TObject);
>var bm: TBitmap;
>    mc: TMyClass;
>begin
>  bm := TBitmap.Create;
>  bm.width := 100;
>  bm.height := 100;
>  mc := TMyClass.Create;
>  mc.DrawSquare(bm);
>  Image1.Picture.Graphics := bm;
>end;  
>The above should be fine.

>This doesn't work. Compiler thinks this is alright but nothing is drawn to Image1. If I use LoadFromFile
>instead of LoadBitmap this would work.

>I am not sure if this is the right way to use Resource File Directive but this seemed to be the only way. If I
>put only {$R MYRES1.RES} anywhere I got message "Duplicate Resource Directive (MYRES1.RES)".

When I first started doing this kind of stuff I also got this message, try changing the
filename
and make sure it appears only once in your code.

Quote
>Thank you very many

>  Jukka Palom?ki

Let me know if this still doesn't work.  I'll have a think about it in the mean-time.

It can be done and I have done it for bitmaps and cursors, so stick with it.

Gary

--
                                                     ____  /"//"/|__  __    
Gary Olliffe <ga...@parallax.co.uk>                 /   /|/ // / / / / /|  
Snail Mail : Parallax Solutions Ltd., Stone Court, / __/ / // / /> " <|/    
             Siskin Drive, Coventy, CV3 4FJ       /_/|_|/ // / /_/|/ /|    
Tel        : +44 (0)1203 514549                   |_|/./_//_/ /|_|/|_|/    
                                                       |_||_|/ Solutions Ltd

Other Threads