Link to home
Start Free TrialLog in
Avatar of carl5121
carl5121

asked on

send image via tcp/ip

i am trying to send a picturebox image to a client computer using tcp.but im really struggling. i have converted the image to bytes before sending it. but i am having trouble converting the bytes back to image once it gets to the client. how do i determine the about of bytes to be read.
Server Side code'''''''''''''''''''''''''''''''''''''''
 Public Sub start()
        Listener.Start()
        Client = Listener.AcceptTcpClient
        Stream = Client.GetStream()
        '----
        w = New BinaryWriter(Stream)
        r = New BinaryReader(Stream)
        '----
        If r.ReadString() = "RequestConnect" Then
            w.Write("Connected")
            Do
                w.Write(imageToByteArray(Form1.PictureBox1.Image))
                w.Write("Connected")
            Loop Until r.ReadString() = "Disconnect"
            w.Write("Disconnected")
        End If
        Client.Close()
        Listener.Stop()
    End Sub
 
    Public Function imageToByteArray(ByVal imageIn As Image) As Byte()
        Dim ms As MemoryStream = New MemoryStream()
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.jpg)
        Return ms.ToArray()
    End Function
''---------------------------------------------------------------
 
CLIENT SIDE CODE''''''''''''''''''''''''''''''''''''''''''''''''''''
 Public Sub connect()
        Client.Connect(IP, port)
        Stream = Client.GetStream
        '----
        w = New BinaryWriter(Stream)
        r = New BinaryReader(Stream)
        '----
        
        w.Write("RequestConnect")
        If r.ReadString() = "Connected" Then
            Do
 ''HERES THE PROBLEM. how do i determine the amount of bytes to read
    Form1.PictureBox1.Image = byteArrayToImage(r.ReadBytes(126643))
                Application.DoEvents()
                w.Write("RequestConnect")
            Loop Until r.ReadString() = "Disconnected"
        End If
        Client.Close()
    End Sub
 
 Public Function byteArrayToImage(ByVal byteArrayIn() As Byte) As Image
        Dim ms As MemoryStream = New MemoryStream(byteArrayIn)
        Dim returnImage As Image = Image.FromStream(ms)
        Return returnImage
    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of drichards
drichards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of carl5121
carl5121

ASKER

thank you for your help. i've been looking for an answer for a week. i joined this website today and a good choice that was.
This is how I do it with messages but it will have to be adapted to 'adding to the byte array'. I believe the keyphrase here is "While stream.DataAvailable". You will also have to use the stream (from Client.GetStream) directly. This is simply another way to do it.


While stream.DataAvailable
       i = stream.Read(bytes, 0, bytes.Length)
      Msg = Msg + System.Text.Encoding.ASCII.GetString(bytes, 0, i)
      Console.WriteLine("Received: {0}", Msg)
End While

Open in new window