简体   繁体   English

动画GIF图像引发异常

[英]Animating GIF image throws exception

I'm trying to animate a GIF image in XE7, but when I set the Animate property to True an exception is raised. 我正在尝试在XE7中为GIF图像设置动画,但是当我将Animate属性设置为True时,会引发异常。

在此处输入图片说明

Here's the .pas 这是.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 和.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 . 我已经安装了DevExpress控件,并且在属性编辑器中将Picture属性声明为TcxSmartImage However, this class descends from TGraphic, so I don't see how this could cause the exception. 但是,此类来自TGraphic,所以我看不到这怎么可能导致异常。

TGifImage(Spinner.Picture.Graphic).Animate := True;

The likely explanation is that Spinner.Picture.Graphic is not of type TGifImage . 可能的解释是Spinner.Picture.Graphic不是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: 如果解码显示在Picture.Data上下文中的十六进制,则会找到以下ASCII文本:

TdxSmartImage

This suggests to me that your image control does not contain TGifImage . 这向我暗示您的图像控件不包含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 感谢@SpeedFreak提供了有关此DevExpress支持项目的有用注释: https ://www.devexpress.com/Support/Center/Question/Details/Q562011

It seems that the DevExpress components register their own handler for the GIF extension. 似乎DevExpress组件为GIF扩展注册了自己的处理程序。 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. 因此,我想解决此问题的最简单方法是在运行时从资源加载GIF图像,这避免了依赖于将图像扩展名与图形组件相关联的所有设计时魔术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM