2012-09-15 142 views
1

我知道类似的问题已经在各种论坛上提出过,但是没有给出的解决方案为我工作,并且想知道是否有人有他们可以提供的其他指针。我基本上试图通过TCP在同一网络上的两台计算机之间发送文件(图像)。我一直试图将图像转换为字节数组,然后在发送字符串之前将其转换为字符串。另一方面,我收到了字符串,转换为字节数组,然后转换回图像。但是,在接收端,字符串是空的,所以当我将其转换为字节数组时,出现错误。vb.net通过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 

预先感谢任何帮助,您可以给!

+0

什么是client.Connect中的客户端的定义;和VS的哪个版本是这个使用的? –

+0

客户端只是一个TCPClient,它的VS 2012专业 – crazyloonybin

回答

1

不知道这里是必然的问题,但我会建议使用System.Convert.ToBase64String字节转换为字符串(BYTE_ARRAY) 这总是会给一个安全的字符串,可以以各种方式被发送的尴尬角色。

+0

谢谢斯图尔特,将在周末尝试 - 现在没有我的第二台笔记本电脑,所以会检查并让你知道:) – crazyloonybin

+0

我试过ToBase64String但是这是给我一个空字符串,所以当我尝试将空字符串转换为字节数组时,我在接收端收到一个错误。 – crazyloonybin

+0

在这种情况下,问题不是图像的东西,字符串只是没有被传输。一个评论,虽然这不是问题,但StreamReader.Read()会返回一个整数 - 您的代码将生成一个Unicode数字列表。尝试ReadToEnd()来获取整个文本 - 不需要循环。我建议删除所有的图像的东西,并做一个测试只传递一个字符串到服务器,并重新发布一个新的问题,如果它仍然只是收到一个空字符串 –

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