我有一个 PixelFormat32bppARGB 格式的 Gdiplus::Bitmap(在 C++ 中),并使用以下方法将其转换为 PixelFormat32bppRGB:
Gdiplus::Bitmap* bitmapRGB = new Gdiplus::Bitmap(bitmap->GetWidth(), bitmap->GetHeight(), PixelFormat32bppRGB);
Gdiplus::Graphics graphics(bitmapRGB);
graphics.DrawImage(bitmap, Gdiplus::Point(0, 0));
如果我检查 RGB 位图的格式,它是正确的 (RGB):
bitmapRGB ->GetPixelFormat() == PixelFormat32bppRGB
如果我保存位图并检查其格式(即使用 Gimp),则存在第四个 channel 。
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
bitmapRGB ->Save(path, &pngClsid, NULL);
如何获得 RGB 像素格式的 png?
最佳答案
PixelFormat32bppRGB
仍然是 32 位。 24 位格式需要 PixelFormat24bppRGB
:
Gdiplus::Bitmap* bitmapRGB = new Gdiplus::Bitmap(
bitmap->GetWidth(), bitmap->GetHeight(), PixelFormat24bppRGB);
new
运算符不是必需的,使用适当的构造函数之一来确保没有内存泄漏:
//convert:
Gdiplus::Bitmap bitmap(L"source.jpg");
Gdiplus::Bitmap bitmapRGB(bitmap.GetWidth(), bitmap.GetHeight(), PixelFormat24bppRGB);
Gdiplus::Graphics graphics(&bitmapRGB);
graphics.DrawImage(&bitmap, Gdiplus::Point(0, 0));
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
bitmapRGB.Save(L"destination.png", &pngClsid);
关于c++ - 保存的 Gdiplus::Bitmap 总是有 Alpha channel (RGBA/ARGB),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50798340/