title: WPF convert byte array from database to Image control source
body: <p>I have a <code>byte</code> array stored in an entity framework database. The list of objects is set to a <code>ListView</code> source. The list view has this template as its item template:</p>
  
  <pre><code>&lt;DataTemplate x:Key="ItemContentTemplate"&gt;
      &lt;Grid Height="auto" Margin="0,0,0,10"&gt;
          &lt;Image Width="auto" Height="auto" Source="{Binding InitialImage, Converter={StaticResource ResourceKey=ImageSourceConverter}}" Margin="10"/&gt;
      &lt;/Grid&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>I'm using this as a converter but I get this exception on EndInit: "No imaging component suitable to complete this operation was found." inner exception:{"Exception from HRESULT: 0x88982F50"} <code>System.Runtime.InteropServices.COMException</code></p>
  
  <pre><code>public class ImageSourceConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, CultureInfo cultureInfo)
      {
          if (value == null)
          {
              return new BitmapImage();
          }
  
          try
          {
              MemoryStream strmImg = new MemoryStream((byte[])value);
              BitmapImage myBitmapImage = new BitmapImage();
              myBitmapImage.BeginInit();
              myBitmapImage.StreamSource = strmImg;
              myBitmapImage.EndInit();
              return myBitmapImage;
          }
          catch (Exception ex)
          {
              string message = ex.Message;
          }
  
          return new BitmapImage();
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
          return null;
      }
  }
  </code></pre>
  
  <p>I'm not really sure what this means...</p>
  
  <p>EDIT:
  Ok here some more info. So I receive the raw data from a usb camera via directshow in the form of an Intptr from managed code. Then I copy it from a the pointer to a write able bitmap:</p>
  
  <pre><code>public static void CopyTo(this IBitmapImage image, WriteableBitmap writeableBitmap)
  {
     Kernel32.MoveMemory(
              writeableBitmap.BackBuffer,
              image.Data,
              Convert.ToUInt32(
                  writeableBitmap.PixelWidth *
                  writeableBitmap.PixelHeight *
                  (PixelFormats.Bgr24.BitsPerPixel / 8)));
      }
  </code></pre>
  
  <p>after that I convert the backbuffer of the writeable bitmap to a byte array and save it to the database. later on when the picture needs to be viewed I grabs it from the database.</p>
  
  <pre><code>var width = cameraOneBitmap.PixelWidth;
  var height = cameraOneBitmap.PixelHeight;
  var stride = width * ((cameraOneBitmap.Format.BitsPerPixel + 7) / 8);
  
  var bitmapData = new byte[height * stride];
  
  cameraOneBitmap.CopyPixels(bitmapData, stride, 0);
  </code></pre>