1

我知道在各种论坛上都问过类似的问题,但是没有一个给定的解决方案对我有用,并且想知道是否有人可以提供任何其他指示。我基本上是在尝试通过 TCP 在同一网络上的两台计算机之间发送文件(图像)。我一直在尝试将图像转换为字节数组,然后在发送字符串之前转换为字符串。另一方面,我收到了字符串,转换为字节数组,然后再转换回图像。但是,在接收端,字符串为空,因此当我将其转换为字节数组时出现错误。

发送方的代码是:

Private Sub startSending()
    Dim ScreenShot As Image = sc.CaptureScreen
    Dim path As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData + "\redsquirimgtest"
    sc.CaptureScreenToFile(path, System.Drawing.Imaging.ImageFormat.Jpeg)
    MsgBox("printscreen saved")

    Dim abyt() As Byte = ConvertImageFiletoBytes(path)
    MsgBox("image converted to byte array")

    Dim str As String = byteArrToString(abyt)
    MsgBox(str)

    client.Connect("192.168.1.10", 55000)
    Dim Writer As New StreamWriter(client.GetStream())
    Writer.Write(str)
    Writer.Flush()

    MsgBox("sent")

    Form2.PictureBox1.Image = ScreenShot
    Form2.Show()

    MsgBox("done")
End Sub

Public Function ConvertImageFiletoBytes(ByVal ImageFilePath As String) As Byte()
    Dim _tempByte() As Byte = Nothing
    If String.IsNullOrEmpty(ImageFilePath) = True Then
        Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
        Return Nothing
    End If
    Try
        Dim _fileInfo As New IO.FileInfo(ImageFilePath)
        Dim _NumBytes As Long = _fileInfo.Length
        Dim _FStream As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read)
        Dim _BinaryReader As New IO.BinaryReader(_FStream)
        _tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes))
        _fileInfo = Nothing
        _NumBytes = 0
        _FStream.Close()
        _FStream.Dispose()
        _BinaryReader.Close()
        Return _tempByte
    Catch ex As Exception
        Return Nothing
    End Try
End Function

Public Function byteArrToString(ByVal arr() As Byte) As String

    Return System.Text.Encoding.Unicode.GetString(arr)

End Function

然后是接收方:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    ' Check the TcpListner Pending Property 
    If TcpListener.Pending = True Then

        Dim Message As String = ""
        ConnectClient = TcpListener.AcceptTcpClient()
        MsgBox("accepting")

        Dim Reader As New StreamReader(ConnectClient.GetStream())
        While Reader.Peek > -1
            Message = Message + Reader.Read().ToString
        End While
        MsgBox(Message)

        Dim abyt() As Byte = StrToByteArray(Message)
        MsgBox("string converted to byte array")

        Dim img As Image = ConvertBytesToImageFile(abyt)
        MsgBox("byte array converted to image")

        PictureBox1.Image = img
        MsgBox("picture loaded in form")
    End If

End Sub


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    TcpListener.Start()
End Sub

Public Function ConvertBytesToImageFile(ByVal ImageData As Byte()) As Image

    Dim myImage As Image
    Dim ms As System.IO.Stream = New System.IO.MemoryStream(ImageData)
    myImage = System.Drawing.Image.FromStream(ms)
    Return myImage

End Function

Public Shared Function StrToByteArray(str As String) As Byte()

    Return System.Text.Encoding.Unicode.GetBytes(str)

End Function

提前感谢您提供的任何帮助!

4

3 回答 3

1

不确定这里是否必然存在问题,但我建议使用 System.Convert.ToBase64String(byte_array) 将字节转换为字符串,这将始终提供一个安全的字符串,没有可以以各种方式传输的尴尬字符。

于 2012-09-16T19:34:59.843 回答
0
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    ' Check the TcpListner Pending Property 
    If TcpListener.Pending = True Then

        Dim Message As String = ""
        ConnectClient = TcpListener.AcceptTcpClient()
        MsgBox("accepting")

        Dim Reader As New StreamReader(ConnectClient.GetStream())
        While Reader.Peek > -1
            Message = Message + Reader.Read().ToString
        End While
        MsgBox(Message)

        Dim abyt() As Byte = StrToByteArray(Message)
        MsgBox("string converted to byte array")

        Dim img As Image = ConvertBytesToImageFile(abyt)
        MsgBox("byte array converted to image")

        PictureBox1.Image = img
        MsgBox("picture loaded in form")
    End If

End Sub


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    TcpListener.Start()
End Sub

Public Function ConvertBytesToImageFile(ByVal ImageData As Byte()) As Image

    Dim myImage As Image
    Dim ms As System.IO.Stream = New System.IO.MemoryStream(ImageData)
    myImage = System.Drawing.Image.FromStream(ms)
    Return myImage

End Function

Public Shared Function StrToByteArray(str As String) As Byte()

    Return System.Text.Encoding.Unicode.GetBytes(str)

End Function
于 2017-02-04T13:14:46.727 回答
0

sc.CaptureScreen sc.CaptureScreenToFile(路径,System.Drawing.Imaging.ImageFormat.Jpeg)

sc。<-- ist nicht deklariert 错误

于 2020-04-29T19:58:19.620 回答