Direct2D:将ID2D1Image转换为ID2D1Bitmap

时间:2015-06-29 14:32:14

标签: c++ image visual-c++ direct2d

我正在开发一个需要修改屏幕上已有内容的程序。 所以我只使用ID2D1Bitmap创建了pRenderTarget->CopyFromRenderTarget

我尝试做的是将效果应用于该位图。该效果仅返回ID2D1Image,但我需要ID2D1Bitmap

有办法做到这一点吗?

EDIT1:

ID2D1Bitmap* myBitmap //the bitmap I want to apply the effect to
ID2D1Effect* effect = NULL;
pDeviceContext->CreateEffect(CLSID_D2D1Saturation, &effect);
effect->SetValue(D2D1_SATURATION_PROP_SATURATION, 0.0f);
effect->SetInput(0, myBitmap);

ID2D1Image* pImg = NULL;
effect->GetOutput(&pImg);

如果我投射此图片,getPixelSize()会引发访问冲突。

1 个答案:

答案 0 :(得分:2)

我解决了。它很脏,但它有效。

ID2D1Bitmap* convertImageToBitmap(ID2D1Image* pImg, D2D1_SIZE_U size)
{
    ID2D1Image* oldTarget = NULL;
    ID2D1Bitmap1* targetBitmap = NULL;

    //Create a Bitmap with "D2D1_BITMAP_OPTIONS_TARGET"
    D2D1_BITMAP_PROPERTIES1 bitmapProperties =
        D2D1::BitmapProperties1(
        D2D1_BITMAP_OPTIONS_TARGET,
        D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)
    );
    pDeviceContext->CreateBitmap(size, 0, 0, bitmapProperties, &targetBitmap);

    //Save current Target, replace by ID2D1Bitmap
    pDeviceContext->GetTarget(&oldTarget);
    pDeviceContext->SetTarget(targetBitmap);
    //Draw Image on Target (if currently not drawing also call Begin/EndDraw)
    pDeviceContext->DrawImage(pImg);

    //Set previous Target
    pDeviceContext->SetTarget(oldTarget);

    oldTarget->Release();

    return targetBitmap;
}