13

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

2 Answers 2

29

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;
Sign up to request clarification or add additional context in comments.

5 Comments

Interesting, nothing happens onload, but onclick it does. However the onclick event (same as yours, I did change the resource name) returns an error saying it can't find the resource.
@Skeela87: Well, the above example is made to execute onclick, not onload. I just gave you the code to load the image. It is up to you to place the code at the right place in your code. Further, SampleBitmap and JpgImage are the names of the resources in my sample. In your case, the resource is called Splashscreen_Background instead.
Yeh, I've got that, I made my comment on your original post. But that works fine, TYVM. I did load a JPG, I hadn't realised that, I'm more of a VB person myself.
@Uwe: Oh, the bug in finally was a really cool one! Thanks for correcting it!
In maXbox set direct the JPEG or JPG token: aname:= 'MAXBOX4LOGO'; RS := TResourceStream.Create(hInstance, aname, 'JPEG');
4

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;

5 Comments

I don't think there is any problem with my much briefer approach.
@Andreas: Didn't say there was. :) My answer was on the way when yours appeared; I just went ahead and posted an alternative.
@Ken: I am pretty sure I have found the issue. The OP tries to load a JPG image into a TBitmap. That cannot work...
@Ken If the resource is linked into the library or package that this code resides, then MainInstance is wrong. The advice should be use MainInstance or HInstance, whichever refers to the module with the resource. I only ever use HInstance because I only ever load resources from the same module that they are linked to.
Granted I do prefer Andreas briefer approach, TYVM for the reply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.