Delphi and Kylix

Moderators: pritaeas
Number of threads: 7264
Number of posts: 19073

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Loading Bitmap Resource Problem Posted by avishai on 1 Oct 2003 at 9:08 AM
Hi All,

I have a proble with the following code. It fails on "LOADFROMSTREAM" with the message "Bitmap Image is not valid".


implementation

{$R *.dfm}
{$R Nikud.res}

procedure TForm1.FormCreate(Sender: TObject);
var
HebRes : TResourceStream;
begin
HebRes := nil;
try
HebRes := TResourceStream.Create(HInstance,'DAGESH',RT_Bitmap);
HebRes.Position := 0;
{Next line gives ERROR: "Bitmap Image is not valid"}
Image1.Picture.Bitmap.LoadFromStream(HebRes);
ImageList1.AddMasked(Image1.Picture.Bitmap,clWhite);
finally
if HebRes <> nil then HebRes.Free;
end;
end;

end.

Report
Re: Loading Bitmap Resource Problem Posted by zibadian on 1 Oct 2003 at 4:34 PM
: Hi All,
:
: I have a proble with the following code. It fails on "LOADFROMSTREAM" with the message "Bitmap Image is not valid".
:
:
: implementation
:
: {$R *.dfm}
: {$R Nikud.res}
:
: procedure TForm1.FormCreate(Sender: TObject);
: var
: HebRes : TResourceStream;
: begin
: HebRes := nil;
: try
: HebRes := TResourceStream.Create(HInstance,'DAGESH',RT_Bitmap);
: HebRes.Position := 0;
: {Next line gives ERROR: "Bitmap Image is not valid"}
: Image1.Picture.Bitmap.LoadFromStream(HebRes);
: ImageList1.AddMasked(Image1.Picture.Bitmap,clWhite);
: finally
: if HebRes <> nil then HebRes.Free;
: end;
: end;
:
: end.
:
:
Perhaps the TBitmap.LoadFromResourceName() will do the trick. Also you should check if the resource is correctly added to the program code.
Report
Re: Loading Bitmap Resource Problem Posted by avishai on 2 Oct 2003 at 6:50 AM
: Perhaps the TBitmap.LoadFromResourceName() will do the trick. Also you should check if the resource is correctly added to the program code.
:

Thanks for your answer.

Yes I already tried LoadFromResourceName and some other things as well. The only way I know to check the validity of the bitmap is to reopen the resource file in the Delphi Image editor and then go to EDIT the bitmap. If it's able to open it, I take it to be valid. Maybe I'm wrong on that.


