This project uses the BitmapDecoder WinRT class to decode a GIF file into its individual frames. These frames can then be rendered into any device context (Form, PictureBox, etc) using vanilla GDI functions.
A TypeLib with the required WinRT interface definitions is included in the ZIP archive, don't forget to set a reference to it.
Here's a classic GIF showing Commander Data losing his temper over the once ubiquitous blue screen of death:
The project also supports more complex GIFs (such as those with partial frames, as well as frames with transparency). Here's how you would play such an animated GIF in a loop on a form, simple and to the point:
frmGIFPlayer - This is all it takes:
Code:Option Explicit Private BitmapDecoder As cBitmapDecoder, lCurrentFrame As Long, bAdjustSize As Boolean Private Sub Form_Load() Set BitmapDecoder = New cBitmapDecoder: AutoRedraw = True With BitmapDecoder .BackgroundBrush = CreateSolidBrush(GetBkColor(hDC)) If .Decode(App.Path & "\Travolta.gif") Then tmrPlayGIF = True End With End Sub Private Sub tmrPlayGIF_Timer() With BitmapDecoder If .DecoderReady Then If Not bAdjustSize Then Width = ScaleX(.ContainerWidth, vbPixels, ScaleMode) + (Width - ScaleWidth): Height = ScaleY(.ContainerHeight, vbPixels, ScaleMode) + (Height - ScaleHeight): bAdjustSize = True If lCurrentFrame < .FrameCount Then With .GetFrame(lCurrentFrame + 1) If .FrameReady Then .RenderFrame hDC: tmrPlayGIF.Interval = .NextFrameDelay lCurrentFrame = lCurrentFrame + 1: Set Picture = Image End If End With Else lCurrentFrame = 0: Set Picture = LoadPicture: tmrPlayGIF_Timer ' Play the GIF in a loop End If End If End With End Sub
Here is the demo project: GIFPlayer.zip (Updated!) - Download the GIFs above and place them in the project folder as they were too big to include in the attachment or provide your own GIFs for testing.
Requirements: Windows 10 ("Anniversary Update", July 2016) or later!![]()






Reply With Quote