Jump to content
  • Advertisement
Sign in to follow this  
Lode

drawing image in GDI with color key

This topic is 3659 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;

  ip_dc->SetMapMode(MM_TEXT);
  ip_dc->SetStretchBltMode(COLORONCOLOR);        

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

  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!
  /*StretchBlt(ip_dc->GetSafeHdc(), 
                x, y,
                width, height,
                m_hdc,
                0, 0,
                width, height,
                SRCCOPY);*/

//NOTHING AT ALL is visible after this, does it not work to go from the CreateCompatibleDC to the CDC* or something???
  TransparentBlt(ip_dc->GetSafeHdc(), 
                x, y,
                width, height,
                m_hdc,
                0, 0,
                width, height,
                0x00000000);
}




Share this post


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

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!