CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2009

    how read gif files?

    heres the function for read animated gif's:
    Public Function LoadGif(sFile As String, ByRef aImg() As StdPicture, ByRef imageHDC() As Long, ByRef picdelay() As Long) As Long    Dim hFile As Long
        Dim sImgHeader As String
        Dim sFileHeader As String
        Dim sBuff As String
        Dim sPicsBuff As String
        Dim nImgCount As Long
        Dim i As Long
        Dim j As Long
        Dim xOff As Long
        Dim yOff As Long
        Dim TimeWait As Long
        Dim sGifMagic As String
        Dim RepeatTimes As Long
        Dim s As Long
        For s = 1 To FrameCount
            Set aImg(s) = Nothing
        Next s
        'Erase imageHDC
        'Erase aImg
        'Erase picdelay
        'magic string signifying end of
        'header and end of a gif frame
        sGifMagic = Chr$(0) & Chr$(33) & Chr$(249)
        'load the gif into a string buffer
        hFile = FreeFile
        Open sFile For Binary Access Read As hFile
        sBuff = String(LOF(hFile), Chr(0))
        Get #hFile, , sBuff
        Close #hFile
        'begin process of splitting the loaded
        'string into individual gif images.
        'Each image will be assigned to its own Image control in a control array. The
        'First, separate the header information from
        'the image info.
        i = 1
        nImgCount = 0
        j = InStr(1, sBuff, sGifMagic) + 1
        sFileHeader = Left(sBuff, j)
        'A gif's first characters are "GIF"
        'followed by the gif type, ie "GIF89am",
        'so if this missing, its not a gif.
        If Left$(sFileHeader, 3) <> "GIF" Then
            MsgBox "This file is not a *.gif file", vbCritical
            Exit Function
        End If
        'set pointer ahead 2 bytes from the
        'end of the gif magic number
        i = j + 2
        'if the fileheader size was greater than
        '127, the info on how many individual
        'frames the gif has is located within the header.
        If Len(sFileHeader) >= 127 Then
            RepeatTimes = Asc(Mid(sFileHeader, 126, 1)) + (Asc(Mid(sFileHeader, 127, 1)) * 256&)
            RepeatTimes = 0
        End If
        'create a temporary file in the current directory
        hFile = FreeFile
        Open "temp.gif" For Binary As hFile
        'split out each frame of the gif, and
        'write each the frame to the temporary file.
        'Then load an image control for the frame,
        'and load the temp file into that control.
        'increment counter
        nImgCount = nImgCount + 1
        'locate next frame end
        j = InStr(i, sBuff, sGifMagic) + 3
        'another check
        If j > Len(sGifMagic) Then
            'pad an output string, fill with the
            'frame info, and write to disk. A header
            'needs to be added as well, to assure
            'LoadPicture recognizes it as a gif.
            'Since VB's LoadPicture command ignores
            'header info and loads animated gifs as
            'static, we can safely reuse the header
            'extracted above.
            sPicsBuff = String(Len(sFileHeader) + j - i, Chr$(0))
            sPicsBuff = sFileHeader & Mid(sBuff, i - 1, j - i)
            Put #hFile, 1, sPicsBuff
            'The first part of the
            'extracted data is frame info
            sImgHeader = Left(Mid(sBuff, i - 1, j - i), 16)
            'embedded in the frame info is a
            'field that represents the frame delay
            TimeWait = ((Asc(Mid(sImgHeader, 4, 1))) + (Asc(Mid(sImgHeader, 5, 1)) * 256&)) * 10&
            'assign the data.
            If nImgCount >= 1 Then
                'if this is the second or later
                'frame, load an image control
                'for the frame
                ReDim Preserve aImg(nImgCount + 1)
                ReDim Preserve imageHDC(nImgCount + 1)
                ReDim Preserve picdelay(nImgCount + 3)
                'aImg(nImgCount - 1).ZOrder 0
                'the frame header also contains
                'the x and y offsets of the image
                'in relation to the first (0) image.
                xOff = Asc(Mid(sImgHeader, 9, 1)) + (Asc(Mid(sImgHeader, 10, 1)) * 256&)
                yOff = Asc(Mid(sImgHeader, 11, 1)) + (Asc(Mid(sImgHeader, 12, 1)) * 256&)
                'position the image controls at
                'the required position
                'aImg(nImgCount - 1).Left = aImg(0).Left + (xOff * Screen.TwipsPerPixelX)
                'aImg(nImgCount - 1).Top = aImg(0).Top + (yOff * Screen.TwipsPerPixelY)
            End If
            'use each control's .Tag property to
            'store the frame delay period, and
            'load the picture into the image control.
            'aImg(nImgCount - 1).Tag = TimeWait
            Set aImg(nImgCount - 1) = LoadPicture("temp.gif")
            imageHDC(nImgCount - 1) = CreateCompatibleDC(0)
            picdelay(nImgCount - 1) = TimeWait
            'OldPicture(nImgCount - 1) =
            SelectObject imageHDC(nImgCount - 1), aImg(nImgCount - 1)
            'update pointer
            i = j
        End If
        'when the j = Instr() command above returns 0,
        '3 is added, so if j = 3 there was no more
        'data in the header. We're done.
        Loop Until j = 3
        'close and nuke the temp file
        Close #hFile
        Kill "temp.gif"
        FrameCount = nImgCount
        LoadGif = nImgCount
    End Function
    these function have some problems:
    1 - some animated gif's aren't read, only the 1st frame;
    2 - the timer interval isn't read correctly...
    3 - some frames aren't read correctly...
    can anyone advice me?
    Last edited by Cambalinho; August 4th, 2023 at 03:56 PM.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: how read gif files?

    I suggest you ask this question on this site's sister site VBforums
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width