send images through TCP

Apr 13, 2011 at 3:36 AM

Hi,

im trying to show image stream that i'm sending through network (TCP).

im sending the images from winforms application.

on every frame that is generated i do this

....

MemoryStream ms = new MemoryStream();
someBItmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
streamByte = ms.ToArray();//streambyte is byte[]
client.Client.BeginSend(streamByte, 0, streamByte.Length, SocketFlags.None, new AsyncCallback(OnDataSend), null);

...

but when i receive the image in the silverlight application, here is part of the code:

...

MemoryStream ms = new MemoryStream();
ms.Write(e.Buffer, 0, e.BytesTransferred);
ExtendedImage extImage = new ExtendedImage();
extImage.LoadingFailed += new EventHandler<UnhandledExceptionEventArgs>(extImage_LoadingFailed);
extImage.LoadingCompleted += new EventHandler(extImage_LoadingCompleted);
extImage.SetSource(ms);

...

private void extImage_LoadingCompleted(object sender, EventArgs e)
{
ExtendedImage ei = (ExtendedImage)sender;
Dispatcher.BeginInvoke(() =>
{
                WriteableBitmap wb = ei.ToBitmap();
                WebcamCapture.Source = wb;// this is an image control
});

}

and it raises and exception

ExceptionObject = {ImageTools.IO.UnsupportedImageFormatException: Exception of type 'ImageTools.IO.UnsupportedImageFormatException' was thrown.
at ImageTools.ExtendedImage.Load(Stream stream)
at ImageTools.ExtendedImage.<>c__DisplayClass18.<LoadAsync>b__17(Object obj...

------------------------------------

the images that im sending, i also saved them before sending, and they look fine.

if i try to open them with filestream, then they will appear in the image control.

i the exception is raised at the source of the ExtendedImage.

 

thanks in advance.

Coordinator
Apr 14, 2011 at 5:11 PM
This is a common mistake:

If you write to a stream:

MemoryStream ms = new MemoryStream();
ms.Write(e.Buffer, 0, e.BytesTransferred);

the position of the stream is at the end, so you have to reset the
position first:

ms.Position = 0;

then you can read from it.