Getting image data in CRgn object in MFC

Usually we come across the situations , where we need to represent the image pixels as an single entity. In VC++ we can make out the image data represented by single entity using its CRgn class provided in MFC.
Bitmap image;//Read from some source, may be you read it from disk or you take it from some other that depends .

int image_width = image.getWidth();
int image_height = image.getHeight();
 
 std::vector vecRect; // vector of rectangles

    for(int j = 0; j < image_width; j++)
    {
        for(int k = 0; k < image_height; k++)
        {
           
            int jTranspA =  get the alpha value of current pixel(i,j)
            if(jTranspA != 0)
            {
                RECT rct;
                rct.left = j;
                rct.top = k;
                rct.right = j + 1;
                rct.bottom = k + 1;
                vecRect.push_back(rct);
            }
        }
    }
 
CRgn rgn;
 
//size of memory to allocate
DWORD size = vecRect.size() * sizeof(RECT) +  sizeof(RGNDATAHEADER);
 

            RGNDATA *rgndata = NULL;

            //allocate  the memory
            rgndata = (RGNDATA *) HeapAlloc( GetProcessHeap(), 0, size);
 
            if(!rgndata)
                return;

            RECT* pRectInitial = (RECT*)rgndata->Buffer;
            RECT rectBounding = vecRect[0];
 
//feeding the rectangles into the buffer of rgndata
            for (int i = 0; i < vecRect.size(); i++)
            {
                RECT rectCurrent      = vecRect[i];
                rectBounding.left     = min(rectBounding.left, rectCurrent.left);
                rectBounding.right    = max(rectBounding.right, rectCurrent.right);
                rectBounding.top      = min(rectBounding.top, rectCurrent.top);
                rectBounding.bottom   = max(rectBounding.bottom, rectCurrent.bottom);

                *pRectInitial = vecRect[i];
                pRectInitial++;
            }
 
//preparing rgndata header
            RGNDATAHEADER  header;
            header.dwSize = sizeof(RGNDATAHEADER);
            header.iType  = RDH_RECTANGLES;
            header.nCount = vecRect.size();
            header.nRgnSize = vecRect.size() * sizeof(RECT);
            header.rcBound.left = rectBounding.left;
            header.rcBound.top  = rectBounding.top;
            header.rcBound.right = rectBounding.right;
            header.rcBound.bottom = rectBounding.bottom;

            rgndata->rdh = header;
 
//creating the region from rgndata
            rgn.CreateFromData(NULL, size, rgndata);
            HeapFree(GetProcessHeap(), 0, rgndata);
 
 
//so at the end u have the CRgn object have the informations of pixels presented in image
// note that CRgn object is just a region, it will not have the colour information of the pixels of image.
// you can use this CRgn object , if you want to fill the specific portion in an image with some color ,by taking the graphics of image.
150 150 Burnignorance | Where Minds Meet And Sparks Fly!