使用(MFC 的)CImage::SetPixel() 改变像素的颜色
Changing the color of pixels using (MFC's) CImage::SetPixel()
我有一个带有 alpha(透明)层的 32 位 png 文件。我想使用 MFC 在每个像素的基础上更改 some 像素的颜色。性能不是问题(尽管越快越好)。
我编写代码调用 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 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];
}
}
我有一个带有 alpha(透明)层的 32 位 png 文件。我想使用 MFC 在每个像素的基础上更改 some 像素的颜色。性能不是问题(尽管越快越好)。
我编写代码调用 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 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];
}
}