Byte Array to Bitmap Image

arrays, bitmap, byte, c#

Solution

This is an alternative method

int w= 100;
int h = 200;
int ch = 3; //number of channels (ie. assuming 24 bit RGB in this case)

byte[] imageData    = new byte[w*h*ch]; //you image data here
Bitmap bitmap       = new Bitmap(w,h,PixelFormat.Format24bppRgb);
BitmapData bmData   = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr pNative      = bmData.Scan0;
Marshal.Copy(imageData,0,pNative,w*h*ch);
bitmap.UnlockBits(bmData);

Problem

I made this code to receive an image and convert it to bitmap image but it doesn't work. Here is the code: ``` public void ReceiveImage() { NetworkStream stream = new NetworkStream(socket); byte[] data = new byte[4]; stream.read(data,0,data.length,0) int size = BitConverter.ToInt32(data,0); data = new byte[size]; stream.read(data,0,data.length) MemoryStream imagestream = new MemoryStream(data); Bitmap bmp = new Bitmap(imagestream); picturebox1.Image = bmp; } ``` It gets to: ``` Bitmap bmp = new Bitmap(imagestream); ``` And gives me this error: Parameter is not valid

Original source