Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Png headers


Robin
 Share

Recommended Posts

Here's the code if anyone wants it.

```
' PNG Header data
Private Type PNGHeader
    Width As Integer
    Height As Integer
End Type
```
```
Private Function GetPNGSize(ByVal filename As String) As PNGHeader
Dim Buffer() As Byte
Dim ChunkSize As Long
Dim ChunkType As String
Dim f As Long

    f = FreeFile()
    Open filename For Binary As #f
        If Err <> 0 Then Exit Function
        If LOF(f) = 0 Then Exit Function

        ' Read the first 2 bytes to identifier
        ReDim Buffer(0 To 1)
        Get #f, , Buffer

        ' Get the next 6 bytes to confirms that a PNG
        ReDim Buffer(0 To 5)
        Get #f, , Buffer
        If Buffer(0) <> &H4E Or Buffer(1) <> &H47 Or Buffer(2) <> &HD Or _
          Buffer(3) <> &HA Or Buffer(4) <> &H1A Or Buffer(5) <> &HA Then
            Exit Function
        End If

        ' Dim the array. Both the chunk size and chunk type have 4 bytes
        ReDim Buffer(0 To 3)

        Do While True
            ' Get the chunk size
            Get #f, , Buffer
            ChunkSize = (Buffer(0) * (2 ^ 24)) + (Buffer(1) * (2 ^ 16)) + (Buffer(2) * (2 ^ 8)) + Buffer(3)

            ' Get the chunk type
            Get #f, , Buffer
            ChunkType = Chr(Buffer(0)) & Chr(Buffer(1)) & Chr(Buffer(2)) & Chr(Buffer(3))

            ' Set the array to chunk size all chunk
            ReDim Buffer(0 To ChunkSize - 1)
            Get #f, , Buffer

            Select Case ChunkType
                Case "IHDR"
                    ' Main Chunk with Height, Width
                    GetPNGSize.Width = (Buffer(0) * (2 ^ 24)) + (Buffer(1) * (2 ^ 16)) + (Buffer(2) * (2 ^ 8)) + Buffer(3)
                    GetPNGSize.Height = (Buffer(4) * (2 ^ 24)) + (Buffer(5) * (2 ^ 16)) + (Buffer(6) * (2 ^ 8)) + Buffer(7)

                Case "IDAT", "IEND"
                    Exit Do
            End Select

            ReDim Buffer(0 To 3)

            ' Jump 4 bytes
            Get #f, , Buffer
        Loop
    Close #f

End Function
```
Link to comment
Share on other sites

@Zetta:

> I think it would be much easier to make a C++ dll to do that since I think JBufferedImage will read and convert images to raw image files but w/e. Also, JBufferedImage isn't a standard C++ class.

I originally tried to convert some Java code over, but half-way through someone pointed me to a class which did the work, so I just ripped the PNG part out and made a function out of it :P
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...