title: Get bitmap from .DLL, convert to byte[] and to image
body: <p>I have received an .DLL for testing purposes, this .dll includes the functionality that will later be used to process live images from hardware.</p>
  
  <p>For this simple .dll, I can open an image (load into memory), get the width and height, and get the pixels which need to be converted to a image. Loading, getting width and getting height is fine, but getting the pixels and converting that to a Bitmap or Image is a problem.</p>
  
  <p>C++ example source that I received:</p>
  
  <pre class="lang-cc prettyprint-override"><code>   ApiFunc-&gt;OpenImageFile(this-&gt;OpenPictureDialog1-&gt;FileName.c_str());
     ApiFunc-&gt;AllocMemory();
  
     w = ApiFunc-&gt;GetImageWidth();
     h = ApiFunc-&gt;GetImageHeight();
     Image1-&gt;Width = w;
     Image1-&gt;Height = h;
  
     unsigned char* ptr = ApiFunc-&gt;GetImagePixels();
     COLORREF pixel;
     int r,g,b;
     for(int y=0; y&lt;w; y++)
     {
         for(int x=0; x&lt;h; x++)
         {
             r = (int)*(ptr+3*x);
             g = (int)*(ptr+3*x+1);
             b = (int)*(ptr+3*x+2);
  
             Image1-&gt;Canvas-&gt;Pixels[y][x] = RGB(r,g,b);
         }
         ptr += 3*h;
     }
  </code></pre>
  
  <p>Where in ApiFunc, this can be found:</p>
  
  <pre class="lang-cc prettyprint-override"><code>void __fastcall TAPIFunc::LoadDll(HINSTANCE m_hMain)
  {
     //some others above
     GET_IMAGE_PIXELS  = (func_GET_IMAGE_PIXELS   )GetProcAddress( m_hMain, "GET_IMAGE_PIXELS");
     //some others below
  }
  unsigned char* __fastcall TAPIFunc::GetImagePixels(void)
  {
     return GET_IMAGE_PIXELS();
  }
  </code></pre>
  
  <p>So now what I have tried so far, I've tried using byte[] as return parameter, but that throw an MarshalDirectiveException.</p>
  
  <pre class="lang-cs prettyprint-override"><code>    [DllImport("ImageTest.dll")]
      public static extern IntPtr GET_IMAGE_PIXELS();
  
      private void OpenImage(string filename)
      {
          OPEN_IMAGE_FILE(filename);
          ALLOC_MEMORY();
          int width = GET_IMAGE_WIDTH(); //=800
          int height = GET_IMAGE_HEIGHT(); //=600
          IntPtr buffer = GET_IMAGE_PIXELS();
          int size = width * height * 3;//not sure what the size must be, I think this is one of the issues, just following logic of one answer below.
  
          //but source: https://stackoverflow.com/a/16300450/2901207
          byte[] bitmapImageArray = new byte[size];
          Marshal.Copy(buffer, bitmapImageArray, 0, size);
  
          Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
          BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
          IntPtr pNative = bmData.Scan0;
          Marshal.Copy(imageData, 0, pNative,size);
          bitmap.UnlockBits(bmData);
          bitmap.Save(Environment.CurrentDirectory + @"\result.bmp");
              using (var ms = new MemoryStream(bitmapImageArray))
              {
                  //both throw exception: Parameter is not valid
                  Bitmap bmp = new Bitmap(ms);
                  Image bitmapImage = Image.FromStream(ms);
              }
      }
  </code></pre>
  
  <p>answer sources:</p>
  
  <p><a href="https://stackoverflow.com/a/16300450/2901207">answer 1, using bitmap.LockBits method</a>
  <a href="https://stackoverflow.com/questions/21555394/how-to-create-bitmap-from-byte-array?lq=1">answers 2 and 3, using memoryStream</a></p>
  
  <p>Just to make sure I have a valid image to test with, I saved an image in photoshop, with this option:
  <a href="https://i.stack.imgur.com/LZMSe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LZMSe.png" alt="photoshoppie"></a></p>
  
  <p>ugly test image:</p>
  
  <p><a href="https://i.stack.imgur.com/sFoKT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sFoKT.png" alt="rotated example image"></a></p>
  
  <p>result:
  <a href="https://i.stack.imgur.com/742BG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/742BG.png" alt="awesome result"></a></p>
  
  <p>Beautiful isn't it? :)</p>
  
  <p>Also tried using a for-loop, and run until it crashes. ran until count = 1441777 and on another image count = 1527793 (same dimensions).</p>
  
  <pre class="lang-cs prettyprint-override"><code>        int count = 0;
          for (int i = 0; i &lt; width * height * 4; i++)
          {
              count++;
              bitmapImageArray[i] = Marshal.ReadByte(buffer, i);
          }
  </code></pre>