在vb.net上加载pal文件

在vb.net上加载pal文件,vb.net,Vb.net,我在vb.net上遇到了这样一个错误“无法读取流末尾以外的内容”。 它发生在我编译程序时,它在那里中断 Dim red As Byte = br.ReadByte() Dim green As Byte = br.ReadByte() Dim blue As Byte = br.ReadByte() Dim flags As Byte = br.ReadByte() 但我也将给出所有代码: Public Shared Function LoadPal(filename As String) A

我在vb.net上遇到了这样一个错误“无法读取流末尾以外的内容”。 它发生在我编译程序时,它在那里中断

Dim red As Byte = br.ReadByte()
Dim green As Byte = br.ReadByte()
Dim blue As Byte = br.ReadByte()
Dim flags As Byte = br.ReadByte()
但我也将给出所有代码:

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

另一个幸运的人。我希望这有助于你,我不知道为什么,但托盘必须装载2乘2,如果不是的颜色将得到混合或类似的东西

调用加载

如果可以保存,为什么要加载

调用Save


您的文件的字节数比您预期的要少。你要么(1)读错了,要么(2)有一个损坏的文件。
'''''''''''''''''''''''''''''''''
    Dim colors1

    colerss = LoadPal(.FileName)
'''''''''''''''''''''''''''''''''

'LOAD PALL
    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

        ' Colors
        For i As Integer = 0 To 7
            Try
                ''''''''''''First''''''''''''''''
                Dim red As Byte = br.ReadByte()
                Dim green As Byte = br.ReadByte()
                Dim blue As Byte = br.ReadByte()
                Dim flags As Byte = br.ReadByte()
                'MsgBox(blue & " " & green & " " & red)
                colors.Add(Color.FromArgb(blue, green, red))
                ''''''''''''''''''Second''''''''''''''''''''
                Dim red1 As Byte = br.ReadByte()
                Dim green1 As Byte = br.ReadByte()
                Dim blue1 As Byte = br.ReadByte()
                Dim flags1 As Byte = br.ReadByte()
                'MsgBox(blue1 & " " & green1 & " " & red1)
                colors.Add(Color.FromArgb(blue1, green1, red1))
                '''''''''''''''''''''''''''''''''''''''''''''''
            Catch
                MsgBox("Erro")
            Finally

            End Try
        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
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

 SavePal(saveFileDialog1.FileName & "TheNameUWant.pal", colors1)


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

  'SAVE PAL
Public Shared Sub SavePal(filename As String, colors As List(Of Color))
    ' Calculate file length
    Dim stream As New FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None)
    Using bw As New BinaryWriter(stream)
        ' Colors
        For i = 0 To 15
            'i dont know why but for insert the pallete i need change red whit blue and blue whit red
            bw.Write(CByte(colors(i).B))
            bw.Write(CByte(colors(i).G))
            bw.Write(CByte(colors(i).R))
            ' Flag in W:A always 0x00
            bw.Write(CByte(0))
        Next
        WriteStringBytes(bw, "PAL ")
    End Using
End Sub

 Private Shared Sub WriteStringBytes(bw As BinaryWriter, value As String)
        bw.Write(Encoding.ASCII.GetBytes(value))
    End Sub