Reputation: 6053
I'm trying to animate a GIF image in XE7, but when I set the Animate property to True an exception is raised.
Here's the .pas
unit Unit6;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
GifImg, Vcl.ExtCtrls;
type
TForm6 = class(TForm)
Spinner: TImage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form6: TForm6;
implementation
{$R *.dfm}
procedure TForm6.Button1Click(Sender: TObject);
begin
TGifImage(Spinner.Picture.Graphic).Animate := True;
end;
end.
And the .dfm
object Form6: TForm6
Left = 0
Top = 0
BorderStyle = bsDialog
Caption = 'GIF Test'
ClientHeight = 83
ClientWidth = 115
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
PixelsPerInch = 96
TextHeight = 13
object Spinner: TImage
Left = 45
Top = 13
Width = 24
Height = 24
AutoSize = True
IncrementalDisplay = True
ParentShowHint = False
Picture.Data = {
0D546478536D617274496D61676547494638396118001800840000FFFFFF0000
00CECECEFAFAFAE0E0E0B0B0B0E8E8E88E8E8EC8C8C89C9C9CD8D8D8A8A8A8C0
C0C0F2F2F2767676868686B8B8B8686868000000000000000000000000000000
00000000000000000000000000000000000000000000000000000021FE1A4372
6561746564207769746820616A61786C6F61642E696E666F0021FF0B4E455453
43415045322E30030100000021F90401070000002C00000000180018000008B3
0001081C48100004080513262440F0C10385100110708050608408031B308C28
F081838D170712D8C8118283041D1F02D0D8002249001E5B1E1438D2A5430103
17B4244852E34B010E51725C397267C6050F16701CF932A1D1A150A30A14B020
4182053887FA24D0A081000856B142EDCAF4A9D4B368053680203622D3850B16
4030BBB0E8D10524053CD598B1A6DAA75F5BFAE54A902E4D085907131E7A70A7
80AC129B3A159017324BAD041FA72DD8756840003B}
ShowHint = True
Transparent = True
end
object Button1: TButton
Left = 13
Top = 49
Width = 91
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
end
I have the DevExpress controls installed and the Picture property in the properties editor is declared as a TcxSmartImage
. However, this class descends from TGraphic, so I don't see how this could cause the exception.
Upvotes: 2
Views: 1048
Reputation: 613392
TGifImage(Spinner.Picture.Graphic).Animate := True;
The likely explanation is that Spinner.Picture.Graphic
is not of type TGifImage
. I suspect that if you use a runtime checked cast then you will discover that is the case.
(Spinner.Picture.Graphic as TGifImage).Animate := True;
If you decode the hex contained in the Picture.Data
context that you showed you find this ASCII text:
TdxSmartImage
This suggests to me that your image control does not contain TGifImage
.
You should as a general rule. avoid using unchecked casts. If you make a mistake and cast to an invalid type then the outcome is unpredictable with an unchecked cast. The best case scenario is that you get a runtime error and at least realise that there is a problem. However, the runtime error is invariably somewhat cryptic. The worst case scenario with an erroneous unchecked cast is that there is no runtime error and the code appears to work.
On the other hand, an invalid type when used with a runtime checked cast will lead to an informative error message.
Thanks to @SpeedFreak for the useful comment which refers to this DevExpress support item: https://www.devexpress.com/Support/Center/Question/Details/Q562011
It seems that the DevExpress components register their own handler for the GIF extension. And that handler does not support animation.
So I guess that the easiest way to resolve this is to load the GIF image at runtime, from a resource, and this avoid relying on all the design time magic that associates image extensions with graphic components.
Upvotes: 4