title: Loading WPF Image control from byte array
body: <p>Hey, I am trying to load an <code>Image</code> control from a byte array, I've tried multiple solutions found online (particularly this site) but nothing seems to work.
  My main goal was to obtain an <code>ImageSource</code> from the byte array and return it from a converter.</p>
  
  <p>I've tried:</p>
  
  <pre><code>BitmapImage bi = new BitmapImage();
  bi.BeginInit();
  bi.StreamSource = new MemoryStream(lBytes);
  bi.EndInit();
  </code></pre>
  
  <p>But this fails with: </p>
  
  <blockquote>
    <p>NotSupportedException</p>
    
    <p>No imaging component suitable to complete this operation was found.</p>
  </blockquote>
  
  <p>Also tried first loading a <code>Bitmap</code> and from there try to get the <code>ImageSource</code>.</p>
  
  <pre><code>using (MemoryStream lMem = new MemoryStream(lBytes))
  {
          TypeConverter tc = TypeDescriptor.GetConverter(typeof(System.Drawing.Bitmap));
          System.Drawing.Bitmap b = (System.Drawing.Bitmap)tc.ConvertFrom(lBytes);
          lResult = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
              b.GetHbitmap(),
              IntPtr.Zero,
              System.Windows.Int32Rect.Empty,
              BitmapSizeOptions.FromEmptyOptions());
  
          b.Dispose();
  }
  </code></pre>
  
  <p>But this fails on the <code>ConvertFrom</code> with "Parameter is not valid."</p>
  
  <p>All of this when loading a valid PNG file in my filesystem.</p>
  
  <p>I am running out of ideas, any clue?</p>
  
  <p>Thanks.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Alright, the problem was my way of loading the file...</p>
  
  <p>I was using</p>
  
  <pre><code>using (FileStream lFileStream = new FileStream(pFilePath, FileMode.Open))
  {
      using (StreamReader lReader = new StreamReader(lFileStream))
      {
          BinaryFormatter bf = new BinaryFormatter();
          using (MemoryStream ms = new MemoryStream())
          {
              string lString = lReader.ReadToEnd();
              bf.Serialize(ms, lString);
              ms.Seek(0, 0);
              lImage = ms.ToArray();
          } 
          lResult = new Graphic(lImage);
      }
  }
  </code></pre>
  
  <p>But then read that I could use:</p>
  
  <pre><code>lImage = File.ReadAllBytes(pFilePath);
  </code></pre>
  
  <p>And that's it.</p>
  
  <p>Thank you.</p>