Page 1 of 1

Animated GIF in VB without OCX, DLL, API

Posted: Wed Nov 12, 2008 6:40 pm
by Kwai chang caine
Hello at all

I have found a super code, all simple, for view a animated gif
But the problem is that it was in VB
But it's without OCX, without API, and no DLL :D
Just a timer, but PB know do that, an control picture, and it's all

The more difficult it's the name of the creator : Arkadiy Olovyannikov (It's not english like name) :lol:

I don't believe, i have seen this before in PB :roll:
I don't like when VB can do something that PB can not do :?
And the code is more simple it's like PB code :lol:
I have do a fast search in the forum and not found the same function.

The creator can read a gif and take the parameter of it :shock:

I know that in PB it's possible to include a GIF in a WebGadget.
But if the pc don't have IE or firefox, there are not WebGadget
And i think it's a better way to manage a gif, directly in a ImageGadget :roll:
And it's an another way, and two ways it's not too :wink:

I have already put this code in my VB application, and he works very fine.
Like with a skinned form, it's works :shock:
And i have a gif alone in my desktop :D

I give you this code if somebody need an idea for begin a code like that

I believe that this code can be useful at this forum.8)
I have always put this in the french forum.

For download the source for all who have VB
http://www.freevbcode.com/ShowCode.Asp?ID=758

And for all who not have VB (The lucky) :lol:

Put this in a module :

Code: Select all

Public RepeatTimes As Long 'This one calculates, but don't use in this sample. If You need, You can add simple checking at Timer1_Timer Procedure
Public TotalFrames As Long
Public FrameCount As Long

Public Function LoadGif(sFile As String, aImg As Variant) As Boolean
 
 LoadGif = False
 
 If Dir$(sFile) = "" Or sFile = "" Then
  MsgBox "File " & sFile & " not found", vbCritical
  Exit Function
 End If
 
 On Error GoTo ErrHandler
 Dim fNum As Integer
 Dim imgHeader As String, fileHeader As String
 Dim buf$, picbuf$
 Dim imgCount As Integer
 Dim i&, j&, xOff&, yOff&, TimeWait&
 Dim GifEnd As String
 GifEnd = Chr(0) & Chr(33) & Chr(249)
 
 For i = 1 To aImg.Count - 1
  Unload aImg(i)
 Next i
 
 fNum = FreeFile
 
 Open sFile For Binary Access Read As fNum
 buf = String(LOF(fNum), Chr(0))
 Get #fNum, , buf 'Get GIF File into buffer
 Close fNum
 
 i = 1
 imgCount = 0
 j = InStr(1, buf, GifEnd) + 1
 fileHeader = Left(buf, j)
 
 If Left$(fileHeader, 3) <> "GIF" Then
  MsgBox "This file is not a *.gif file", vbCritical
  Exit Function
 End If
 
 LoadGif = True
 i = j + 2
 
 If Len(fileHeader) >= 127 Then
  RepeatTimes& = Asc(Mid(fileHeader, 126, 1)) + (Asc(Mid(fileHeader, 127, 1)) * 256&)
 Else
  RepeatTimes = 0
 End If

 Do ' Split GIF Files at separate pictures and load them into Image Array
 
  imgCount = imgCount + 1
  j = InStr(i, buf, GifEnd) + 3
  
  If j > Len(GifEnd) Then
   
   fNum = FreeFile
   Open "temp.gif" For Binary As fNum
   picbuf = String(Len(fileHeader) + j - i, Chr(0))
   picbuf = fileHeader & Mid(buf, i - 1, j - i)
   Put #fNum, 1, picbuf
   imgHeader = Left(Mid(buf, i - 1, j - i), 16)
   Close fNum
   TimeWait = ((Asc(Mid(imgHeader, 4, 1))) + (Asc(Mid(imgHeader, 5, 1)) * 256&)) * 10&
   
   If imgCount > 1 Then
    xOff = Asc(Mid(imgHeader, 9, 1)) + (Asc(Mid(imgHeader, 10, 1)) * 256&)
    yOff = Asc(Mid(imgHeader, 11, 1)) + (Asc(Mid(imgHeader, 12, 1)) * 256&)
    Load aImg(imgCount - 1)
    aImg(imgCount - 1).Left = aImg(0).Left + (xOff * Screen.TwipsPerPixelX)
    aImg(imgCount - 1).Top = aImg(0).Top + (yOff * Screen.TwipsPerPixelY)
   End If
   
   ' Use .Tag Property to save TimeWait interval for separate Image
   aImg(imgCount - 1).Tag = TimeWait
   aImg(imgCount - 1).Picture = LoadPicture("temp.gif")
   Kill ("temp.gif")
   i = j
  
  End If
  
  DoEvents
 
 Loop Until j = 3
 
 ' If there are one more Image - Load it
 
 If i < Len(buf) Then
  
  fNum = FreeFile
  Open "temp.gif" For Binary As fNum
  picbuf = String(Len(fileHeader) + Len(buf) - i, Chr(0))
  picbuf = fileHeader & Mid(buf, i - 1, Len(buf) - i)
  Put #fNum, 1, picbuf
  imgHeader = Left(Mid(buf, i - 1, Len(buf) - i), 16)
  Close fNum
  TimeWait = ((Asc(Mid(imgHeader, 4, 1))) + (Asc(Mid(imgHeader, 5, 1)) * 256)) * 10
  
  If imgCount > 1 Then
   xOff = Asc(Mid(imgHeader, 9, 1)) + (Asc(Mid(imgHeader, 10, 1)) * 256)
   yOff = Asc(Mid(imgHeader, 11, 1)) + (Asc(Mid(imgHeader, 12, 1)) * 256)
   Load aImg(imgCount - 1)
   aImg(imgCount - 1).Left = aImg(0).Left + (xOff * Screen.TwipsPerPixelX)
   aImg(imgCount - 1).Top = aImg(0).Top + (yOff * Screen.TwipsPerPixelY)
  End If
  
  aImg(imgCount - 1).Tag = TimeWait
  aImg(imgCount - 1).Picture = LoadPicture("temp.gif")
  Kill ("temp.gif")
 
 End If
 
 TotalFrames = aImg.Count - 1
 Exit Function

