By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,727 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,727 IT Pros & Developers. It's quick & easy.

Error when trying to pull a jpeg out of a stream..

P: n/a
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..

Jul 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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...

Jul 21 '05 #2

P: n/a
<bu***********@rcn.com> wrote:
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..


<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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3

P: n/a
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.

Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.