【发布时间】:2019-02-09 23:55:36
【问题描述】:
我不使用 c++ 或 MFC/ATL 等,所以假设我什么都不知道。
整个情况是,我需要接受一个带有透明层的 PNG,并将其写为具有指定压缩的 JPEG,以传递给另一个系统。
在这里我想知道的是,如何用纯白色初始化目标 CImage 结构?
到目前为止我已经这样做了(是的,我知道它还有其他风格问题)
void ClipBoardFuncs::savePNGASJPEG(char filePath[256], char errorBuff[256]) {
int sizeOfErrorBuff = 256;
CImage imagePNG;
CImage imageJPG;
int xPNG, yPNG = 0;
imagePNG.Load(filePath);
xPNG = imagePNG.GetWidth();
yPNG = imagePNG.GetHeight();
//Create my target JPG same size and bit depth specifiying that there is no alpha channel (dwflag last param)
imageJPG.Create(xPNG, yPNG, imagePNG.GetBPP(), 0);
//Let there be white....
for (int x = 0; x <= xPNG; x++)
{
for (int y = 0; y <= yPNG; y++)
{
imageJPG.SetPixelRGB(x, y, 255, 255, 255);
}
}
//Copy the image over onto on the white
//BitBlt copies everything....
//imagePNG.BitBlt(imageJPG.GetDC(), 0, 0);
//Draw is more like the C# draw examples I've seen
imagePNG.Draw(imageJPG.GetDC(), 0, 0);
imageJPG.ReleaseDC();
HRESULT hr = NULL;
hr = imageJPG.Save(filePath, Gdiplus::ImageFormatJPEG);
imagePNG.Destroy();
imagePNG.ReleaseGDIPlus();
imageJPG.Destroy();
imageJPG.ReleaseGDIPlus();
LPCTSTR error = _com_error(hr).ErrorMessage();
strncpy_s(errorBuff, sizeOfErrorBuff, _com_error(hr).ErrorMessage(), _TRUNCATE);
}
可爱的 C# 人有这样的答案:
Convert Transparent PNG to JPG with Non-Black Background Color
但我需要 c++ MFC 解决方案才能用作 DLL 中的导出函数。
我所说的导出函数是指与您在 kernel32.dll 中找到的架构相同的架构 - 抱歉,我不知道用什么术语来区分这种 DLL 和填充了 COM 对象的 DLL。
谁能建议一种比我在这里使用的嵌套 x/y for 循环更快的方法来将 imageJPEG 结构初始化为纯白色?
干杯
4GLGuy
【问题讨论】:
标签: c++ mfc png atl alpha-transparency