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.
I just added a resource named SampleBitmap
(a bitmap image) to a new VCL project. Then I added a TImage
control, and gave it an OnClick
handler:
procedure TForm1.Image1Click(Sender: TObject);
begin
Image1.Picture.Bitmap.LoadFromResourceName(HInstance, 'SampleBitmap');
end;
It works perfectly.
Update
The problem is most likely that you are using a JPG image, and not a Bitmap. You cannot load a JPG image into a TBitmap
. So, what to do? Well, add JPEG
to your uses
clause, and do
procedure TForm5.Image1Click(Sender: TObject);
var
RS: TResourceStream;
JPGImage: TJPEGImage;
begin
JPGImage := TJPEGImage.Create;
try
RS := TResourceStream.Create(hInstance, 'JpgImage', RT_RCDATA);
try
JPGImage.LoadFromStream(RS);
Image1.Picture.Graphic := JPGImage;
finally
RS.Free;
end;
finally
JPGImage.Free;
end;
end;