每个TcpClients发送和接收位图

时间:2016-01-02 13:23:23

标签: vb.net tcp bitmap client

所以,我目前正在制作LAN-Streaming-Program,它应该记录流光的屏幕并将其显示在观众的屏幕上。 我的问题是,当我尝试将收到的ByteArray-Image转换为图像时,它会给我一个System.ArgumentException

以下是发送代码:

Private Sub SendFrame(ByRef IP As String, ByRef Frame As Bitmap)

    FrameClient = New TcpClient(IP, 7009)

    Dim FrameStream As New MemoryStream
    Dim FrameBytes() As Byte

    Frame.Save(FrameStream, ImageFormat.Bmp)
    FrameBytes = FrameStream.GetBuffer()

    SendMessage(IP, "NextFrameSize;" & LocalIP & ";" & FrameBytes.Length)

    Using NS As NetworkStream = FrameClient.GetStream
        NS.Write(FrameBytes, 0, FrameBytes.Length)
        NS.Close()
    End Using

End Sub

这里接收:

Private Sub CheckForFrames() 'This Sub is called everytime the viewer receives the size of the next Bitmap.

    If FrameListener.Pending = True Then

        FrameClient = FrameListener.AcceptTcpClient
        Dim ImageBytes(NextFrameSize) As Byte

        FrameClient.GetStream.Read(ImageBytes, 0, NextFrameSize)

        Dim MS As New MemoryStream(ImageBytes)
        StreamBox.Image = Image.FromStream(MS, False)
        MS.Close()

        FlushMemory()

    End If

End Sub

我会感谢任何答案!

1 个答案:

答案 0 :(得分:1)

Dim ImageBytes(FrameClient.ReceiveBufferSize) As Byte

这将返回te缓冲区的大小,而不是位图的实际大小:

  

接收缓冲区的大小,以字节为单位。默认值为8192   字节。

因此,ImageBytes可能是8192字节,而不是位图的大小。

您可能希望在发送位图数据之前发送大小,并在接收方首先读取大小以初始化MemoryStream