ID2D1Bitmap 到 Wic 与 Alpha 但不是 PNG
ID2D1Bitmap to Wic with Alpha but not PNG
我正在保存 Direct2D 设备上下文的位图。我的目标是保持 32 位(带 alpha 的 RGB)但没有 PNG,我宁愿有 32 位位图。
我正在使用这个功能:
HRESULT SaveBitmapToStream(
_In_ CComPtr<ID2D1Bitmap1> d2dBitmap,
_In_ CComPtr<IWICImagingFactory2> wicFactory2,
_In_ CComPtr<ID2D1DeviceContext> d2dContext,
_In_ REFGUID wicFormat,
_In_ IStream* stream
)
{
// Create and initialize WIC Bitmap Encoder.
CComPtr<IWICBitmapEncoder> wicBitmapEncoder;
auto hr =
wicFactory2->CreateEncoder(
wicFormat,
nullptr, // No preferred codec vendor.
&wicBitmapEncoder
);
hr =
wicBitmapEncoder->Initialize(
stream,
WICBitmapEncoderNoCache
);
// Create and initialize WIC Frame Encoder.
CComPtr<IWICBitmapFrameEncode> wicFrameEncode;
hr =
wicBitmapEncoder->CreateNewFrame(
&wicFrameEncode,
nullptr // No encoder options.
);
if (FAILED(hr))
return hr;
hr =
wicFrameEncode->Initialize(nullptr);
if (FAILED(hr))
return hr;
// Retrieve D2D Device.
CComPtr<ID2D1Device> d2dDevice;
if (!d2dContext)
return E_FAIL;
d2dContext->GetDevice(&d2dDevice);
// Create IWICImageEncoder.
CComPtr<IWICImageEncoder> imageEncoder;
hr =
wicFactory2->CreateImageEncoder(
d2dDevice,
&imageEncoder
);
if (FAILED(hr))
return hr;
hr =
imageEncoder->WriteFrame(
d2dBitmap, wicFrameEncode,
nullptr // Use default WICImageParameter options.
);
if (FAILED(hr))
return hr;
hr = wicFrameEncode->Commit();
if (FAILED(hr))
return hr;
hr = wicBitmapEncoder->Commit();
if (FAILED(hr))
return hr;
// Flush all memory buffers to the next-level storage object.
hr =
stream->Commit(STGC_DEFAULT);
return hr;
}
问题是,当我传递 GUID_ContainerFormatBmp 时,生成的位图没有 alpha。我必须放 GUID_ContainerFormatPng,但这会压缩我不想要的图像,这是用于视频渲染,我不想进行任何压缩。
有没有办法以 32 位格式而不是压缩格式获取 Direct2D 上下文的捕获?
native BMP codec 支持 属性、EnableV5Header32bppBGRA
类型 VT_BOOL
:
Specifies whether to allow encoding data in the
GUID_WICPixelFormat32bppBGRA pixel format. If this option is set to
VARIANT_TRUE, the BMP will be written out with a BITMAPV5HEADER
header. The default value is VARIANT_FALSE.
Note for 16-bit and 32-bit Windows BMP files, the BMP codec ignores
any alpha channel, as many legacy image files contain invalid data in
this extra channel. Starting with Windows 8, 32-bit Windows BMP files
written using the BITMAPV5HEADER with valid alpha channel content are
read as WICPixelFormat32bppBGRA
我正在保存 Direct2D 设备上下文的位图。我的目标是保持 32 位(带 alpha 的 RGB)但没有 PNG,我宁愿有 32 位位图。
我正在使用这个功能:
HRESULT SaveBitmapToStream(
_In_ CComPtr<ID2D1Bitmap1> d2dBitmap,
_In_ CComPtr<IWICImagingFactory2> wicFactory2,
_In_ CComPtr<ID2D1DeviceContext> d2dContext,
_In_ REFGUID wicFormat,
_In_ IStream* stream
)
{
// Create and initialize WIC Bitmap Encoder.
CComPtr<IWICBitmapEncoder> wicBitmapEncoder;
auto hr =
wicFactory2->CreateEncoder(
wicFormat,
nullptr, // No preferred codec vendor.
&wicBitmapEncoder
);
hr =
wicBitmapEncoder->Initialize(
stream,
WICBitmapEncoderNoCache
);
// Create and initialize WIC Frame Encoder.
CComPtr<IWICBitmapFrameEncode> wicFrameEncode;
hr =
wicBitmapEncoder->CreateNewFrame(
&wicFrameEncode,
nullptr // No encoder options.
);
if (FAILED(hr))
return hr;
hr =
wicFrameEncode->Initialize(nullptr);
if (FAILED(hr))
return hr;
// Retrieve D2D Device.
CComPtr<ID2D1Device> d2dDevice;
if (!d2dContext)
return E_FAIL;
d2dContext->GetDevice(&d2dDevice);
// Create IWICImageEncoder.
CComPtr<IWICImageEncoder> imageEncoder;
hr =
wicFactory2->CreateImageEncoder(
d2dDevice,
&imageEncoder
);
if (FAILED(hr))
return hr;
hr =
imageEncoder->WriteFrame(
d2dBitmap, wicFrameEncode,
nullptr // Use default WICImageParameter options.
);
if (FAILED(hr))
return hr;
hr = wicFrameEncode->Commit();
if (FAILED(hr))
return hr;
hr = wicBitmapEncoder->Commit();
if (FAILED(hr))
return hr;
// Flush all memory buffers to the next-level storage object.
hr =
stream->Commit(STGC_DEFAULT);
return hr;
}
问题是,当我传递 GUID_ContainerFormatBmp 时,生成的位图没有 alpha。我必须放 GUID_ContainerFormatPng,但这会压缩我不想要的图像,这是用于视频渲染,我不想进行任何压缩。
有没有办法以 32 位格式而不是压缩格式获取 Direct2D 上下文的捕获?
native BMP codec 支持 属性、EnableV5Header32bppBGRA
类型 VT_BOOL
:
Specifies whether to allow encoding data in the GUID_WICPixelFormat32bppBGRA pixel format. If this option is set to VARIANT_TRUE, the BMP will be written out with a BITMAPV5HEADER header. The default value is VARIANT_FALSE.
Note for 16-bit and 32-bit Windows BMP files, the BMP codec ignores any alpha channel, as many legacy image files contain invalid data in this extra channel. Starting with Windows 8, 32-bit Windows BMP files written using the BITMAPV5HEADER with valid alpha channel content are read as WICPixelFormat32bppBGRA