Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet size information from a GIF or JPG file without loading the whole picture
DescriptionThis example shows how to get size information from a GIF or JPG file without loading the whole picture in Visual Basic 6.
KeywordsGIF, size, image size, GIF size
CategoriesGraphics, Files and Directories
 
Thanks to Neil Crosby.

Subroutine GetGIFInfo gets the GIF file's size and then reads its first five bytes. The first three bytes should be "GIF" if this is a GIF file.

The next two bytes gives the image's width and the two after that give its height.

 
Private Function GetGIFInfo(ByVal FileName As String) As _
    BITMAPINFO
    Dim bChar As Byte
    Dim i As Integer
    Dim dotPos As Integer
    Dim Header As String
    Dim blExit As Boolean
    Dim a As String, b As String
    Dim ImgSize As String
    Dim fnum As Integer
    Dim ImgWidth As Integer
    Dim ImgHeight As Integer

    On Error Resume Next
    fnum = FreeFile
    Open FileName For Binary As #fnum

    ImgSize = LOF(fnum) / 1024

    dotPos = InStr(ImgSize, ",")
    ImgSize = Left(ImgSize, dotPos - 1)

    For i = 0 To 5
        Get #fnum, , bChar
        Header = Header + Chr(bChar)
    Next i

    If Left(Header, 3) <> "GIF" Then
        MsgBox FileName & ": not a GIF file"
        Close #fnum
        Exit Function
        End
    End If

    Get #fnum, , bChar
    a = a + Chr(bChar)
    Get #fnum, , bChar
    a = a + Chr(bChar)

    ImgWidth = CInt(Asc(Left(a, 1)) + 256 * Asc(Right(a, _
        1)))

    Get #fnum, , bChar
    b = b + Chr(bChar)
    Get #fnum, , bChar
    b = b + Chr(bChar)

    ImgHeight = CInt(Asc(Left(b, 1)) + 256 * Asc(Right(b, _
        1)))

    Close #fnum

    With ImageInfo
        .Width = ImgWidth
        .Height = ImgHeight
    End With

    GetGIFInfo = ImageInfo
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated