Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load image from embedded resource

I am trying to assign an image(Image1) a picture at Run-time.

Since I can't set a property to load from resource. So I need to load at run time.

I have the code

procedure TForm1.FormCreate(Sender: TObject); 
var RS:Tresourcestream ; 
begin 
RS := TResourceStream.Create(HInstance,'Splashscreen_Background', RT_RCDATA);   
image1.Picture.Bitmap.LoadFromResourcename(HInstance,'splashscreen_background'); 
end;

But it just loads the forms with a blank Image. aswell as:

procedure TForm1.FormCreate(Sender: TObject);
BitMap1 : TBitMap;
begin
BitMap1 := TBitMap.Create;
BitMap1.LoadFromResourceName(HInstance,'Live');
image1.Picture.Bitmap.Assign(Bitmap1);
end;

I have no idea if the bottom one would work at all, guess not. Just something I tried.

Resource and Image

like image 645
Skeela87 Avatar asked Apr 27 '11 14:04

Skeela87


People also ask

How do I add a resource image in Visual Studio?

In Visual Studio, open a SharePoint solution. In Solution Explorer, choose a SharePoint project node, and then, on the menu bar, choose Project > Add New Item. In the Add New Item dialog box, choose the Global Resources File template, and then choose the Add button.

What is embedded resource?

Embedded files are called as Embedded Resources and these files can be accessed at runtime using the Assembly class of the System. Reflection namespace. Any file within the project can be made into an embedded file.

How do I add an image to a resource in Visual Studio 2019?

In Visual Studio, click the Project menu, and select Add Existing Item. Find and select the image you want to add to your project.


1 Answers

Load it directly into a TBitmap instead, like you tried:

// Create your resource like this:
// MyResource.rc
SPLASHBKGND BITMAP YourSplashscreen.bmp

Compile it:

C:\YourResFolder\Brcc32 MyResource.rc MyResource.res

or in later versions of Delphi:

{$R MyResource.res MyResource.rc}

Use it:

procedure TForm1.FormCreate(Sender: TObject);
var
  Bmp: TBitmap;
begin
  Bmp := TBitmap.Create;
  try
    Bmp.LoadFromResourceName(HInstance, 'SPLASHBKGND');
    Image1.Picture.Assign(Bmp);
  finally
    Bmp.Free;
  end;
end;
like image 54
Ken White Avatar answered Oct 27 '22 09:10

Ken White