send image via tcp/ip

Posted on 2008-10-30
Last Modified: 2013-11-26
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()


        Client = Listener.AcceptTcpClient

        Stream = Client.GetStream()


        w = New BinaryWriter(Stream)

        r = New BinaryReader(Stream)


        If r.ReadString() = "RequestConnect" Then





            Loop Until r.ReadString() = "Disconnect"


        End If



    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)




        If r.ReadString() = "Connected" Then


 ''HERES THE PROBLEM. how do i determine the amount of bytes to read

    Form1.PictureBox1.Image = byteArrayToImage(r.ReadBytes(126643))



            Loop Until r.ReadString() = "Disconnected"

        End If


    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

Question by:carl5121
LVL 19

Accepted Solution

drichards earned 250 total points
ID: 22846127
The best way to do it would be to send a header indicating the number of bytes to expect.  That way you can be sure to get them all.  Without that, the best you can do is to have a fixed size buffer (say 1024 bytes) and use the Read method.  You don't need the binary reader for this as it exists on the stream itself.

After doing a read, you transfer the bytes to a MemoryStream and then read again.  You read until no bytes are received for some period of time.  The problem is that you cannot predict the correct wait time as it might range from milliseconds to many seconds, or the data may not show up at all due to network conditions.  The most robust way to do it will be to send the image size in bytes ahead of the image data.  You'll need to make the header a fixed size so you know where the header ends and the image data starts.

Author Closing Comment

ID: 31511682
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.

Expert Comment

ID: 25328194
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


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: (…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now