Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read a GIF image's dimensions

Status
Not open for further replies.

Giraffe69

Programmer
Oct 7, 2003
22
GB
I need to be able to read an image's dimensions in VB6, but am struggling with the GIF format.

Its easy to do for a bitmap by directly reading the image header part of the file, and can do similar for a JPEG with the help of Intel's JPEG Library.

From code that writes GIF it appears that an image header:

Code:
Private Type GifImageDescriptor
     Left As Integer
     Top As Integer
     Width As Integer
     Height As Integer
     Format As Byte 'ImageFormat
End Type

is written to position 783 for an '87 GIF and 791 for an '89 GIF. The code I've written:

Code:
Private Function GetGIFDimensions(ByVal strGIFFile As String, ByRef intGIFWidth As Integer, ByRef intGIFHeight As Integer) As Boolean
    Dim intGFileNo As Integer, strGReadHead  As String * 5
    On Error GoTo errGetGIFDimensions
    intGFileNo = FreeFile
    Open strGIFFile For Binary Access Read Lock Read As #intGFileNo
        Get #intGFileNo, 1&, strGReadHead
        If strGReadHead = "GIF87" Then
            Get #intGFileNo, 787&, intGIFWidth
            Get #intGFileNo, 789&, intGIFHeight
            GetGIFDimensions = True
        ElseIf strGReadHead = "GIF89" Then
            Get #intGFileNo, 795&, intGIFWidth
            Get #intGFileNo, 797&, intGIFHeight
            GetGIFDimensions = True
        End If
    Close #intGFileNo
errGetGIFDimensions:
    On Error GoTo 0
    Reset
End Function

doesnt read the correct values.

I can do a workaround by using a control, loadpicture, autosize, scaleheight etc., but that's too slow when working with thousands of images.

Any advice would be really appreciated!

"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true." -- Robert Wilensky
 
Option Explicit

' Shows filename and dimensions of all gif files in named folder
Public Sub Example(Optional strFolder As String = "C:\")
Dim myShell As Shell32.Shell
Dim myFolderItems As FolderItems3
Dim myFile As ShellFolderItem ' This is the important declaration to get the extended properties later

Set myShell = New Shell32.Shell
Set myFolderItems = myShell.NameSpace(strFolder).Items
myFolderItems.Filter 64, "*.gif"

For Each myFile In myFolderItems
'Do your thing here. This example uses a message box
MsgBox myFile & vbCrLf & "Dimensions: " & myFile.ExtendedProperty("dimensions")
Next
End Sub
 
Thanks strongm,
that looks brilliant, and so simple - I'm embarrassed!
sorry for being thick, but how do I reference Shell32?
thanks
G

"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true." -- Robert Wilensky
 
Sorry - that's the problem with putting an example together too quickly. What you need to dfo is add a reference to the Microsoft Shell Controls and Automation library
 
works a treat! - thanks

"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true." -- Robert Wilensky
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top