Search code examples
c++graphicsbitmapmfccimage

Changing the color of pixels using (MFC's) CImage::SetPixel()


I have a 32-bit png file with an alpha (transparent) layer. I want to change the color of some pixels on a per-pixel basis using MFC. Performance isn't an issue (although faster is better).

I wrote code to call CImage::GetPixel() tweak the returned COLORREF, and SetPixel() the new color, but the entire image was transparent. So I wrote the following block which simply gets and sets the original color. The resulting image is entirely transparent. I also tried simply using SetPixel(x, y, RGB(255, 0, 0)) to set all pixels to red. Any advice to resolve this?

CImage image;
if(image.Load(sFilename) == S_OK)
{
    TRACE(L"IsTransparencySupported %d", image.IsTransparencySupported()); // Returns 1.
    TRACE(L"IsDIBSection %d", image.IsDIBSection()); // Returns 1.
    TRACE(L"Size %dx%d", image.GetWidth(), image.GetHeight()); // Displays 141x165.
    TRACE(L"BPP %d", image.GetBPP()); // Returns 32.
    TRACE(L"Pitch %d", image.GetPitch()); // Returns -564.

    COLORREF color;
    for(int x = 0; x < image.GetWidth(); x++)
    {
        for(int y = 0; y < image.GetHeight(); y++)
        {
            color = image.GetPixel(x, y); 
            image.SetPixel(x, y, color);
        }
    }

    if(image.Save(sFilenameNew, Gdiplus::ImageFormatPNG) != S_OK)
        TRACE(L"Error saving %s.", sFilenameNew);
}
else
    TRACE(L"Error loading png %s.", sFilename);

Thanks!


Solution

  • CImage image;
    for (int i=0;i<image.ImgHeight;i++)
    {
        for (int j=0;j<image.ImgWidth;j++)
        {
            int index = i*image.ImgWidth+j;
            unsigned char* pucColor = reinterpret_cast<unsigned char *>      (image.GetPixelAddress(j , i)); 
            pucColor[0] = bValues[index];   
            pucColor[1] = gValues[index];   
            pucColor[2] = rValues[index];  
        }
    }