By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

c# sending a bitmap by socket

P: n/a
hello,
I have a client/server application. the server capture picture from
webcam and send it to every client connected to it.the network part
works good and the capture from webcam too. I associate an event when
a capture is done, then every frame of the webcam should be sent to
the client.
but I cannot find a way to send bitmap throught network, of course I
found many method from internet, some doen't work, some work but it's
never stable.
here what I do:

server side:
NetworkStream ns = cl.GetStream(); //cl is my
tcpClient for one connection with client

e.Save(stream, tt[1], encodeurs); // stream is
a MemoryStream
stream.Position = 0;
byte[] buffer = new byte[stream.Length];
stream.Write(buffer , 0, buffer .Length); //now
my picture is in buffer

System.IO.StreamWriter sw = new
System.IO.StreamWriter(ns); //
sw.WriteLine(stream.Length.ToString()); // I
send first the picture's size
sw.Flush();
ns.Write(buffer , 0, buffer.Lenght); //Send
picture

client side:

stream = client.GetStream();
streamReader = new System.IO.StreamReader(stream);

string size = streamReader.ReadLine(); //Get picture size

byte[] buffer = new byte[Convert.ToInt32(size)]; //create
buffer with good size
int nRead = stream.Read(buffer, 0, buffer.Length); //read
the picture
System.IO.MemoryStream ImageDataStream = new
System.IO.MemoryStream();
ImageDataStream.Read(buffer, 0, nRead );

Image img = Image.FromStream(ImageDataStream ); Get the
picture from memoryStream

This method doesn't work good and is not stable.

Is it possible by an other method to skip to send the picture size?

What is the real and best method that works, never failed, to send
picture from webcam by socket in c#?
I'm sure it's possible to skip sending the size but I didn't find any
methods that works!

thanks a lot

Jun 7 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
You don't send the size. You send the data in chunks and read it in chunks.
When there's no more to be read, the end of the file has been reached. The
Stream.Read method returns the number of bytes received.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"david" <da***********@hotmail.comwrote in message
news:11**********************@q19g2000prn.googlegr oups.com...
hello,
I have a client/server application. the server capture picture from
webcam and send it to every client connected to it.the network part
works good and the capture from webcam too. I associate an event when
a capture is done, then every frame of the webcam should be sent to
the client.
but I cannot find a way to send bitmap throught network, of course I
found many method from internet, some doen't work, some work but it's
never stable.
here what I do:

server side:
NetworkStream ns = cl.GetStream(); //cl is my
tcpClient for one connection with client

e.Save(stream, tt[1], encodeurs); // stream is
a MemoryStream
stream.Position = 0;
byte[] buffer = new byte[stream.Length];
stream.Write(buffer , 0, buffer .Length); //now
my picture is in buffer

System.IO.StreamWriter sw = new
System.IO.StreamWriter(ns); //
sw.WriteLine(stream.Length.ToString()); // I
send first the picture's size
sw.Flush();
ns.Write(buffer , 0, buffer.Lenght); //Send
picture

client side:

stream = client.GetStream();
streamReader = new System.IO.StreamReader(stream);

string size = streamReader.ReadLine(); //Get picture size

byte[] buffer = new byte[Convert.ToInt32(size)]; //create
buffer with good size
int nRead = stream.Read(buffer, 0, buffer.Length); //read
the picture
System.IO.MemoryStream ImageDataStream = new
System.IO.MemoryStream();
ImageDataStream.Read(buffer, 0, nRead );

Image img = Image.FromStream(ImageDataStream ); Get the
picture from memoryStream

This method doesn't work good and is not stable.

Is it possible by an other method to skip to send the picture size?

What is the real and best method that works, never failed, to send
picture from webcam by socket in c#?
I'm sure it's possible to skip sending the size but I didn't find any
methods that works!

thanks a lot

Jun 7 '07 #2

P: n/a
On Thu, 07 Jun 2007 03:52:35 -0700, david wrote:
I have a client/server application. the server capture picture from
webcam and send it to every client connected to it.the network part
works good and the capture from webcam too. I associate an event when
a capture is done, then every frame of the webcam should be sent to
the client.
but I cannot find a way to send bitmap throught network, of course I
found many method from internet, some doen't work, some work but it's
never stable.
[...]
This method doesn't work good and is not stable.
What do you mean with "doesn't work good" and "not stable"? The piece of
code you gave us contains for too many references to undeclared variables.
I can't make any sense of it. It looks overly complicated to me as well.
Is it possible by an other method to skip to send the picture size?
If you don't first send the picture size, how could the receiver possibly
know how many bytes it has to read before it has read the entire picture?
The only way to not send the picture size first (by picture size you
actually mean the number of bytes formed by the picture's data) would be to
design your network protocol so that a particular sequence of bytes
appended at the end of the picture's data signals that the picture is
complete. This would make the implementation of the receiver side more
complex and probably less efficient. Plus given that the picture data can
potentially contain any combination of bytes, you'd need to make sure that
it doesn't contain your End-Of-Image byte sequence which adds further
complexity.
What is the real and best method that works, never failed, to send
picture from webcam by socket in c#?
There's no "real" and "best" method to send pictures over a socket. The
first thing that you should do is sit down and design a suitable network
protocol. If you have never designed a network protocol before, I would
suggest that you download the specifications of some network protocol and
see what it looks like (the VNC network protocol is a very simple and easy
to understand protocol. The documentation is well written too so that could
be a good starting point <http://www.realvnc.com/products/personal/4.2/>).

In your particular case, if all you want is send images from the server to
the client, your protocol is probably going to be most basic. Something
like that for example:

Server Sends:
MessageLength Type Description
4 bytes UInt32 (BigEndian) ImageDataSize
ImageDataSize byte array Image Data

Then use your NetworkStream Send or BeginSend and Read or BeginRead methods
to implement the protocol. If your webcam image grabber code raises events
in worker threads, make sure that you do not start sending the next image
while you are still in the process of sending the previous one.
Jun 7 '07 #3

P: n/a
On Thu, 7 Jun 2007 07:23:39 -0400, Kevin Spencer wrote:
You don't send the size. You send the data in chunks and read it in chunks.
When there's no more to be read, the end of the file has been reached. The
Stream.Read method returns the number of bytes received.
That probably won't work all that well if images are sent over the socket
in quick succession which it what the OP appears to be doing.
Jun 7 '07 #4

P: n/a
Why not use the .NET serialization? I don't know for sure if you are
using a standard .NET Bitmap (think you do) but they are serializable.

using System.Runtime.Serialization.Formatters.Binary;

sender:

BinaryFormatter binfmt = new BinaryFormatter ();
binfmt.Serialize (stream, img);

receiver:

BinaryFormatter binfmt = new BinaryFormatter ();
Image img = binfmt.Deserialize (stream) as Image;

with 'stream' your network stream and 'img' the image you're
sending/receiving.

This let's .NET take care of all the details of sending the size and the
data and reconstructing the image for you.

To be honest, I didn't try it for sending across a network but it should
work. This is after all the whole point of the .NET serialization. :-)

For more info see:
http://msdn2.microsoft.com/en-us/library/7ay27kt9.aspx

-- Freddy
david wrote:
hello,
I have a client/server application. the server capture picture from
webcam and send it to every client connected to it.the network part
works good and the capture from webcam too. I associate an event when
a capture is done, then every frame of the webcam should be sent to
the client.
but I cannot find a way to send bitmap throught network, ...
[snip]
Jun 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.