Report
Re: Loading Bitmap Resource Problem Posted by zibadian on 2 Oct 2003 at 7:13 AM
: : Perhaps the TBitmap.LoadFromResourceName() will do the trick. Also you should check if the resource is correctly added to the program code.
: :
:
: Thanks for your answer.
:
: Yes I already tried LoadFromResourceName and some other things as well. The only way I know to check the validity of the bitmap is to reopen the resource file in the Delphi Image editor and then go to EDIT the bitmap. If it's able to open it, I take it to be valid. Maybe I'm wrong on that.
:
I assume that you linked it into the source code using the $R directive.
You can check if the resource exists in your app with the FindResource() function. Here is what the code should look like:
   MyRes := FindResource(hInstance, PChar(MyResourceName), RT_Bitmap);
   if MyRes = 0 then
     ShowMessage('Error: resource doesn't exist')
   else
     // Use resource;

You can find more info on the function in the Windows SDK help files.
Report
Re: Loading Bitmap Resource Problem Posted by avishai on 2 Oct 2003 at 7:46 AM
: I assume that you linked it into the source code using the $R directive.
: You can check if the resource exists in your app with the FindResource() function. Here is what the code should look like:
:
:    MyRes := FindResource(hInstance, PChar(MyResourceName), RT_Bitmap);
:    if MyRes = 0 then
:      ShowMessage('Error: resource doesn't exist')
:    else
:      // Use resource;
: 

: You can find more info on the function in the Windows SDK help files.
:

Thanks again for your input. I added your code to my procedure and I get the same result. MyRes returns a non-zero number. Here's the code.

implementation

{$R *.dfm}
{$R Nikud.res}{ <--- This is the Resource File }

procedure TForm1.FormCreate(Sender: TObject);
var
HebRes : TResourceStream;
MyRes : Integer;
begin
HebRes := nil;
try
MyRes := FindResource(hInstance, PChar('DAGESH'), RT_Bitmap);
if MyRes = 0 then
ShowMessage('Error: resource doesn''t exist')
else begin
HebRes := TResourceStream.Create(HInstance,'DAGESH',RT_Bitmap);
HebRes.Position := 0;
{Next line gives ERROR: "Bitmap Image is not valid"}
Image1.Picture.Bitmap.LoadFromStream(HebRes);
ImageList1.AddMasked(Image1.Picture.Bitmap,clWhite);
end;
finally
if HebRes <> nil then HebRes.Free;
end;
end;

end.


Report
Re: Loading Bitmap Resource Problem Posted by zibadian on 2 Oct 2003 at 8:07 AM
: : I assume that you linked it into the source code using the $R directive.
: : You can check if the resource exists in your app with the FindResource() function. Here is what the code should look like:
: :
: :    MyRes := FindResource(hInstance, PChar(MyResourceName), RT_Bitmap);
: :    if MyRes = 0 then
: :      ShowMessage('Error: resource doesn't exist')
: :    else
: :      // Use resource;
: : 

: : You can find more info on the function in the Windows SDK help files.
: :
:
: Thanks again for your input. I added your code to my procedure and I get the same result. MyRes returns a non-zero number. Here's the code.
:
: implementation
:
: {$R *.dfm}
: {$R Nikud.res}{ <--- This is the Resource File }
:
: procedure TForm1.FormCreate(Sender: TObject);
: var
: HebRes : TResourceStream;
: MyRes : Integer;
: begin
: HebRes := nil;
: try
: MyRes := FindResource(hInstance, PChar('DAGESH'), RT_Bitmap);
: if MyRes = 0 then
: ShowMessage('Error: resource doesn''t exist')
: else begin
: HebRes := TResourceStream.Create(HInstance,'DAGESH',RT_Bitmap);
: HebRes.Position := 0;
: {Next line gives ERROR: "Bitmap Image is not valid"}
: Image1.Picture.Bitmap.LoadFromStream(HebRes);
: ImageList1.AddMasked(Image1.Picture.Bitmap,clWhite);
: end;
: finally
: if HebRes <> nil then HebRes.Free;
: end;
: end;
:
: end.
:
:
:
Perhaps you could try to use the LoadResource() function, and then set the resulting handle to the bitmap in your image.
If that doesn't work, then all I can suggest is that you add a new hidden image to your form and load the bitmap into it. Then you can simply copy the bitmap from one image to the other. That will certainly work.
Report
Re: Loading Bitmap Resource Problem Posted by avishai on 2 Oct 2003 at 12:37 PM
Thanks for your help with this. I finally got it. I had thought it was difficult and then make it true. Simplicity is sometimes a surprise. Here's the code.

procedure TForm1.FormCreate(Sender: TObject);
begin
SpeedButton1.Glyph.LoadFromResourceName(HInstance,'DAGESH');
end;
Report
Re: Loading Bitmap Resource Problem Posted by avishai on 2 Oct 2003 at 8:10 AM
: I assume that you linked it into the source code using the $R directive.
: You can check if the resource exists in your app with the FindResource() function. Here is what the code should look like:
:
:    MyRes := FindResource(hInstance, PChar(MyResourceName), RT_Bitmap);
:    if MyRes = 0 then
:      ShowMessage('Error: resource doesn't exist')
:    else
:      // Use resource;
: 

: You can find more info on the function in the Windows SDK help files.
:

I just tried loading an ICON and it fails with or without the "FindResource" saying the "Resource does not exist". But when I look at the EXE in EXPLORER it's using the ICON that supposedly doesn't exist.





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.