簡體   English   中英

從資源加載 Gif 圖片

[英]Loading Gif Picture From Resources

我想將動畫 .Gif 圖片從 .Res 文件加載到 TImage 中,但 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 圖片時該怎么辦?

  1. 包括gifimg單元在至少一種uses在你的程序條款。
  2. 將映像作為資源鏈接到可執行文件。
  3. 將資源加載到TResourceStream對象中。
  4. 創建一個TGifImage對象,並通過調用LoadFromStream()將圖像從第3步傳遞到其中,並將圖像加載到其中。
  5. TGifImage對象分配給TImage.Picture屬性。

如果此問題與缺少的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 點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM