我想将动画.Gif图片加载到TImage From .Res文件,但TImage仅为MyImage.Picture .Bitmap准备LoadFromResourceName()函数。 我按照
编写一个简单的代码procedure TForm2.FormCreate(Sender: TObject);
Var
MyImage:TImage;
begin
MyImage:=TImage.Create(Self);
MyImage.Parent:=Self;
MyImage.AutoSize:=True;
MyImage.Picture.LoadFromFile('H:\Component\Automation\Test\Animation\TL.Gif');
MyImage.Top:=0;
MyImage.Left:=0;
MyImage.Visible:=True;
MyImage.Transparent:=True;
( MyImage.Picture.Graphic as TGIFImage ).Animate := True;
( MyImage.Picture.Graphic as TGIFImage ).AnimationSpeed:= 100;
end;
工作正常。现在,当我想从.Res文件中加载.Gif图片时,我该怎么办?
答案 0 :(得分:2)
gifimg
子句中包含uses
单元。 TResourceStream
对象中。 TGifImage
对象,并通过调用LoadFromStream()
将图像加载到其中,并从第3步传递流。TGifImage
对象分配给TImage.Picture
属性。答案 1 :(得分:0)
如果这个问题是关于缺少的 LoadFromResourceName()
过程,使用这个类助手(添加这个单元)来添加 TGifImage
缺少的过程:
unit Mv.VCL.Helper.Imaging;
interface
uses
VCL.Imaging.GifImg;
type
TGifImageHelper = class helper for TGifImage
procedure LoadFromResourceName(AInstance: HInst; const AName: string);
end;
implementation
uses
System.SysUtils,
System.Classes,
WinApi.Windows; //RT_RCDATA
procedure TGifImageHelper.LoadFromResourceName(AInstance: HInst; const AName: string);
var
ResStream: TResourceStream;
begin
try
ResStream := TResourceStream.Create(AInstance, AName, RT_RCDATA);
try
LoadFromStream(ResStream);
finally
ResStream.Free;
end;
except on E: Exception do
begin
E.Message := 'Error while loading GIF image from resource: ' + E.Message;
raise;
end;
end;
end;
end.
这解决了@Davids 回答中的第 3 点和第 4 点。