在vb.net上加载pal文件

时间:2015-06-11 16:13:48

标签: 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) 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

1 个答案:

答案 0 :(得分:0)

另一个幸运的人。我希望这有助于你,我不知道为什么但托盘必须加载2乘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