【问题标题】:Loading Gif Picture From Resources从资源加载 Gif 图片
【发布时间】:2021-05-31 17:20:09
【问题描述】:

我想将动画 .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 图片时该怎么办?

【问题讨论】:

    标签: delphi


    【解决方案1】:
    1. 在程序的至少一个uses 子句中包含gifimg 单元。
    2. 将图像作为资源链接到您的可执行文件。
    3. 将资源加载到TResourceStream 对象中。
    4. 创建一个TGifImage 对象并通过调用LoadFromStream() 将图像加载到其中,并传递来自步骤3 的流。
    5. TGifImage 对象分配给TImage.Picture 属性。

    【讨论】:

    • @TLama 谢谢。为什么不直接编辑答案?请随意这样做。
    • 感谢您的回复我尝试这样做,但它会引发运行时错误
    • @TLama 在我的宽限期内随时进行编辑。我会很高兴地欢迎他们。谢谢。
    • @Fayyaz 对我来说很好用。猜猜你的代码有问题。
    • 感谢您的回复我尝试这样做,但它会引发运行时错误它找不到资源名称我将此行添加到我的代码'RS:=TResourceStream.Create(HInstance,' TL', RT_RCDATA);'但是在运行时我收到一个错误“找不到资源 TL”请给我写一个简单的示例代码吗?
    【解决方案2】:

    如果这个问题是关于缺少的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 点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多