472,778 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,778 software developers and data experts.

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

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
3 2404
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
<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
0
by: CroDude | last post by:
Hi all! I have problems when writting bitmap to a byte array and after reading it back form byte to Bitmap object. What I do is this: First I throw Bitmap to a memory-stream and then I write it...
3
by: PB | last post by:
As part of an ASP.NET WEb Application I have a routine (relevant portion is below) that lets users rotate a photo (jpg or gif). The routine works just fine if it is run once. If run a second time...
3
by: T. Davis | last post by:
In C#, I am able to successfully stream a TIFF image that comes from a BLOB field in a database, save it to file, then convert the pages within TIFF file into jpegs (using GDI+) and display on the...
2
by: Richard | last post by:
I each time get errors when I use an .bmp or ,.gif file as source but whenever I use a .jpg file it works perfect. Why is that? and how to fix it? I almost have no knowledge about the...
3
by: bull.enteract | last post by:
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...
1
by: krishna5 | last post by:
My php program to resize an jpeg image is as follows, ?php // File and new size $filename = '\bala\testPHP\Lavanya\Kids2.jpeg'; //$filename = 'Kids2.jpeg'; $percent = 0.5; // Content type
3
by: tshad | last post by:
oThumbnail.Save is giving me an error: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. theFileName = New FileInfo(strFileName) fileOut =...
1
by: Alexander Higgins | last post by:
>>Thanks for the response.... Point Taken but this is not the case. Thus, if a person writes a text file on her or his computer and does not use UNICODE to save it, the current code page is...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.