Rectangle 27 0

delphi Load image from embedded resource?


procedure TForm1.Image1Click(Sender: TObject);
begin
  Image1.Picture.Bitmap.LoadFromResourceName(HInstance, 'SampleBitmap');
end;
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;

+1 for the JPG catch and code.

@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.

@Uwe: Oh, the bug in finally was a really cool one! Thanks for correcting it!

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:

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.

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

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.

Note