1

I have a legacy MCF application that displays some images (bmp 32-bits with alpha channel information) by pre-multiplying the images and using CDC::AlphaBlend method.

I would like to introduce some new graphics using Direct2D but I don't want to migrate all the images to png or other formats.

I managed to draw a bmp image from a file but I'm facing problems to get the image from resources and also the displayed image does not use the alpha channel information.

So could anybody help me out with this?

This is my code to create the bitmap:

hr = pIWICFactory->CreateDecoderFromFilename(   L"D:\\image.bmp", 
                                                NULL, 
                                                GENERIC_READ, 
                                                WICDecodeMetadataCacheOnDemand, 
                                                &pDecoder);
if (SUCCEEDED(hr))
{
    // Create the initial frame.
    hr = pDecoder->GetFrame(0, &pSource);
}

if (SUCCEEDED(hr))
{
    //create a Direct2D bitmap from the WIC bitmap.
    hr = pRenderTarget->CreateBitmapFromWicBitmap(
        pSource,
        NULL,
        ppBitmap
        );

}

This is the code to draw the bitmap:

m_pRenderTarget->DrawBitmap(
    m_pBitmap,
    D2D1::RectF(0.0f, 0.0f, size.width, size.height)
    );
4

1 回答 1

1

您需要从资源中创建一个 IStream 以传递给 IWICImagingFactory::CreateDecoderFromStream。

由于资源在内存中可用(假设加载了包含它们的模块),最简单的方法是使用 IWICImagingFactory::CreateStream 创建一个 IWICStream 对象并使用 IWICStream::InitializeFromMemory 对其进行初始化。

要获取资源的大小和指向第一个字节的指针,请使用 FindResource、LoadResource、LockResource 和 SizeofResource 函数。

如果您的位图标题使用 BI_BITFIELDS 指定带有 alpha 数据的格式,我相信 WIC 会尊重这一点。我对 Direct2D 没有任何经验,所以我不能说您是否需要进一步做任何事情以使其使用 alpha 数据。

如果您不能使用 BI_BITFIELDS(或者如果这不起作用),您可以编写自己的 IWICBitmapSource 实现来包装框架的 IWICBitmapSource。您应该能够将大多数调用直接传递给帧源,并提供您自己的 GetPixelFormat 方法来返回图像数据的真实格式。或者,您可以创建具有所需格式的 IWICBitmap,锁定位图,然后从帧源复制像素数据。

于 2012-03-13T03:38:00.267 回答