C++ 使用(MFC';s)CImage::SetPixel()更改像素的颜色

C++ 使用(MFC';s)CImage::SetPixel()更改像素的颜色,c++,graphics,bitmap,mfc,cimage,C++,Graphics,Bitmap,Mfc,Cimage,我有一个带有alpha(透明)层的32位png文件。我想使用MFC在每个像素的基础上更改一些像素的颜色。性能不是问题(尽管速度越快越好) 我编写代码调用CImage::GetPixel()调整返回的COLORREF,并SetPixel()新颜色,但整个图像是透明的。所以我写了下面的代码块,它只是获取和设置原始颜色。生成的图像是完全透明的。我还尝试简单地使用SetPixel(x,y,RGB(255,0,0))将所有像素设置为红色。有什么建议可以解决这个问题吗 CImage image; if(im

我有一个带有alpha(透明)层的32位png文件。我想使用MFC在每个像素的基础上更改一些像素的颜色。性能不是问题(尽管速度越快越好)

我编写代码调用
CImage::GetPixel()
调整返回的
COLORREF
,并
SetPixel()
新颜色,但整个图像是透明的。所以我写了下面的代码块,它只是获取和设置原始颜色。生成的图像是完全透明的。我还尝试简单地使用
SetPixel(x,y,RGB(255,0,0))
将所有像素设置为红色。有什么建议可以解决这个问题吗

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);
CImage图像;
if(image.Load(sFilename)==S_确定)
{
跟踪(L“IsTransparencySupported%d”,image.IsTransparencySupported());//返回1。
跟踪(L“IsDIBSection%d”,image.IsDIBSection());//返回1。
跟踪(L“大小%dx%d”,image.GetWidth(),image.GetHeight());//显示141x165。
跟踪(L“BPP%d”,image.GetBPP());//返回32。
跟踪(L“Pitch%d”,image.GetPitch());//返回-564。
COLORREF color;
对于(int x=0;x
谢谢

CImage图像;
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];  
    }
}

对于(int i=0;它的
COLORREF
宏似乎不支持alpha,因此alpha始终为零。请尝试
SetPixel(x,y,color | 0xff000000)。如果这样的话,你可能想用自己的宏来定义非零alpha的颜色常量。如果它不起作用,可能是 StEngult太愚蠢了,你可能想考虑在由@ PAD <代码> COLREFF 返回的缓冲区中设置像素完全能够代表alpha通道。大部分情况下,GDI不准备支持alpha透明。比如,它不会(也不能更改为)返回alpha透明信息,因为它保留了
0xffffff
CLR\u INVALID
)作为一个特殊的错误值。我不确定
SetPixel
是否支持alpha透明度,原因也是一样的,尽管可能。