delphianimationgifgraphics32

GIF animation TImage/Timage32


The goal is to have animated GIFs playing inside image components with good image quality even after resizing one of the image components.

TImage example

This is the resized TImage, very bad image quality but flawless animation:

timage example

// GIF = TGIFImage
// TImage = TImage
GIF.Animate := True;
TImage.Stretch := True;
TImage.Proportional := True;
TImage.Picture.Assign(GIF);

TImage32 example

This is a resized TImage32 from the graphics32 library, very good image quality but no animation at all, only the first frame ist visible:

graphics32 example

// GIF = TGIFImage
// TImage32 = TImage32
GIF.Animate := True;
TImage32.ScaleMode := smResize;
TImage32.BitmapAlign := baCenter;
TImage32.Bitmap.Assign(GIF);

I need to have the TImage32 component play the animation or for the TImage component to have better resampling.


Solution

  • Graphics32 is not meant to display animations (like in a GIF) out of the box. Furthermore it does not contain a native GIF decoder. The code you show relies on the internal TPicture decoder from Delphi. A conversion to TImage32 with assign will only copy the first frame and thus it will result in a still image.

    In order to display animations you need further code. As mentioned in the comments it would make sense to first copy the each frame of the GIF to a TBitmap32 instance (TImage32 has too much overhead). Than you need to perform the animation. If possible this should relate to the display refresh rate or take at least the time interval since the last drawing into account.

    If for example your monitor uses 60fps and your gif contains 30 frames per second than you need to display each frame for two refresh cycles. Though, this would be an easy situation.

    Things will get slightly more complicated if your gif contains 29 frames per second (for example). You need to develop an algorithm to pick the right frame for the current time.

    So far I have not seen any implementation of the above for Graphics32, but it's not that complicated once you know what to do.