'Load pal files on vb.net

Im having this error on vb.net "unable to read beyond the end of the stream". It happens when i compile the program and it breaks in there.

Dim red As Byte = br.ReadByte()
Dim green As Byte = br.ReadByte()
Dim blue As Byte = br.ReadByte()
Dim flags As Byte = br.ReadByte()

But i will also give all the code:

Public Shared Function LoadPal(filename As String) As List(Of Color)
        Dim colors As New List(Of Color)()
        Dim stream As New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
        Using br As New BinaryReader(stream)
            ' RIFF header
            Dim riff As String = ReadByteString(br, 4)
            ' "RIFF"
            Dim dataSize As Integer = br.ReadInt32()
            Dim type As String = ReadByteString(br, 4)
            ' "PAL "
            ' Data chunk
            Dim chunkType As String = ReadByteString(br, 4)
            ' "data"
            Dim chunkSize As Integer = br.ReadInt32()
            Dim palVersion As Short = br.ReadInt16()
            ' always 0x0300
            Dim palEntries As Short = br.ReadInt16()

            ' Colors
            For i As Integer = 0 To palEntries - 1
                Dim red As Byte = br.ReadByte()
                Dim green As Byte = br.ReadByte()
                Dim blue As Byte = br.ReadByte()
                Dim flags As Byte = br.ReadByte()
                ' always 0x00
                colors.Add(Color.FromArgb(red, green, blue))
            Next
        End Using
        Return colors
    End Function


 Private Shared Function ReadByteString(br As BinaryReader, length As Integer) As String
        Return Encoding.ASCII.GetString(br.ReadBytes(length))
    End Function


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source