使用WIC将QImage编码为png

Encode QImage to png using WIC?

本文关键字:png 编码 QImage WIC 使用      更新时间:2023-10-16

我正在尝试测试WIC和QT的编码函数之间的性能。但我找不到一种使用WIC将QImage(QT对象)编码为PNG的方法。

WIC的编码器需要从IStream初始化,IStream应该提供与图像文件相同的数据。但我有点像位图。

有人能帮我吗?如有任何意见,我们将不胜感激。

这很容易做到。我不知道如何使用编码器。IWICBitmapFrameEncode.WritePixels是我应该放置原始像素的位置:

factory = CreateComObject(CLSID_WICImagingFactory);
IWICBitmapEncoder pngEncoder = factory.CreateEncoder(GUID_ContainerFormatPng, null);
pngEncoder.Initialize(targetStream, WICBitmapEncoderNoCache);
IPropertyBag2 propertyBag;
IWICBitmapFrameEncode frame = pngEncoder.CreateNewFrame(ref propertyBag);
frame.Initialize(propertyBag);
//Set the size of the image
frame.SetSize(640, 480);
//Request the encoder use this format.
Guid format = GUID_WICPixelFormat32bppPBGRA;
//If the encoder doesn't support that format, it will set format to the closest it does support
frame.SetPixelFormat(ref format);
//if the frame encoder doesn't support the pixel format we want to give it
//then we just cannot proceed
Assert(format == GUID_WICPixelFormat32bppPBGRA); 
frame.WritePixels(
     lineCount, 
     stride, 
     bufferSize, 
     pointerToPixelData);
frame.Commit();
pngEncoder.Commit();