title: Display a byte array in a pictureBox in C#
body: <p>I read a lot of questions and answers, most suggest:</p>
  
  <pre><code>byte[] byteArray; //(contains image data)
  MemoryStream stream = new MemoryStream(byteArray);
  Bitmap image = new Bitmap(stream);
  pictureBox.Image = image;
  </code></pre>
  
  <p>or more directly:</p>
  
  <pre><code>pictureBox.Image = Image.FromStream(stream);
  </code></pre>
  
  <p>I always get :"An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll</p>
  
  <p>Additional information: Parameter is not valid."</p>
  
  <p>in regards to the stream parameter.</p>
  
  <p>Even in the case where:</p>
  
  <pre><code>byte[] byteArray = new byte[1];
  byteArray[0] = 255;
  </code></pre>
  
  <p>I can't figure out why.</p>
  
  <p>EDIT:</p>
  
  <p>I get the data from a file like this:</p>
  
  <pre><code>//byteArray is defined as List&lt;byte&gt; byteArray = new List&lt;byte&gt;();
  TextReader tr = new StreamReader(file);
  string File = tr.ReadToEnd();
  string[] bits = File.Split('\t');
  List&lt;string&gt; image = new List&lt;string&gt;(bits);
  height = int.Parse(bits[0]);
  width  = int.Parse(bits[1]);
  image.RemoveRange(0, 2);
  image.RemoveAt(image.Count - 1);
  foreach (string s in image)
  {
    byteArray.Add(byte.Parse(s));
  }
  return byteArray //(i do .ToArray() in the MemoryStream call);
  </code></pre>
  
  <p>In the debugger, I see i.e. that the byteArray is fine, count = 2244, values everywhere, etc.</p>
  
  <p>EDIT #2: Sample data file (first byte[0] is height, second byte[1] is width, rest is RGB data)</p>
  
  <pre><code>47 15 12 55 25 52 55 25 52 55 25 52 55 25 52 55
  25 52 55 25 52 55 25 52 55 25 52 55 25 52 55 25
  52 55 25 52 55 25 52 55 25 52 55 25 52 55 25 52
  55 25 52 55 25 52 55 25 52 55 25 52 55 25 52 55
  25 52 51 24 82 49 24 82 49 24 92 50 25 12 50 24
  92 48 24 92 50 24 82 50 25 02 50 24 92 50 25 02
  51 25 12 50 24 92 49 25 02 50 25 02 49 25 12 49
  25 02 49 25 02 47 25 12 47 25 22 50 24 82 47 24
  82 50 24 72 50 24 82 49 24 82 50 24 72 50 24 82
  50 24 72 49 24 82 49 25 22 52 24 92 50 24 82 50
  24 72 47 25 00 etc.
  </code></pre>
  
  <p>EDIT #3: SOLUTION</p>
  
  <pre><code>Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
  BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
  IntPtr ptr = bmpData.Scan0;
  Marshal.Copy(byteArray, 0, ptr, height * width * 3);
  bmp.UnlockBits(bmpData);
  pictureBox.Image = bmp;
  </code></pre>
  
  <p>Need to check for 4 byte alignment, so loading function is now:</p>
  
  <pre><code>TextReader tr = new StreamReader(file);
  string File = tr.ReadToEnd();
  string[] bits = File.Split('\t');
  List&lt;string&gt; image = new List&lt;string&gt;(bits);
  height = int.Parse(bits[0]);
  width  = int.Parse(bits[1]);
  int falseBits = 0;
  int oldWidth = width;
  while (width % 4 != 0)
  {
      width++;
      falseBits++;
  }
  int size = height * width * 3;
  byte[] byteArray = new byte[size];
  Parallel.For(0, size - 1, i =&gt; byteArray[i] = 255);
  int index  = 0;
  int lineIndex = 0;
  image.RemoveRange(0, 2);
  image.RemoveAt(image.Count - 1);
  foreach (string s in image)
  {
      byteArray [index]   = byte.Parse(s);
      byteArray [index + 1] = byteArray [index];
      byteArray [index + 2] = byteArray [index];
      index +=3;
      lineIndex++;
      if (lineIndex == oldWidth)
      {
          lineIndex = 0;
          index += 3*falseBits;
      }
  }
  return byteArray ;
  </code></pre>