title: Converting an opencv image cv::Mat Format to a C# BitmapImage
body: <p>I wrote this sample code to explain my problem. I have a solution in VS 2013, contains one C# project and a C++ project. I try to read an image with OpenCV in C++ (x86). and want to pass in to a C# x86 project (used CLR mode) to a Bitmap Object and Then BitmapImage Object to use as a WPF ImageSource.<br>
  My C++ Code:  </p>
  
  <pre><code>Bitmap^ SomeClass::Test(System::String^ imgFileName)
  {
      auto fileName = msclr::interop::marshal_as&lt;string&gt;(imgFileName);
      Mat img = imread(fileName);
      //Do something
      auto bmp = gcnew Bitmap(img.cols, img.rows, img.step, Imaging::PixelFormat::Format24bppRgb, (IntPtr)img.data);
      bmp-&gt;Save("InC++Side.png");     
      return bmp;
  }
  </code></pre>
  
  <p>My C# Code:</p>
  
  <pre><code>private void ImageTester(object sender, RoutedEventArgs e)
  {
      var image = testClass.Test("test.png");
      image.Save("InC#Side.png");
      bg.Source = ConvertToBitmapImageFromBitmap(image);
  }
  public static BitmapImage ConvertToBitmapImageFromBitmap(Bitmap image)
  {
      using(var ms = new MemoryStream())
      {
          image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
          BitmapImage bImg = new BitmapImage();
          bImg.BeginInit();
          bImg.StreamSource = new MemoryStream(ms.ToArray());
          bImg.EndInit();
          return bImg;
      }
  }
  </code></pre>
  
  <p>Problem is that the file saved by C++ (InC++Side.png) is perfect; but the other one which is presented the Bitmap object in C# is just a gray rectangle with that image's Height and Width.<br>
  Where is the problem?<br>
  How I can pass the Image to my C# project?</p>