从嵌入式资源加载图像

从嵌入式资源加载图像

本文介绍了从嵌入式资源加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在运行时分配图像(Image1)图片。

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.

我有代码

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.

推荐答案

我刚刚将名为 SampleBitmap (位图图像​​)的资源添加到新的VCL中项目。然后我添加了 TImage 控件,并为其提供了 OnClick 处理程序:

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;

它非常好用。

更新

问题很可能是您使用的是JPG图像,而不是位图。您不能将JPG图像加载到 TBitmap 中。那么该怎么办?好吧,将 JPEG 添加到您的 uses 子句中,然后执行

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;

这篇关于从嵌入式资源加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 15:15