Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all.
I have a question about how to play animation without to use too much memory.
I want to load in every Timer Tick only one frame directly from the disk, without to load all frames into memory in a variable.
I have make one function for this, but if i called it slows down in every frame a bit more.
If the gif has lot of frames at the end the animation looks like Replay. The time interval plays no role.

The first function play the animation and uses a variable.
When i use this function below, the animation plays without any problem, but uses to much memory.

The second function is Selective.
It takes only one frame directly from the disk and not all, must be called in every timer tick. See below.
In this function the animation plays but in every frame a bit slower.
I suppose the system.Drawing.imaging read the gif every time from the beginning of the gif until the indexed frame,
and so needs more time for the next frame.

I have posted this question also in Microsoft Developer Network in the follow Link,

https://social.msdn.microsoft.com/Forums/vstudio/en-US/2b0e4b02-fd85-4f89-aedd-e7af91d32dfb/gif-animation-slows-down?forum=vbgeneral#2b0e4b02-fd85-4f89-aedd-e7af91d32dfb

but no luck.

Please any suggestion will be Appreciated.
Also this article is my first one. Please be patient. :)

What I have tried:

Private Function GetAllinMemory(ByVal GifImg As Image) As Frame()
    'RETURNS ALL FRAMES AND THE DURATION IN VARIABLE
    Dim gif As Image = GifImg ' 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
    Else
        frames(0) = New Frame(0, New Bitmap(gif))
    End If
    Return frames
End Function


Private Function GetGifFremeFromDisk(ByVal File As String, ByVal Index As Integer) As Frame
    FrameInterval = 1
    If System.IO.File.Exists(File) = False Then
        Return Nothing
    End If

    Dim gif As Image = Image.FromFile(File)
    Dim fd As New Imaging.FrameDimension(gif.FrameDimensionsList()(0))
    Dim frameCounter As Integer = gif.GetFrameCount(fd)
    frameCount = frameCounter
    Dim OneFrame As Frame
    If Index >= frameCount Then Index = frameCount
    If frameCount > 1 Then
        Dim times() As Byte = gif.GetPropertyItem(&H5100).Value
        gif.SelectActiveFrame(fd, Index)
        Dim length As Integer = BitConverter.ToInt32(times, 4 * Index) * 10
        OneFrame = New Frame(length, New Bitmap(gif))
        FrameInterval = length
    Else
        OneFrame = New Frame(FrameInterval, New Bitmap(gif))
    End If
    gif.Dispose()
    Return OneFrame
End Function
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900