title: Convert a Byte array to Image in c# after modifying the array
body: <p>I'm trying to convert a byte[] to an image in C#. I know this question has been asked on different forums. But none of the answers given on them helped me. To give some context=
  I open an image, convert it to a byte[]. I encrypt the byte[]. In the end I still have the byte[] but it has been modified ofc. Now I want to display this again. The byte[] itself consists of 6559 bytes. I try to convert it by doing :</p>
  
  <pre><code> public Image byteArrayToImage(byte[] byteArrayIn)
      {
          MemoryStream ms = new MemoryStream(byteArrayIn);
          Image returnImage = Image.FromStream(ms);
          return returnImage;
      }
  </code></pre>
  
  <p>and I get this error: Parameter is not valid.</p>
  
  <p>The byte array is constructed by using the .toArray() on a List</p>
  
  <pre><code>List&lt;byte&gt; encryptedText = new List&lt;byte&gt;();    
  pbEncrypted.Image = iConverter.byteArrayToImage(encryptedText.ToArray())
  </code></pre>
  
  <p>;</p>
  
  <p>Can anyone help me? Am I forgetting some kind of format or something ? </p>
  
  <p>The byte that has to be converted to an image :
  <img src="https://i.stack.imgur.com/EzANS.png" alt="alt text"></p>
  
  <pre><code> private void executeAlgoritm(byte[] plainText)
      {
  
          // Empty list of bytes
          List&lt;byte&gt; encryptedText = new List&lt;byte&gt;();
  
          // loop over all the bytes in the original byte array gotten from the image
          foreach (byte value in plainText)
          {
              // convert it to a bitarray
              BitArray myBits = new BitArray(8); //define the size
  
              for (byte x = 0; x &lt; myBits.Count; x++)
              {
                  myBits[x] = (((value &gt;&gt; x) &amp; 0x01) == 0x01) ? true : false;
              }
  
              // encrypt the bitarray and return a byte
              byte bcipher = ConvertToByte( sdes.IPInvers(sdes.FK(sdes.Shift(sdes.FK(sdes.IP(myBits),keygen.P8(keygen.shift(keygen.P10(txtKey.Text))))),keygen.P8(keygen.shift(keygen.shift(keygen.shift(keygen.P10(txtKey.Text))))))));
  
              // add the byte to the list
              encryptedText.Add(bcipher);
  
          }
          // show the image by converting the list to an array and the array to an image
          pbEncrypted.Image = iConverter.byteArrayToImage(encryptedText.ToArray());
      }
  </code></pre>