title: byte[] array from unmanaged memory
body: <p>I'm writing a simple .NET wrapper to C/C++ Pylon library for Basler cameras under linux (Ubuntu) in monodevelop. I'm building a .so (.dll) file in Code::Blocks and P/Invoke it in monodevelop.
  I have two simple tasks: get a single image and get a sequence of images.</p>
  
  <p>I managed the first part in this way:</p>
  
  <p>in C++ i have a function:</p>
  
  <pre><code>void GetImage(void* ptr)
  {
      CGrabResultPtr ptrGrabResult;
      //here image is grabbing to ptrGrabResult
      camera.GrabOne(5000,ptrGrabResult,TimeoutHandling_ThrowException);
      //i'm copying byte array with image to my pointer ptr
      memcpy(ptr,ptrGrabResult-&gt;GetBuffer(),_width*_height);
  
      if (ptrGrabResult-&gt;GrabSucceeded())
          return;
      else
          cout&lt;&lt;endl&lt;&lt;"Grab Failed;"&lt;&lt;endl;
  }
  </code></pre>
  
  <p>and in C#:</p>
  
  <pre><code>[DllImport("libPylonInterface.so")]
  private static extern void GetImage(IntPtr ptr);
  
  public static void GetImage(out byte[] arr)
  {
      //allocating unmanaged memory for byte array
      IntPtr ptr = Marshal.AllocHGlobal (_width * _height);
      //and "copying" image data to this pointer
      GetImage (ptr);
  
      arr = new byte[_width * _height];
      //copying from unmanaged to managed memory
      Marshal.Copy (ptr, arr, 0, _width * _height);
      Marshal.FreeHGlobal(ptr);
  }
  </code></pre>
  
  <p>and after that i can build an image from this <code>byte[] arr</code>.</p>
  
  <p>I have to copy a huge amount of bytes twice (1. in c++ <code>memcpy()</code>; 2. in c# <code>Marshal.Copy()</code>). I tried to use a direct pointer to image buffer <code>ptr = ptrGrabResult -> GetBuffer()</code>, but get a mono environment error, when marshalling bytes.</p>
  
  <p>So here is a question: is it a fine solution? Am i going in right direction?</p>
  
  <p>P.S. Give me please some advices how should i manage with image sequence?</p>