每个 TcpClients 发送和接收位图

Sending and Receiving Bitmaps per TcpClients

所以,我目前正在制作一个 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

如有任何答案,我将不胜感激!

Dim ImageBytes(FrameClient.ReceiveBufferSize) As Byte

这returns缓冲区的大小,而不是位图的实际大小:

The size of the receive buffer, in bytes. The default value is 8192 bytes.

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

您可能希望在发送位图数据之前发送尺寸,并在接收端先读取尺寸以初始化您的 MemoryStream