Jump to content
  • Advertisement
Sign in to follow this  

drawing image in GDI with color key

This topic is 3832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When trying to render an image with color key in GDI in Windows: Given: *) an unsigned long* buffer of RGB pixels *) width and height of it in pixels *) a CDC* or HDC *) a position (x, y) *) a color key value (pixels of this color should be transparent) How can I draw that buffer of pixels on that CDC* or HDC such that the pixels in the unsigned long* buffer that have the color key value are NOT drawn? In other words, I simply want to draw an image with transparency (not blending, just 1 transparent color) on a surface in GDI. This without DirectDraw or anything, just GDI and things like StretchDIBits and stuff. The following code fails to do that, WHY?
void DrawTexture(CDC* ip_dc,
                 const unsigned long* ip_pixels,
                 size_t width,
                 size_t height,
                 int x,
                 int y)
  BITMAPINFO  bitmapinfo;
  ZeroMemory(&bitmapinfo, sizeof(bitmapinfo));
  bitmapinfo.bmiHeader.biSize = sizeof(bitmapinfo.bmiHeader);
  bitmapinfo.bmiHeader.biWidth       = (long)width;
  bitmapinfo.bmiHeader.biHeight      = (long)height;
  bitmapinfo.bmiHeader.biBitCount    = 32;
  bitmapinfo.bmiHeader.biPlanes      = 1;
  bitmapinfo.bmiHeader.biCompression = BI_RGB;


  HDC m_hdc = ::CreateCompatibleDC(NULL);
  if (m_hdc == NULL)

  SetMapMode(m_hdc, MM_TEXT);
  SetStretchBltMode(m_hdc, COLORONCOLOR);        
  StretchDIBits(m_hdc, //if I put here ip_dc->GetSafeHdc(), it's rendered (really visible) but without the color key
                0, 0,
                width, height,
                0, 0,
                width, height,
                ip_pixels, &bitmapinfo, DIB_RGB_COLORS, SRCCOPY);    

  //even this instead of the TransparentBlt does NOTHING!
                x, y,
                width, height,
                0, 0,
                width, height,

//NOTHING AT ALL is visible after this, does it not work to go from the CreateCompatibleDC to the CDC* or something???
                x, y,
                width, height,
                0, 0,
                width, height,

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!