title: saving to bitmap with win32 API or GDI+
body: <p>Question is rather simple but I couldn't find nice &amp; clean solution to my problem on the Internet.
  What I got are some drawings on my window. Now, I can save those using BitBlt function from window device context to image device context, and also from there to bitmap handle:</p>
  
  <pre><code>HDC bitmapDC = CreateCompatibleDC(dc);
  HBITMAP bitmap = CreateCompatibleBitmap(bitmapDC, 200, 200);
  SelectObject(bitmapDC,bitmap);
  BitBlt(bitmapDC, 0, 0, 200, 200, dc, 200, 200, SRCCOPY);
  </code></pre>
  
  <p>But from there I'm lost. I had a look at GDI+ Bitmap class which got save function, and I found how to implement code for retrieving CLSID of picture encoding. However I don't know if I use loading to that class correctly. There's overloaded constructor for HBITMAP, but it's also asking for some palette, which I set to NULL:</p>
  
  <pre><code>Bitmap image(bitmap,NULL);
  </code></pre>
  
  <p>I tried to save png file but it resulted in black quare without those drawings I was expecting. If you'd like, full code for my painting procedure:</p>
  
  <pre><code>void GetCLSID(const WCHAR* format, CLSID* pClsid){
      UINT  num = 0;          // number of image encoders
      UINT  size = 0;         // size of the image encoder array in bytes
  
      ImageCodecInfo* pImageCodecInfo = NULL;
  
      GetImageEncodersSize(&amp;num, &amp;size);
  
      pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
  
      GetImageEncoders(num, size, pImageCodecInfo);
  
      for(UINT j = 0; j &lt; num; ++j)
      {
        if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
        {
           *pClsid = pImageCodecInfo[j].Clsid;
           free(pImageCodecInfo);
        }    
      }
  }
  
  void OnPaint(HDC dc){
      RECT rect; rect.bottom = 0; rect.top = 20; rect.left = 0; rect.right = 100;
      HBRUSH blueBrush = CreateSolidBrush(RGB(0,0,200));
      FillRect(dc, &amp;rect, blueBrush);
  
      Graphics graphics(dc);
      Pen      pen(Color(255, 0, 0, 255));
      graphics.DrawLine(&amp;pen, 0, 0, 200, 100);
  
      SolidBrush greenBrush(Color(0,200,0));
      Rect ellipseRect(20,20,20,20);
      graphics.FillEllipse(&amp;greenBrush, ellipseRect);
  
      SolidBrush redBrush(Color(200,0,0));
      Rect boxRectangle(0,40,20,100);
      graphics.FillRectangle(&amp;redBrush, boxRectangle);
  
      pen.SetColor(Color(200,0,200));
      pen.SetWidth(20);
      graphics.DrawBezier(&amp;pen, 100, 20, 130, 40, 200, 10, 230, 20);
  
      HDC bitmapDC = CreateCompatibleDC(dc);
      HBITMAP bitmap = CreateCompatibleBitmap(bitmapDC, 200, 200);
      SelectObject(bitmapDC,bitmap);
      BitBlt(bitmapDC, 0, 0, 500, 500, dc, 500, 500, SRCCOPY);
  
      Bitmap image(bitmap,NULL);
      CLSID clsID;
      GetCLSID(L"image/png", &amp;clsID);
      image.Save(L"pic.png", &amp;clsID);
  
  }
  </code></pre>
  
  <p>I couldn't even imagine that simple saving will be such problem, so I'll be glad for any help, thanks!</p>