title: Converting from WriteableBitmap to Mat for then showing it in a window with CvInvoke.Imshow?
body: <p>I'm fetching infrared frames from a Kinect V2 camera. The frames come in the form of WritableBitmap, so I try to convert it with this code that I made: </p>
  
  <pre><code>public Mat WritableBitmapToMat(WriteableBitmap writeBmp)
  {
      Bitmap bmp;
      using (MemoryStream outStream = new MemoryStream())
      {
          BitmapEncoder enc = new BmpBitmapEncoder();
          enc.Frames.Add(BitmapFrame.Create((BitmapSource)writeBmp));
          enc.Save(outStream);
          bmp = new Bitmap(outStream);
      }
  
      Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
  
      BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
  
      IntPtr data = bmpData.Scan0;
  
      int step = bmpData.Stride;
  
      Mat mat = new Mat(bmp.Height, bmp.Width, Emgu.CV.CvEnum.DepthType.Cv32F, 4, data, step);
  
      bmp.UnlockBits(bmpData);
  
      return mat;
  }
  </code></pre>
  
  <p>I don't know if it's a problem with the conversion, but when I try to show the image in a window with this code:</p>
  
  <pre><code>CvInvoke.Imshow("window showing a picture", infraredMat);
  </code></pre>
  
  <p>I end up getting an exception that looks like this: </p>
  
  <p>Exception: Exception thrown: 'System.AccessViolationException' in Emgu.CV.World.dll ("Attempted to read or write protected memory. This is often an indication that other memory is corrupt.").</p>
  
  <p>I also want to mention I am using EmguCV 3.1 and Kinect SDK 2.0</p>
  
  <p>Thanks in advance for any help I get. </p>