title: OpenCV create Mat from byte array
body: <p>In my C++ dll I am creating Mat from byte array:</p>
  
  <pre><code>BYTE * ptrImageData;  //Image data is in this array passed to this function
  
  Mat newImg = Mat(nImageHeight, nImageWidth, CV_8UC3, ptrImageData);
  </code></pre>
  
  <p>The image is created with some gray shade not the original one.</p>
  
  <p>Is this the proper way of creating Mat from byte array?</p>
  
  <p>Please see code </p>
  
  <p>ptrImageData is passed to the C++ dll from C# code.</p>
  
  <p>C# code to pass the image data</p>
  
  <pre><code>System.Drawing.Image srcImage //Has the image
  MemoryStream ms = new MemoryStream(); 
  Marshal.FreeHGlobal(ptrImageData); 
  srcImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
  byte[] imgArray = ms.ToArray();
  ms.Dispose();
  
  
  int size1 = Marshal.SizeOf(imgArray[0]) * imgArray.Length;
  IntPtr ptrImageData = Marshal.AllocHGlobal(size1);
  Marshal.Copy(imgArray, 0, ptrImageData, imgArray.Length);
  
  //Calling C++ dll function
  ProcessImage(ptrImageData, srcImage.Width, srcImage.Height);
  
  Marshal.FreeHGlobal(ptrImageData);
  </code></pre>