Can I create a HRGN/CRgn by excluding all the magenta pixels in a given image?

652 views Asked by At

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?

2

There are 2 answers

2
Chris Becke On

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.

0
Roger Lipscombe On

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?