gif vb.net

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.

Gif VB.NET


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 vb.net 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 vb.net 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

Private
_nFrames As Integer
Private
_currentFrame As Integer
Private
_fileName As String
Private
_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 =
True
End Sub

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

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

Public Property
currentFrame() As Integer
Get
Return
_currentFrame
End Get
Set
(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
If
_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

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

Public Sub
Dispose() Implements IDisposable.Dispose
Dispose(
True)
GC.SuppressFinalize(
Me)
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 vb.net 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 vb.net.

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
Gif.OpenFile(
"C:ImagesGif1.gif")
Me.DoubleBuffered = True
End Sub

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

Private Sub
Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawImage(Gif.GifBitmap,
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.
06/03/2019

Grade 
03/07/2019

Muy bien

Esto no existia en español! genial.

    Write your review!

    Write a review

    gif vb.net

    gif vb.net

    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.

    (optional)
    *(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