How to create a shared bitmap (would like to share a bitmap created with Direct2D with WIC) RRS feed

  • Question

  • I would like to use 'ID2D1Bitmap1' object created using Direct2D context in my WIC code. Here is example code.

    I tried to do the following but I am getting errors and also tried to create sharedBitmap (not sure how to create it properly, can you point me to any examples?) and that too does not work for me.

    Thank you


    // D2D Bitmap
    ComPtr<ID2DBitmap1> d2dBitMap;
    m_d2dContext->CreateBitmap(..., &d2dBitMap);
    // My WIC code
    ID2D1RenderTarget imgRenderTarget;
    m_d2dFactory->CreateWicBitmapRenderTarget(imgWicBitMap.Get(), ..., &imgRenderTarget);
    // I am getting error here
    imgRenderTarget->DrawBitmap(d2dBitMap.Get(), resultImagePos, 1.0F, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR);
    hr = imgRenderTarget->EndDraw();

    • Edited by __PB Tuesday, November 13, 2012 7:48 PM
    Tuesday, November 13, 2012 7:46 PM

All replies

  • Hi,

    CreateWicBitmapRenderTarget needs IWICBitmap as the first parameters.
    In your codes you pass a ID2DBitmap1

    You can follow these codes

    		ComPtr<IWICBitmap> bitmap = NULL; 
    		m_wicFactory->CreateBitmap(600,800, GUID_WICPixelFormat32bppPBGRA,WICBitmapCacheOnLoad,&bitmap)
    		ComPtr<ID2D1RenderTarget> imgRenderTarget;
    		D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties();
    		rtProps.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED);
    		rtProps.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
    		rtProps.usage = D2D1_RENDER_TARGET_USAGE_NONE;

    Best regards,

    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, November 15, 2012 9:10 AM
  • Thanks Jesse, but I have ID2DBitmap1 bitmap and would like to use it in 'DrawBitMap' as shows in my code above, option you suggested does not work for me.

    In other words I have content in ID2DBitmap1, would like to draw it on my WIC Rendertarget, what is a way to do this?



    Friday, November 16, 2012 7:11 PM
  • I do have the same question. 

    Any help would be appreciated.

    Sunday, December 22, 2013 8:35 AM
  • The reason it doesn't work is that you are trying to use a resource created with one context/rendertarget with a different context/rendertarget. There are only a few situations in which that is possible and this isn't one of them.

    If all you want to do is save the bitmap to a file using WIC, follow this sample: http://code.msdn.microsoft.com/windowsapps/SaveAsImageFile-68073cb0 .

    If you really want a WIC render target you can do it with ID2D1Bitmap1::Map by copying the data out and then creating a new bitmap using an overload that lets you specify the initial data. If the original ID2D1Bitmap1 wasn't created with the D2D1_BITMAP_OPTIONS_CPU_READ flag then you'll need to create an intermediate bitmap rendertarget that has that flag set and render the original bitmap to it or else using ID2D1DeviceContext::CreateBitmapFromDxgiSurface [you should be able to QueryInterface (or use ComPtr<T>::As) to get an IDxgiSurface interface from an ID2D1Bitmap1]. Once you have a bitmap with that flag set then you can use Map on it, copy out its data, and then use http://msdn.microsoft.com/en-us/library/dd371803(v=vs.85).aspx to create the bitmap with initial data (i.e. the data you copied out). Make sure that you call Unmap when you are done reading the data and that you free any memory you copied (preferably you'd use a std::unique_ptr to store the copied data. You might even be able to get away with just using the data pointer that you get from calling Map directly rather than copying the data out. If so that'd be far more efficient so do that.)

    Visual C++ MVP | Website | Blog | @mikebmcl | Windows Store DirectX Game Template

    • Proposed as answer by MikeBMcL Sunday, December 22, 2013 5:03 PM
    Sunday, December 22, 2013 5:02 PM