title: Is there a good way to convert between BitmapSource and Bitmap?
body: <p>As far as I can tell the only way to convert from BitmapSource to Bitmap is through unsafe code... Like this (from <a href="http://blogs.msdn.com/llobo/archive/2007/03/08/bitmapsource-bitmap-interop.aspx" rel="noreferrer">Lesters WPF blog</a>):</p>
  
  <pre><code>myBitmapSource.CopyPixels(bits, stride, 0);
  
  unsafe
  {
    fixed (byte* pBits = bits)
    {
        IntPtr ptr = new IntPtr(pBits);
  
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(
          width,
          height,
          stride,
          System.Drawing.Imaging.PixelFormat.Format32bppPArgb,ptr);
  
        return bitmap;
    }
  }
  </code></pre>
  
  <p>To do the reverse:</p>
  
  <pre><code>System.Windows.Media.Imaging.BitmapSource bitmapSource =
    System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
      bitmap.GetHbitmap(),
      IntPtr.Zero,
      Int32Rect.Empty,
      System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  </code></pre>
  
  <p>Is there an easier way in the framework? And what is the reason it isn't in there (if it's not)? I would think it's fairly usable.</p>
  
  <p>The reason I need it is because I use AForge to do certain image operations in an WPF app. WPF wants to show BitmapSource/ImageSource but AForge works on Bitmaps.</p>