Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

windows - C++/Win32: How to get the alpha channel from an HBITMAP?

I have an HBITMAP containing alpha channel data. I can successfully render this using the ::AlphaBlend GDI function.

However, when I call the ::GetPixel GDI function, I never get back values with an alpha component. The documentation does say that it returns the RGB value of the pixel.

Is there a way to retrieve the alpha channel values for pixels in an HBITMAP?

I want to be able to detect when to use ::AlphaBlend, and when to use an old-school method for treating a particular colour in the source HBITMAP as transparent.


HDC sourceHdc = ::CreateCompatibleDC(hdcDraw);
::SelectObject(sourceHdc, m_hbmp);

// This pixel has partial transparency, but ::GetPixel returns just RGB.
COLORREF c = ::GetPixel(sourceHdc, 20, 20);

// Draw the bitmap to hdcDraw
BLENDFUNCTION bf1;
bf1.BlendOp = AC_SRC_OVER;
bf1.BlendFlags = 0;
bf1.SourceConstantAlpha = 0xff;  
bf1.AlphaFormat = AC_SRC_ALPHA;            
::AlphaBlend(di.hdcDraw, x, 10, 64, 64, sourceHdc, 0, 0, 64, 64, bf1);

::DeleteDC(sourceHdc);

Answer

Use GetDIBits to retrieve the first (or more) scan line(s) of the image:

  byte* bits[1000];// = new byte[w * 4]; 
  BITMAPINFO bmi;
  memset(&bmi, 0, sizeof(BITMAPINFO)); 
  bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  bmi.bmiHeader.biWidth = w;
  bmi.bmiHeader.biHeight = -h;
  bmi.bmiHeader.biBitCount = 32;
  bmi.bmiHeader.biPlanes = 1;
  bmi.bmiHeader.biCompression = BI_RGB;
  bmi.bmiHeader.biSizeImage     = 0;
  bmi.bmiHeader.biXPelsPerMeter = 0;
  bmi.bmiHeader.biYPelsPerMeter = 0;
  bmi.bmiHeader.biClrUsed       = 0;
  bmi.bmiHeader.biClrImportant  = 0;
  int rv = ::GetDIBits(sourceHdc1, m_hbmp, 0, 1, (void**)&bits, &bmi, DIB_RGB_COLORS);

  //bits[3] == alpha of topleft pixel;

  //delete[] bits;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use GetDIBits. That way you get an array of RGBQUAD's which have as you can probably guess an alpha channel next to the R, G and B components.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...