lorenzo vinco

lorenzo vinco

  • NA
  • 14
  • 3.3k

Streaming byte data into a bitmap

Jun 7 2022 1:01 PM

Hi everyone!

I'm programming a camera and I am finding issues with streaming data I have stored into a Byte array into a picturebox. I used the syntax I found also on several examples online and it seems very reasonable to me, but the following error is prompted: System.Argument.Exception: 'Invalid parameter'. 

Thank you for your kind answers.

Here is the code:

        private Bitmap array2bitmap(int[] last_image, int cols, int rows)
        {
            // create image to be displayed from the row data;
            Bitmap newBitmap;
            byte[] imagebytes = new byte[cols * rows];

            for (int j = 0; j < rows; j++)
            {
                for (int i = 0; i < cols; i++)
                {

                    imagebytes[i + j * cols] = (byte) (last_image[i + j * cols] >> 4 & 0xFF);
                }
            }
            Stream ms = new MemoryStream(imagebytes);
            newBitmap = new Bitmap(ms);        //error is here
             return newBitmap;
        }


Answers (2)