title: Draw on DeviceContext from COLORREF[]
body: <p>I have a pointer to a COLORREF buffer, something like: <code>COLORREF* buf = new COLORREF[x*y];</code></p>
  
  <p>A subroutine fills this buffer with color-information. Each COLORREF represents one pixel. </p>
  
  <p><strong>Now I want to draw this buffer to a device context.</strong> My current approach works, but is pretty slow (== ~200ms per drawing, depending on the size of the image):</p>
  
  <pre><code>for (size_t i = 0; i &lt; pixelpos; ++i) 
  {
      // Get X and Y coordinates from 1-dimensional buffer.
      size_t y = i / wnd_size.cx;
      size_t x = i % wnd_size.cx;
      ::SetPixelV(hDC, x, y, buf[i]);
  }
  </code></pre>
  
  <p><strong>Is there a way to do this faster; all at once, not one pixel after another?</strong><br>
  I am not really familiar with the GDI. I heard about al lot of APIs like CreateDIBitmap(), BitBlt(), HBITMAP, CImage and all that stuff, but have no idea how to apply it. It seems all pretty complicated...<br>
  MFC is also welcome.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks in advance.  </p>
  
  <p>(Background: the subroutine I mentioned above is an OpenCL kernel - the GPU calculates an Mandelbrot image and safes it in the COLORREF buffer.)  </p>
  
  <p><strong>EDIT:</strong><br>
  Thank you all for your suggestions. The answers (and links) gave me some insight into Windows graphics programming. The performance is now acceptable (semi-realtime-scrolling into the Mandelbrot works :)<br>
  I ended up with the following solution (MFC):   </p>
  
  <pre><code>...
  CDC dcMemory;
  dcMemory.CreateCompatibleDC(pDC);
  
  CBitmap mandelbrotBmp;
  mandelbrotBmp.CreateBitmap(clientRect.Width(), clientRect.Height(), 1, 32, buf);
  
  CBitmap* oldBmp = dcMemory.SelectObject(&amp;mandelbrotBmp);
  pDC-&gt;BitBlt(0, 0, clientRect.Width(), clientRect.Height(), &amp;dcMemory, 0, 0, SRCCOPY);
  
  dcMemory.SelectObject(oldBmp);
  mandelbrotBmp.DeleteObject();
  </code></pre>
  
  <p>So basically  CBitmap::CreateBitmap() saved me from using the raw API (which I still do not fully understand). The example in the documentation of <a href="http://msdn.microsoft.com/de-de/library/kwxzck32.aspx" rel="nofollow noreferrer">CDC::CreateCompatibleDC</a> was also helpful.<br>
  My Mandelbrot is now blue - using SetPixelV() it was red. But I guess that has something to do with CBitmap::CreateBitmap() interpreting my buffer, not really important.  </p>
  
  <p>I might try the OpenGL suggestion because it would have been the much more logical choice and I wanted to try OpenCL under Linux anyway. </p>