title: Parameter is not valid error when creating image from byte[] in c#
body: <p>I am trying to convert a <code>byte[]</code> to <code>Bitmap</code> in c#. Following is the code:</p>
  
  <pre><code>MemoryStream ms = new MemoryStream(b);
  Bitmap bmp = new Bitmap(ms);
  </code></pre>
  
  <p>It shows the error <code>Parameter is not valid</code> when creating the <code>Bitmap</code>.</p>
  
  <p><code>byte[] b</code> is coming from a network stream.</p>
  
  <p>But when I write this byte[] to a file, and open this file in any image viewer just works perfectly. Following is code for writing the byte[] to file:</p>
  
  <pre><code> var fs = new BinaryWriter(new FileStream("tmp.bmp", FileMode.Create, FileAccess.Write));
   fs.Write(b);
   fs.Close();
  </code></pre>
  
  <p>What am I missing here?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><strong>Here is my full code that was causing problem</strong></p>
  
  <pre><code> Socket s = listener.AcceptSocket();
   byte[] b = new byte[imgLen];
   s.Receive(b);
   MemoryStream ms = new MemoryStream(b);
   // now here I am using ms.Seek(0, SeekOrigin.Begin); that fixed my problem.
   Bitmap bmp = new Bitmap(ms);
   pictureBox1.Image = bmp;
   s.Close();
  </code></pre>
  
  <p>I am using this code on <code>Form_Load</code> event and there is nothing extra. I am just trying to display an Image that is streamed on network. The server is written in Java that is streaming this image.</p>
  
  <p>Hope it clarifies the doubts.</p>
  
  <p>Thanks</p>