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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bull.enteract@rcn.com

    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.Jpe g);
    byte[] byteData = memStream.ToArr ay();
    networkStream.W rite(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.R ead(dataBuffer, totalBytesRead,
    dataBuffer.Leng th - totalBytesRead) ) < (dataBuffer.Len gth -
    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(da taBuffer);
    Bitmap bmp = new Bitmap(memStrea m);
    //Image img = Image.FromStrea m(memStream); Same result if I use this

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

    A point in the right direction please..

  • bull.enteract@rcn.com

    #2
    Re: Error when trying to pull a jpeg out of a stream..

    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.FromStrea m
    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.jp eg",
    ImageFormat.Jpe g) 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...

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Error when trying to pull a jpeg out of a stream..

      <bull.enteract@ rcn.com> wrote:[color=blue]
      > 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..[/color]

      <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.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Eyal Safran

        #4
        Re: Error when trying to pull a jpeg out of a stream..

        works great:

        private byte[] jpegArray;

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

        private void Class1_Load(obj ect sender, System.EventArg s e)
        {
        MemoryStream ms = new MemoryStream(jp egArray);
        Bitmap bmp = (Bitmap)System. Drawing.Bitmap. FromStream(ms);
        BackgroundImage = bmp;
        }

        Eyal.

        Comment

        Working...