Groups | Blog | Home
all groups > dotnet general > june 2005 >

dotnet general : Error when trying to pull a jpeg out of a stream..


bull.enteract NO[at]SPAM rcn.com
6/4/2005 3:02:24 PM
Ok, I start off with a bitmap image. I encode it as a jpeg and send it
across the network and pick it up on the other end. Now I have the
jpeg image on the other end as an arrray of bytes and I want to display
it in a picturebox control. How the heck do I convert this byte[] into
a bitmap?

Here is the pertinent code I use to convert it to a jpeg on the server
and send it to client..

....Bitmap workerBmp = new Bitmap(bmp);
MemoryStream memStream = new MemoryStream();
workerBmp.Save(memStream, ImageFormat.Jpeg);
byte[] byteData = memStream.ToArray();
networkStream.Write(byteData, 0, byteData.Length);

So now I pick up the jpeg data on the client and that's where I am
stuck.. how do I get this data into a form that a picturebox can
understand?
I read the data...

while ((bytesRead = networkStream.Read(dataBuffer, totalBytesRead,
dataBuffer.Length - totalBytesRead)) < (dataBuffer.Length -
totalBytesRead))
totalBytesRead += bytesRead;

Now what? I tried dropping the buffer into a MemoryStream and just
instantiating a Bitmap from the stream...i.e..
MemoryStream memStream = new MemoryStream(dataBuffer);
Bitmap bmp = new Bitmap(memStream);
//Image img = Image.FromStream(memStream); Same result if I use this

I get the following Exception..
An unhandled exception of type 'System.ArgumentException' occurred in
system.drawing.dll
Additional information: Invalid parameter used.

A point in the right direction please..
bull.enteract NO[at]SPAM rcn.com
6/4/2005 6:37:35 PM
Ok, I've narrowed the problem down to the fact that I am converting it
to a jpeg image. If I change it to ImageFormat.Bmp then it works
fine.. sooo, I have this jpeg data that I have sent over the wire.. how
do I work with it on the other end? I thought that Image.FromStream
would work with jpeg data but it doesn't seem to... also, if I send the
jpeg image to a file on the server workerBmp.Save("C:\\pic.jpeg",
ImageFormat.Jpeg) it saves as a valid jpeg image, so I'm pretty sure
the data coming across is valid....I'm pretty confused.. there must be
a way to "decode" the jpeg data on the client...
Jon Skeet [C# MVP]
6/5/2005 6:04:10 AM
[quoted text, click to view]

<snip>

You haven't shown where you're specifying dataBuffer's length. Is it
the exact size of the data you'll be receiving?

Are the server and the client both PCs, or is one a PocketPC or
similar?

If you try to convert byteData on the server side back into a JPEG,
does that work?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Eyal Safran
6/5/2005 2:55:11 PM
works great:

private byte[] jpegArray;

public Class1()
{
InitializeComponent();
Text = "Form1";
FileStream fs = File.OpenRead(@"C:\pic.jpg");
jpegArray = new byte[fs.Length];
fs.Read(jpegArray, 0, (int)fs.Length);
fs.Close();
}

private void Class1_Load(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream(jpegArray);
Bitmap bmp = (Bitmap)System.Drawing.Bitmap.FromStream(ms);
BackgroundImage = bmp;
}

Eyal.
AddThis Social Bookmark Button