gpt4 book ai didi

image - 从嵌入资源加载图像

转载 作者:行者123 更新时间:2023-12-03 14:39:31 26 4
gpt4 key购买 nike

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

因为我无法设置从资源加载的属性。所以我需要在运行时加载。

我有代码

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;

但它只是加载带有空白图像的表单。还有:

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

我不知道底部的那个是否能工作,我猜不会。只是我尝试过的。

Resource and Image

最佳答案

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

procedure TForm1.Image1Click(Sender: TObject);
begin
Image1.Picture.Bitmap.LoadFromResourceName(HInstance, 'SampleBitmap');
end;

它工作得很好。

更新

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

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;

关于image - 从嵌入资源加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5805372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com