New product

How to handle, create, paint and play animated gif in VB.NET. Get the animation speed of the gif, with a simple class to learn.


The gif images are widely used because they offer especially in Net framework of self-management and broad support.

We can place a gif and play directly in a form to use it as background of the form or in a picturebox to signal some wait or work in the background and automatically, NET will do the animation work without writing a single line of code.

But what happens if we want to manipulate a gif manually?

Both and c # offer support for gif in the Bitmap, Imaging.FrameDimension classes.

Next, I show you a simple class that loads and manages a gif:

Public Class ImageGif
Implements IDisposable

Private _Gif As Bitmap
Private _frameD As Imaging.FrameDimension
Private times() As Byte

_nFrames As Integer
_currentFrame As Integer
_fileName As String
_runGif As Boolean

Public Event
UpdateGif(ByVal bmpGif As Bitmap)

Private WithEvents AutoUpdater As Timer

Public Sub OpenFile(ByVal filename As String, Optional ByVal autoUpdate As Boolean = True)
'Load the bitmap
_Gif = New Bitmap(filename)

_frameD =
New Imaging.FrameDimension(_Gif.FrameDimensionsList(0))

_nFrames = _Gif.GetFrameCount(_frameD)

times = _Gif.GetPropertyItem(&H5100).Value

_runGif = autoUpdate

AutoUpdater =
New Timer
AutoUpdater.Interval = 33
AutoUpdater.Enabled =
End Sub

Public ReadOnly Property
GifBitmap() As Bitmap
End Get
End Property

Public ReadOnly Property
nFrames() As Integer
End Get
End Property

Public Property
currentFrame() As Integer
End Get
(ByVal value As Integer)
If value >= _nFrames Then value = 0
If value < 0 Then value = 0
_currentFrame = value
'update bitmap gif
_Gif.SelectActiveFrame(_frameD, _currentFrame)
End Set
End Property

Private Sub
UpdateFrameGif() Handles AutoUpdater.Tick
Dim timeDelay As Integer
_runGif Then
_currentFrame += 1
If _currentFrame >= _nFrames Then _currentFrame = 0
_Gif.SelectActiveFrame(_frameD, _currentFrame)
timeDelay = BitConverter.ToInt32(times, 4 * _currentFrame) * 10
RaiseEvent UpdateGif(_Gif)
AutoUpdater.Interval = timeDelay
End If
End Sub

" IDisposable Support "
Private disposedValue As Boolean = False
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
disposing Then
AutoUpdater.Enabled = False
End If
End If
.disposedValue = True
End Sub

Public Sub
Dispose() Implements IDisposable.Dispose
End Sub
#End Region

End Class

Implement the ImageGif class

It is quite simple to use with very few elements:

Upload the image gif e a form in would be:

1.- We declare the gif container,

2.- We load the image in the container. So that the flicker is not visible, we enable the doublebuffer for the Paint event.

3.- We intercept the updateGif event to know that the gif has changed the frame and invalidate the form to be painted.

4.- Paint the gif on the form with the Paint event.

That's it, with this we'll have an animated gif on

The code is shown below:

Private WithEvents Gif As ImageGif

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Gif =
New ImageGif
Me.DoubleBuffered = True
End Sub

Private Sub
Gif_UpdateGif(ByVal bmpGif As System.Drawing.Bitmap) Handles Gif.UpdateGif
End Sub

Private Sub
Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
New Point(0, 0))
End Sub

Well, I will continue to improve this class to make a tool to extract gif.

If you have questions, below in the question form. :)

Written by:
Elimar G.


Muy bien

Esto no existia en español! genial.

    Write your review!

    Write a review



    How to handle, create, paint and play animated gif in VB.NET. Get the animation speed of the gif, with a simple class to learn.

    Ask a question

    NO registration required!

    If the question you have has not yet been answered here, use the form below to ask something about this addon.

    *(Required to be notified when an answer is available)

    If the download link redirects to another product that is not described in the article or is broken, report it using our Reporting Form