Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Loading Bitmap Resource Problem

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.

Comments

  • zibadianzibadian Posts: 6,349Member
    : 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.
  • avishaiavishai Posts: 10Member
    : 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.


  • zibadianzibadian Posts: 6,349Member
    : : 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:
    [code]
    MyRes := FindResource(hInstance, PChar(MyResourceName), RT_Bitmap);
    if MyRes = 0 then
    ShowMessage('Error: resource doesn't exist')
    else
    // Use resource;
    [/code]
    You can find more info on the function in the Windows SDK help files.
  • avishaiavishai Posts: 10Member
    : 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:
    : [code]
    : MyRes := FindResource(hInstance, PChar(MyResourceName), RT_Bitmap);
    : if MyRes = 0 then
    : ShowMessage('Error: resource doesn't exist')
    : else
    : // Use resource;
    : [/code]
    : 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.


  • zibadianzibadian Posts: 6,349Member
    : : 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:
    : : [code]
    : : MyRes := FindResource(hInstance, PChar(MyResourceName), RT_Bitmap);
    : : if MyRes = 0 then
    : : ShowMessage('Error: resource doesn't exist')
    : : else
    : : // Use resource;
    : : [/code]
    : : 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.
  • avishaiavishai Posts: 10Member
    : 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:
    : [code]
    : MyRes := FindResource(hInstance, PChar(MyResourceName), RT_Bitmap);
    : if MyRes = 0 then
    : ShowMessage('Error: resource doesn't exist')
    : else
    : // Use resource;
    : [/code]
    : 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.


  • avishaiavishai Posts: 10Member
    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;
Sign In or Register to comment.