本文介绍了Direct2D:将 ID2D1Image 转换为 ID2D1Bitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a programm that needs to modify what is already on the screen.So I only have a ID2D1Bitmap that i created using pRenderTarget->CopyFromRenderTarget.

我想要做的是,将效果应用于该位图.效果只返回ID2D1Image,但我需要有一个ID2D1Bitmap.

What I'm trying to do is, applying effects to that Bitmap. The effect only returns ID2D1Image, but i need to have a ID2D1Bitmap.

有没有办法做到这一点?

Is there a way to do this?

编辑 1:

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() 会引发访问冲突.

If I cast this Image, getPixelSize() raises an access violation.

推荐答案

我解决了.它很脏,但很管用.

I solved it. It's dirty, but it works.

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;
}

这篇关于Direct2D:将 ID2D1Image 转换为 ID2D1Bitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 04:49