Ok, here a version of some code that I found at PSC and I was thinking of making this a FAQ for this forum.
This VB6 code has been tested on...
Win95 Version B VB6 SP5 (AMD 200Mhz)
WinNT4 SP6 VB6 SP5 (AMD 1Ghz)
Win2k Server VB6 SP5 .NET 2002 (AMD 1Ghz)
Win2k Pro SP3 VB6 SP5 .NET 2002 SP1 and .NET 2003 (P3 750Mhz)
it seems to work fine on all machines except the old one at the top. It has a flicker between frames.
Please note that this is by no means a perfect solution but with the decoding of the gif done there are many ways to improve this code to include double buffering (to get rid of that flicker on older machines).
[tt]
'to use this demo you will need a timer and an image control along with the path of the animated gif file
'once you have added the image control (Image1) set its index to zero (0)
Option Explicit
Dim NumberOfTimesToRepeatSequence As Long, RepeatedCount As Long
Dim MaxCnt As Integer, ImgCnt As Integer
Private Sub Form_Load()
On Error GoTo Form_LoadError
Me.Width = 8000
Image1(0).Left = 0
Image1(0).Top = 0
'place the path to the gif you want to animate here
Call DecodeGif("place the path to the gif you want to animate here"
Exit Sub
Form_LoadError:
MsgBox Err.Description
End Sub
Private Sub DecodeGif(PathToGifFile As String)
On Error GoTo DecodeGifError
'declare procedural variables
Dim FNumb As Integer, GifBuffer As String, GifHeader As String, SectionStart As Long, SectionEnd As Long, SectionMarker As String
Dim ImageCount As Integer, I As Integer
Dim NewPicBuff As String, ImageHeader As String, DisplayTime As Long
Dim LftOffSet As Long, TopOffSet As Long
'make sure we have something to work with
If Dir(PathToGifFile) = vbNullString Then
MsgBox "Wheres the GIF!?"
Exit Sub
End If
'disable timer
Timer1.Enabled = False
'if you were to change this demo so other gifs could be displayed in this array then you would need to unload all but the origional
For I = 1 To Image1.Count - 1
Unload Image1(I)
Next I
'set value(s)
SectionMarker = Chr(0) & "!ù"
SectionStart = 1
'open our gif, read it in, close it out
FNumb = FreeFile
Open PathToGifFile For Binary As #FNumb
GifBuffer = Input(FileLen(PathToGifFile), #FNumb)
Close #FNumb
'get where this (the header info) ends
SectionEnd = InStr(SectionStart, GifBuffer, SectionMarker) + Len(SectionMarker) - 2
'retrieve the header
GifHeader = Left(GifBuffer, SectionEnd)
'set where the next section starts at
SectionStart = SectionEnd + 2
'check the length of the header for extended information
If Len(GifHeader) > 127 Then
NumberOfTimesToRepeatSequence = Asc(Mid(GifHeader, 126, 1)) + (Asc(Mid(GifHeader, 127, 1)) * 256)
Else
NumberOfTimesToRepeatSequence = 0 'infinit loop
End If
'now run through file an decode each frame
Do While SectionEnd <> Len(SectionMarker)
'increase the count of images we have by 1
ImageCount = ImageCount + 1
'find out where the next section ends
SectionEnd = InStr(SectionStart, GifBuffer, SectionMarker) + Len(SectionMarker)
'check to make sure we have some information to use
If SectionEnd > Len(SectionMarker) Then
'create a single frame gif from this information
NewPicBuff = GifHeader & Mid(GifBuffer, SectionStart - 1, SectionEnd - SectionStart)
'now write it to file so we can use the load picture function on it
FNumb = FreeFile
Open App.Path & "\temp.gif" For Binary As #FNumb
Put #FNumb, 1, NewPicBuff
Close #FNumb
'now extract some information about the file we just saved
ImageHeader = Left(Mid(GifBuffer, SectionStart - 1, SectionEnd - SectionStart), 16)
'now calcualte the time that the image we just saved is to be displayed
DisplayTime = ((Asc(Mid(ImageHeader, 4, 1))) + (Asc(Mid(ImageHeader, 5, 1)) * 256)) * 10
'check to see if we have more than one image
If ImageCount > 1 Then
'retrieve offsets
LftOffSet = Asc(Mid(ImageHeader, 9, 1)) + (Asc(Mid(ImageHeader, 10, 1)) * 256)
TopOffSet = Asc(Mid(ImageHeader, 11, 1)) + (Asc(Mid(ImageHeader, 12, 1)) * 256)
'load a new control and set its properties
Load Image1(ImageCount - 1)
Image1(ImageCount - 1).ZOrder 0
Image1(ImageCount - 1).Left = Image1(0).Left + (LftOffSet * 15)
Image1(ImageCount - 1).Top = Image1(0).Top + (TopOffSet * 15)
End If
'set the amount of time that this frame of the gif image is to be displayed for and load the image
Image1(ImageCount - 1).Tag = DisplayTime
Image1(ImageCount - 1).Picture = LoadPicture(App.Path & "\temp.gif"
'don't need it anymore so kill it
Kill App.Path & "\temp.gif"
SectionStart = SectionEnd
End If
Loop
If SectionStart < Len(GifBuffer) Then
'create a single frame gif from this information
NewPicBuff = GifHeader & Mid(GifBuffer, SectionStart - 1, Len(GifBuffer) - SectionStart)
'now write it to file so we can use the load picture function on it
FNumb = FreeFile
Open App.Path & "\temp.gif" For Binary As #FNumb
Put #FNumb, 1, NewPicBuff
Close #FNumb
'now extract some information about the file we just saved
ImageHeader = Left(Mid(GifBuffer, SectionStart - 1, Len(GifBuffer) - SectionStart), 16)
'now calcualte the time that the image we just saved is to be displayed
DisplayTime = ((Asc(Mid(ImageHeader, 4, 1))) + (Asc(Mid(ImageHeader, 5, 1)) * 256)) * 10
'check to see if we have more than one image
If ImageCount > 1 Then
'retrieve offsets
LftOffSet = Asc(Mid(ImageHeader, 9, 1)) + (Asc(Mid(ImageHeader, 10, 1)) * 256)
TopOffSet = Asc(Mid(ImageHeader, 11, 1)) + (Asc(Mid(ImageHeader, 12, 1)) * 256)
'load a new control and set its properties
Load Image1(ImageCount - 1)
Image1(ImageCount - 1).ZOrder 0
Image1(ImageCount - 1).Left = Image1(0).Left + (LftOffSet * 15)
Image1(ImageCount - 1).Top = Image1(0).Top + (TopOffSet * 15)
End If
'set the amount of time that this frame of the gif image is to be displayed for and load the image
Image1(ImageCount - 1).Tag = DisplayTime
Image1(ImageCount - 1).Picture = LoadPicture(App.Path & "\temp.gif"
'don't need it anymore so kill it
Kill App.Path & "\temp.gif"
End If
'set our variables that we will use to keep track of which frame of the animation we are on
ImgCnt = 0
MaxCnt = Image1.Count - 1
'set up the timer
Timer1.Interval = CInt(Image1(0).Tag)
Timer1.Enabled = True
Exit Sub
DecodeGifError:
MsgBox Err.Description
End Sub
Private Sub Timer1_Timer()
On Error GoTo Timer1_TimerError
'increment,show, and set the next timers interval
ImgCnt = ImgCnt + 1
Image1(ImgCnt).Visible = True
Timer1.Interval = CInt(Image1(ImgCnt).Tag)
'for gifs that have a moving animation each frame is offset from the first frame by some amount so you need to make sure that the old frame is not
'shown because you would get one image overlaid on another (or in some cases next to each other).
If ImgCnt = 0 Then
'for images with offsets you need to do this
Image1(MaxCnt).Visible = False
Else
'for images with offsets you need to do this
Image1(ImgCnt - 1).Visible = False
'check to see if we have reached the end of the animation
If ImgCnt = MaxCnt Then
'reset the counter to before the first element since the first thing we do when we enter this sub is to increment the element counter
ImgCnt = -1
'this can be removed for continious play but since it was decoded above...
If NumberOfTimesToRepeatSequence = 0 Then
DoEvents
Else
'increment the number of times this animation has played and then check to see if we have reached the limit and if we have then disable the timer
RepeatedCount = RepeatedCount + 1
If RepeatedCount > NumberOfTimesToRepeatSequence Then Timer1.Enabled = False
End If
End If
Exit Sub
Timer1_TimerError:
MsgBox Err.Description
End Sub
[/tt]
So what do you think???