Fayyaz
Fayyaz

Reputation: 219

Loading Gif Picture From Resources

I want to load animated .Gif Picture into TImage From .Res File but TImage prepare LoadFromResourceName() function only for MyImage.Picture .Bitmap. I Write a Simple Code as Follow

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;

It Work Properly. Now, what should I do when I want to load .Gif Picture from .Res File?

Upvotes: 1

Views: 1451

Answers (2)

yonojoy
yonojoy

Reputation: 5566

If this question is about the missing LoadFromResourceName() procedure, use this class helper (add this unit) to add the missing procedure for 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.

This addresses points 3 and 4 from @Davids answer.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 613442

  1. Include the gifimg unit in at least one uses clause in your program.
  2. Link the image to your executable as a resource.
  3. Load the resource into a TResourceStream object.
  4. Create a TGifImage object and load the image into it by calling LoadFromStream(), passing the stream from step 3.
  5. Assign the TGifImage object to the TImage.Picture property.

Upvotes: 3

Related Questions