Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display animated gif in datagridview

Status
Not open for further replies.

lawlegacy

IS-IT--Management
Jun 5, 2006
53
US
I created a datagridview with an image column. When I add an animated gif to that column the picture is static and does not display the animation. Does anyone know how to make the gif animate?
 
Use the ImageAnimator class:
Code:
Public Class Form1

    Dim img As Image = Image.FromFile("c:\ani.gif")

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim ic As New DataGridViewImageColumn

        ic.Width = img.Width

        dg.Columns.Add(ic)

        AddHandler dg.CellPainting, AddressOf dg_CellPainting

        ImageAnimator.Animate(img, New EventHandler(AddressOf OnFrameChanged))

    End Sub

    Private Sub OnFrameChanged(ByVal sender As Object, ByVal e As EventArgs)

        dg.Invalidate()

    End Sub

    Private Sub dg_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs)

        If e.RowIndex >= 0 AndAlso e.ColumnIndex = 0 Then

            e.Paint(e.CellBounds, DataGridViewPaintParts.Background)

            ImageAnimator.UpdateFrames()

            e.Graphics.DrawImage(img, e.CellBounds.Location)

            e.Handled = True

        End If

    End Sub

End Class
You might find that the grid flickers if you have a lot of rows visible or a lot of gif columns.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top