Let's say I have an image in a CBitmap with a color that signifies a transparent pixel (in my case it's magenta). Can I create a HRGN or a CRgn for the image that includes all the pixels except the magenta ones?
Asked
Active
Viewed 623 times
2 Answers
0
The only way to do this is to scan over the image, row at a time, scanning pixels and keep on combining pixel ranges into a HRGN object.
You will want to do this with a DIBSection as calling GetPixel is rather slow.

Chris Becke
- 34,244
- 12
- 79
- 148
-
Speed is not a problem, this is done only once and the image is small. How do I combine pixel ranges? – sashoalm Feb 24 '11 at 12:39
-
I just found the [CreateRegionFromFile](http://www.codeproject.com/KB/GDI/coolrgn.aspx?msg=940898) article in codeproject :) – sashoalm Feb 24 '11 at 14:29
0
At its simplest, you need to call CreateRectRgn
multiple times, passing a single-pixel rectangle for each magenta pixel. You'd then combine these regions together using CombineRgn
.
Obvious optimisations include:
- using a DIB section, rather than
GetPixel
to scan the original image. - looking for single-row runs of the same pixel, so that you're not combining 1 x 1 regions.
- looking for multi-row blocks of the same pixel, so that you're not combining 1 x n regions.
That said: why do you need an HRGN? Could you get away with just transforming the original bitmap into a mask bitmap?

Roger Lipscombe
- 89,048
- 55
- 235
- 380