Luis
Luis

Reputation: 31

C# Video Streaming

I'm trying to do a application with video stream, and by now I can send only one image from the server to the client. When I try to send more than only one image at the client I receive the following error: "Parameter is not valid." at pictureBox1.Image = new Bitmap(ms);

Client side code:

while((data = cliente.receiveImage()) != null)
{

   ms = new MemoryStream(data);
   pictureBox1.Image = new Bitmap(ms);
   ms.Close();

}

Server side code (this code is repeated continuously):

servidor.sendImage(ms.GetBuffer());

Upvotes: 3

Views: 1026

Answers (2)

David
David

Reputation: 73554

Images are nit-picky things, and you have to have the entire set of bytes that comprise the image in order to reconstruct an image.

I would bet my left shoe that the issue is that when the client object is receiving data, it's getting it in chunks comprised of partial images, not the whole image at once. This would cause the line that says

pictureBox1.Image = new Bitmap(ms);

to fail because it simply doesn't have a whole image's bytes.

Alternatives

  • Rather than having the server push images out to the client, perhaps another approach would be to have the client pull images from the server.

  • Use an existing streaming mechanism. I personally think that streaming video manually from C# may be more complex than you're bargaining for, and I'd humbly recommend using an existing component or application to stream the video rather than writing your own. There are already so many different options out there (wmv, Flash, and a hundred more) that you're reinventing a wheel that really doesn't need to be re-invented.

Upvotes: 1

SLaks
SLaks

Reputation: 887195

ms.GetBuffer() returns the entire buffer of the memory stream, including any extra unused portion.
You should call ToArray(), which only returns actual contents.

(Or, your data might be invalid for some other reason, such as an issue in sendImage or receiveImage)

Upvotes: 3

Related Questions