Solved

send image via tcp/ip

Posted on 2008-10-30
3
2,202 Views
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()
        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

0
Comment
Question by:carl5121
3 Comments
 
LVL 19

Accepted Solution

by:
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.
0
 

Author Closing Comment

by:carl5121
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.
0
 
LVL 1

Expert Comment

by:agoud
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

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculate number of nights between two dates 5 53
Where is this file? 3 25
Adding items to a C# list incrementally 5 30
Configure BGP 22 11
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

861 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