473,386 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

c# sending a bitmap by socket

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
4 12026
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tim Black | last post by:
My application requires sending a large piece (~2MB) of data to several devices on a network via TCP sockets. I have experimented with different methods for doing this and this has raised some...
1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
4
by: yaron | last post by:
Hi, I have a problem when sending data over TCP socket from c# client to java server. the connection established ok, but i can't send data from c# client to java server. it's work ok with...
3
by: Sells, Fred | last post by:
I'm using MSW XP Pro with Python 2.4 to develop but production will be Linux with Python 2.3. (could upgrade to 2.4 if absolutely necessary) I can also switch to Linux for development if...
1
by: Eric Sheu | last post by:
Greetings, I have been searching the web like mad for a solution to my SMTP problem. I am using Windows Server 2003 and ASP.NET 2.0 w/ C# to send out e-mails from a web site I have created to...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
2
by: =?Utf-8?B?UHJ6ZW1v?= | last post by:
Hi, I would like to have a web service method sending a bitmap image. But code like do not work: <WebMethod()_ Public Function GetBitmap() As Drawing.Bitmap Dim a As New...
2
by: Danny | last post by:
Hi all, Trying to send mail with System.Net.SmtpClient, using very simple code just for testing: SmtpClient smtp = new SmtpClient("mail.server.com", 25); smtp.Credentials = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.