Sending Images through a TCP connection

  • Thread starter Thread starter Juan Romero
  • Start date Start date
J

Juan Romero

Hey guys,

I am trying to send an image through a TCP connection. What is the best way
to accomplish this?

Thank you.
 
Hey guys,

I am trying to send an image through a TCP connection. What is the best way
to accomplish this?

Use the TcpListener class to listen for incoming connections. Use
TcpClient to connect to a remote host (that is listening for a
connection). Once connected, transmit your picture (using a FileStream,
MemoryStream, etc...). The receiver can use Image.FromStream() to load
the data directly from the stream, although you might want to buffer the
data in a MemoryStream first to be able to handle timeouts and
connection problems yourself.
 
* "Juan Romero said:
I am trying to send an image through a TCP connection. What is the best way
to accomplish this?

Load the image into a byte array and send the byte array:

\\\
Dim fs As FileStream = New FileStream( _
"C:\WINDOWS\Angler.bmp", _
FileMode.Open _
)
Dim br As BinaryReader = New BinaryReader(fs)
Dim abyt() As Byte = br.ReadBytes(fs.Length)
br.Close()
///

On the other side, you can reconstruct the image from the received byte array:

\\\
Dim ms As New MemoryStream(abyt)
Dim img As Image = Image.FromStream(ms)
///
 
(e-mail address removed) (Herfried K. Wagner [MVP]) wrote in
Load the image into a byte array and send the byte array:

If you use Indy, which is free, you can just call WriteFile, or WriteStream.
You can skip marshalling it around, or even loading it into memory.

http://www.indyproject.org/
 
Thanks for replying everyone.
Ok, I guess I should have been more specific.

What I am trying to send is a bitmap, and I DO NOT want to write to disk
first. I have the bitmap object, and I want to send it to the receiver. I
got the bitmap by taking a snapshot from the screen.
I am using a TCPClient. The problem I have is writing the bitmap to the
stream. The stream takes a buffer as input, so how do I get a byte buffer
from a bitmap object?

I have tried serialization too, but it gives me an error. If you know how to
do this with serialization, please write.

Thanks!
 
Back
Top