ErrHandler:
 MsgBox "Error No. " & Err.Number & " when reading file", vbCritical
 LoadGif = False
 On Error GoTo 0

End Function
And this is the call

Code: Select all


If LoadGif(App.Path + "\ImageGif.gif", Form.ImgGif) Then
   FrameCount = 0
   Programme.Timer1.Interval = CLng(Programme.ImgSaliere(0).Tag)
   Programme.Timer1.Enabled = True
  End If
You see, it's not hard :D
But always too hard for KCC :oops:

I wish you a good day

Posted: Wed Nov 12, 2008 6:44 pm
by srod
How do you propose replacing the LoadPicture() function? :wink:

Posted: Wed Nov 12, 2008 7:33 pm
by localmotion34
please look at the source of my animated GIF control. haqibaba tweaked C source for a GIF decoder, and I then took that and was able to parse the format of GIF frames and create a static control that displays the frames of the GIF based on internal GIF frame structures and their set delay times.


It has already been done in PB.

Posted: Wed Nov 12, 2008 10:51 pm
by Kwai chang caine
How do you propose replacing the LoadPicture() function?
Oh no.....you know, you begin to knowing me, i can't replace anything me, in programing :cry:
I can just replace my pantie :lol:

It's just because i have found this code really short and effective, like the PB code, but it's VB code, and i don't like when it's VB code :?
Usually PB is allways the best, i'm used of this.
I don't like that VB is before, even for once :D

@Localmotion34
Yes your code is cool 8)
But i don't understand how you can read when the picture make a delay.
Because sometimes a GIF have a different delay between the picture and she stop and restart.
And for the transparency ??? with your code you can manage this ???

Because this code manage the GIF exactly like a HTML page :shock: