VB's LoadPicture hangs if a defective GIF or JPG load is attempted. Other than using IE, or a third party control, is there a way to do this in vb?
Thanks
Printable View
VB's LoadPicture hangs if a defective GIF or JPG load is attempted. Other than using IE, or a third party control, is there a way to do this in vb?
Thanks
There should be no problem loading JPG or GIf (not animated) picture
Picture1 = LoadPicture("filename.jpg\.gif")
And to load animated GIFs, you could use
this code:
1) Make a picturebox (Picture1, and set it's Index to 0)
2) Make a Timer (Timer1)
Dim RepeatTimes&
Dim RepeatCount&
Sub LoadAniGif(xFile as string, xImgArray)
If Not IIf(Dir$(xFile) = "", false, true) Or xFile = "" then
MsgBox "File not found.", vbExclamation, "File error"
Exit Sub
End If
Dim F1, F2
Dim Picture1s() as string
Dim imgHeader as string
static buf$, picbuf$
Dim fileHeader as string
Dim imgCount
Dim i&, j&, xOff&, yOff&, TimeWait&
Dim GifEnd
GifEnd = Chr(0) & "!ù"
Timer1.Enabled = false
for i = 1 to xImgArray.Count - 1
Unload xImgArray(i)
next i
F1 = FreeFile
on error GoTo badFile:
Open xFile for binary Access Read as F1
buf = string(LOF(F1), Chr(0))
get #F1, , buf
Close F1
i = 1
imgCount = 0
j = (InStr(1, buf, GifEnd) + len(GifEnd)) - 2
fileHeader = Left(buf, j)
i = j + 2
If len(fileHeader) < 127 then
repeartimes& = 1
else
RepeatTimes& = Asc(mid(fileHeader, 126, 1)) + _
(Asc(mid(fileHeader, 127, 1)) * TIMES)
End If
Do
imgCount = imgCount + 1
j = InStr(i, buf, GifEnd) + len(GifEnd)
If j > len(GifEnd) then
F2 = FreeFile
Open "tmp.gif" for binary as F2
picbuf = string(len(fileHeader) + j - i, Chr(0))
picbuf = fileHeader & mid(buf, i - 1, j - i)
Put #F2, 1, picbuf
imgHeader = Left(mid(buf, i - 1, j - i), 16)
Close F2
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)) * 2561)
Load xImgArray(imgCount - 1)
xImgArray(imgCount - 1).ZOrder 0
xImgArray(imgCount - 1).Left = xImgArray(0).Left + (xOff * 15)
xImgArray(imgCount - 1).Top = xImgArray(0).Top + (yOff * 15)
End If
xImgArray(imgCount - 1).Tag = TimeWait
xImgArray(imgCount - 1).Picture = LoadPicture("tmp.gif")
Kill ("tmp.gif")
i = j '+ 1
End If
Loop Until j = len(GifEnd)
If i < len(buf) then
F2 = FreeFile
Open "tmp.gif" for binary as F2
picbuf = string(len(fileHeader) + len(buf) - i, Chr(0))
picbuf = fileHeader & mid(buf, i - 1, len(buf) - i)
Put #F2, 1, picbuf
imgHeader = Left(mid(buf, i - 1, len(buf) - i), 16)
Close F2
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)) * 2561)
Load xImgArray(imgCount - 1)
xImgArray(imgCount - 1).ZOrder 0
xImgArray(imgCount - 1).Left = xImgArray(0).Left + (xOff * 15)
xImgArray(imgCount - 1).Top = xImgArray(0).Top + (yOff * 15)
End If
xImgArray(imgCount - 1).Tag = TimeWait
xImgArray(imgCount - 1).Picture = LoadPicture("tmp.gif")
Kill ("tmp.gif")
End If
on error GoTo badTime
Timer1.Interval = CInt(xImgArray(0).Tag)
badTime:
Timer1.Enabled = true
Exit Sub
badFile:
MsgBox "File not found.", vbExclamation, "File error"
End Sub
private Sub Timer1_Timer()
for i = 0 to Picture1.Count
If i = Picture1.Count then
If RepeatTimes > 0 then
RepeatCount = RepeatCount + 1
If RepeatCount > RepeatTimes then
Timer1.Enabled = false
Exit Sub
End If
End If
for j = 1 to Picture1.Count - 1
Picture1(j).Visible = false
next j
on error GoTo badTime
Timer1.Interval = CLng(Picture1(0).Tag)
badTime:
Exit for
End If
If Picture1(i).Visible = false then
Timer1.Interval = CLng(Picture1(i).Tag)
on error GoTo badTime2
Picture1(i).Visible = true
badTime2:
Exit for
End If
next i
End Sub
private Sub Form_Load()
Picture1(Index).BorderStyle = 0
Call LoadAniGif("filename.gif", Picture1)
End Sub
LoadPicture or code above should load any kind of GIF or JPG file
Good Luck!!!
Just a couple of questions about this:
1) what should the tmp.gif be? should i create a 1X1 gif image as a place holder? or is this the same as the xfile gif image.
2) what is the TIMES variable that isn't declared. i assume that it's the number of times to loop through the animated gif. is there a way to set it up to run indefinitely?
Thanks,
John
John Pirkey
MCSD
www.ShallowWaterSystems.com
okay, nevermind the above questions - sorry.
but i am getting an overflow error on this line:
>TimeWait = ((Asc(Mid(imgHeader, 4, 1))) + (Asc(Mid(imgHeader, 5, 1)) * 256)) * 10
any suggestions?
thanks,
John
John Pirkey
MCSD
www.ShallowWaterSystems.com
Alternatively, you can download a free anitmated GIF control (no source unfortunately) from : http://nt1.pncl.co.uk/sbutler/vb/activex.asp - it's much quicker than the method in Andy's post (although that does contain some excellent reference material).
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
Try to remove multiplication symbols and integers (256 and 10), also you could try to remove whole line, but not sure if that's a good idea
never mind about removing wholde line, that wont work, i accidently modified a line, but i thougth i removed it, so nevermind aobut removing the line