Search code examples
delphifiremonkey

Loading Bitmap From Resource


I find a solution for Loading Bitmap From Resource file on How to store images in FireMonkey? And I've tried to use it in my Firemonkey application, but it can't find Resource and raises an error "EresNotFound". My Resource .RC File Is Like This

Bitmap_1    BITMAP    "Test.bmp"

and my Code is

procedure Tform1.load_image_from_resource(var Im1: Timage; res_name: String);
var InStream: TResourceStream;
begin
  InStream := TResourceStream.Create(HInstance, res_name,RC_RTDATA);
  try
    Im1.Picture.Bitmap.LoadFromStream(InStream);
  finally
    InStream.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Load_image_from_resource(Image1,'Bitmap_1');
end;

I also found a solution on the question Delphi 2010: unable to find resource - EResNotFound. But it still doesn't find recource


Solution

  • There are several issues in your code, you need to declare resource as RCDATA

    Bitmap_1    RCDATA    "Test.bmp"
    

    Also looks like you created VCL application and there is a typo in resource type name, it should be RT_RCDATA, working FireMonkey code looks like this

    procedure Tform1.load_image_from_resource(var Im1: Timage; res_name: String);
    var InStream: TResourceStream;
    begin
      InStream := TResourceStream.Create(HInstance, res_name, RT_RCDATA);
      try
        Im1.Bitmap.LoadFromStream(InStream);
      finally
        InStream.Free;
      end;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      Load_image_from_resource(Image1, 'Bitmap_1');
    end;