Cyclone
Cyclone

Reputation: 18295

GIF manipulation in vb.net

Is it possible, without an external code library, to separate a .gif file into its individual frames, and conversely, to assemble a group of images into one?

I have spent a while playing with the System.Drawing namespace, and cannot seem to figure out how.

Also, I cannot figure out what to pass as a parameter for GetFrameCount. I do not know what they mean by a "dimension", and apparently it cannot be left null.

Additionally, is it possible to have playback control over the gif without first splitting it and recompiling/displaying in a way that mimics playback?

Thanks for the help.

This is in vb.net, 2008.

EDIT: If it is not possible without an external library, what is the best one to use?

Upvotes: 3

Views: 6961

Answers (2)

Tom
Tom

Reputation: 3374

I used code similar to this and it worked just fine... I didn't invent it, so credit to the person who did (I've forgotten)... (maybe here).

Private Class Frame
    Public Property MilliSecondDuration As Integer
    Public Property Bitmap As Image
    Public Sub New(ByVal duration As Integer, ByVal img As Bitmap)
        Me.MilliSecondDuration = duration
        Me.Bitmap = img
    End Sub
End Class

Private Function SeparateGif() As Frame()

    Dim gif As Image = Image.FromFile("MyGif.gif")

    Dim fd As New Imaging.FrameDimension(gif.FrameDimensionsList()(0))
    Dim frameCount As Integer = gif.GetFrameCount(fd)
    Dim frames(frameCount) As Frame

    If frameCount > 1 Then
        Dim times() As Byte = gif.GetPropertyItem(&H5100).Value
        For i As Integer = 0 To frameCount - 1
            gif.SelectActiveFrame(fd, i)
            Dim length As Integer = BitConverter.ToInt32(times, 4 * i) * 10
            frames(i) = New Frame(length, New Bitmap(gif))
        Next
    End If

    Return frames

End Function

Upvotes: 4

Jed Smith
Jed Smith

Reputation: 15934

According to this forum post saving the animated GIF is not supported in .NET. So no, without an external code library, it's likely not possible.

Any particular reason you're averse to external libs?

Upvotes: 0

Related Questions