Gif image RRS feed

  • Question

  • i will use visual studio 2017 my picture box will now play a gif image .it will simple show the picture only

    my code

    PictureBox5.Image = Image.FromFile(File path)

    Wednesday, December 13, 2017 6:30 AM

All replies

  • i will use visual studio 2017 my picture box will now play a gif image .it will simple show the picture only

    There is an example here of how to animate a GIF image in a picture box:

    The alternatives is to use a different control that can do animated GIF.

    Wednesday, December 13, 2017 8:22 AM
  • Seems that PictureBox should be able to animate such images. Try, for example, some on-line GIF files:

       PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

    Maybe you should simply normalize your file.

    See also the ImageAnimator class:

    Wednesday, December 13, 2017 8:48 AM
  • Yes, but why don't you simple try?


    Wednesday, December 13, 2017 9:35 AM
  •  This is a common problem that i have seen asked about many times.  Some animated gif's will animate and some wont.  It seems to depend on the gif in some cases and in others it skips frames when there are other messages being processed in the window.

     MS seems to have made the ImageAnimator class that Viorel mentiioned which kind of works but,  if i remember right,  it does not animate the frames at their correct frame delays unless the images all have the same delay times.  If the frames all have the same delay times which many gif's don't,  it will work fairly decent.

     If the frames don't have the same delay times,  i recommend stripping each frame and it's delay from the animated gif file.  Then play them back using a Timer.  Although Timers are not really accurate,  it has been the best and most reliable method i have found to date to have full control of animating gif files.

     Doing it this way gives you full control of the animation for things like... starting/stopping the animation,  playing frames in forward or reverse order,  changing/editing the frame images,  changing the frame delays,  or even saving each of the images if you wanted to.

     The WebBrowser control and ImageAnimator class will not give you all that functionality.

     Below is an example of doing this.  You can try it in a new form project with 2 Buttons, 1 PictureBox,  and 1 Timer added to the form.  Name one button as "Button_OpenGif" and the other as "Button_StartStop".  Then use the code below.

    Imports System.Drawing.Imaging
    Public Class Form1
        Private GifFrames As New List(Of GifFrame)
        Private PlayIndex As Integer = 0
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Interval = 10
        End Sub
        Private Sub Button_OpenGif_Click(sender As Object, e As EventArgs) Handles Button_OpenGif.Click
            Using ofd As New OpenFileDialog
                ofd.Filter = "Gif Images|*.gif"
                If ofd.ShowDialog = DialogResult.OK Then
                    If GifFrames.Count > 0 Then
                        If GifFrames(0).Frame.Width > PictureBox1.ClientSize.Width OrElse GifFrames(0).Frame.Height > PictureBox1.ClientSize.Height Then
                            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
                            PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
                        End If
                    End If
                End If
            End Using
        End Sub
        Private Sub StripGifFrames(fName As String)
            Const PIID_FRAMEDELAY As Integer = &H5100
            Using bm As New Bitmap(fName)
                Dim fd As New FrameDimension(bm.FrameDimensionsList(0))
                Dim framecount As Integer = bm.GetFrameCount(fd)
                If framecount > 1 Then
                    Dim fdv() As Byte = bm.GetPropertyItem(PIID_FRAMEDELAY).Value
                    For i As Integer = 0 To framecount - 1
                        Dim framedelay As Integer = (BitConverter.ToInt32(fdv, 4 * i)) * 10
                        If framedelay < 10 Then framedelay = 90 'rarely some gifs use 0 for all the delays, this sets them to animate at a default delay
                            bm.SelectActiveFrame(fd, i)
                            GifFrames.Add(New GifFrame(bm, framedelay))
                        Catch ex As Exception
                            MessageBox.Show("An Unexpected Error Occurred Stripping Frame (" & i.ToString & "}", "Error...", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            Exit For
                        End Try
                    MessageBox.Show("The selected gif image only has 1 frame and can not be animated.", "Error...", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
                fd = Nothing
            End Using
        End Sub
        Private Sub ClearGifFrames()
            PictureBox1.Image = Nothing
            For i As Integer = GifFrames.Count - 1 To 0 Step -1
            PlayIndex = 0
            Timer1.Interval = 10
        End Sub
        Private Sub Button_StartStop_Click(sender As Object, e As EventArgs) Handles Button_StartStop.Click
            Timer1.Enabled = Not Timer1.Enabled
        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Timer1.Interval = GifFrames(PlayIndex).Delay
            PictureBox1.Image = GifFrames(PlayIndex).Frame
            PlayIndex += 1
            If PlayIndex = GifFrames.Count Then PlayIndex = 0
        End Sub
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        End Sub
    End Class
    Public Class GifFrame
        Public Property Frame As Bitmap
        Public Property Delay As Integer
        Public Sub New(frm As Bitmap, dly As Integer)
            Me.Frame = New Bitmap(frm)
            Me.Delay = dly
        End Sub
    End Class


     Here is an example of what my example form is set up like...

    If you say it can`t be done then i`ll try it

    Wednesday, December 13, 2017 1:03 PM
  • Another simple way is to use a WebBrowser =>

    Public Class Form1
        Private WithEvents webBrowser1 As WebBrowser
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Size = New Size(330, 330)
            webBrowser1 = New WebBrowser()
            With webBrowser1
                .Size = New Drawing.Size(320, 320)
                .Location = New Drawing.Point(2, 2)
            End With
            webBrowser1.ScrollBarsEnabled = False
            webBrowser1.Navigate(New Uri(""))
        End Sub
    End Class

    Wednesday, December 13, 2017 1:46 PM
  • Hello,

    I try to load gif image in picturebox control, and it works fine, but you said that it just simply shows picture only, I think it may be the reason for gif, you could  try to manually animate and draw the frames you could use the ImageAnimator-class.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, December 14, 2017 6:19 AM