473,549 Members | 2,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(bu ffer , 0, buffer .Length); //now
my picture is in buffer

System.IO.Strea mWriter sw = new
System.IO.Strea mWriter(ns); //
sw.WriteLine(st ream.Length.ToS tring()); // I
send first the picture's size
sw.Flush();
ns.Write(buffer , 0, buffer.Lenght); //Send
picture

client side:

stream = client.GetStrea m();
streamReader = new System.IO.Strea mReader(stream) ;

string size = streamReader.Re adLine(); //Get picture size

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

Image img = Image.FromStrea m(ImageDataStre am ); 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 12058
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.comwrot e in message
news:11******** **************@ q19g2000prn.goo glegroups.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(bu ffer , 0, buffer .Length); //now
my picture is in buffer

System.IO.Strea mWriter sw = new
System.IO.Strea mWriter(ns); //
sw.WriteLine(st ream.Length.ToS tring()); // I
send first the picture's size
sw.Flush();
ns.Write(buffer , 0, buffer.Lenght); //Send
picture

client side:

stream = client.GetStrea m();
streamReader = new System.IO.Strea mReader(stream) ;

string size = streamReader.Re adLine(); //Get picture size

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

Image img = Image.FromStrea m(ImageDataStre am ); 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.F ormatters.Binar y;

sender:

BinaryFormatter binfmt = new BinaryFormatter ();
binfmt.Serializ e (stream, img);

receiver:

BinaryFormatter binfmt = new BinaryFormatter ();
Image img = binfmt.Deserial ize (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
6932
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 questions about the implementation of Python sockets. (both methods use blocking sockets) Method 1: Calls socket.sendall(data) for each device in...
1
3778
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 after i send data into it? I simply want to open socket, send data, close socket and have the server just handle one client thread to recieve...
4
8193
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 TcpClient, NetworkStream and StreamWriter classes. but with low level socket it doesn't work (When using the Socket class Send method).
3
11319
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 necessary. I am writing some python to replace proprietary software that talks to a timeclock via UDP. The timeclock extracts the sending port from the...
1
8160
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 the members of my organization. I think my problem is incorrectly setting the settings on my server or an authentication problem. Here is the code I...
3
4273
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 receiver is running within Process1. If I move the receiver into Process2 then its fast. Please can someone explain.
0
3149
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 System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Runtime.Serialization.Formatters.Binary;
2
2347
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 Drawing.Bitmap("1.bmp", False) Return a End Function
2
6061
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 System.Net.NetworkCredential("user", "password"); try {
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7956
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1935
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.