locked
How to create ID2D1DeviceContext for IWICBitmap? (For Metro app in C++)

    Question

  • I can create a ID2D1RenderTarget for a IWICBitmap using the following code...

        D2D1_FACTORY_OPTIONS options;
       
    ZeroMemory(&options, sizeof(D2D1_FACTORY_OPTIONS));

    #if defined(_DEBUG)
         
    // If the project is in a debug build, enable Direct2D debugging via SDK Layers
        options
    .debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
    #endif

       
    ThrowIfFailed(D2D1CreateFactory(
                D2D1_FACTORY_TYPE_SINGLE_THREADED
    ,
                __uuidof
    (ID2D1Factory1),
               
    &options,
               
    &m_d2dFactory
               
    ));

        D2D1_RENDER_TARGET_PROPERTIES props
    ;
        props
    = D2D1::RenderTargetProperties();
        m_d2dFactory
    ->CreateWicBitmapRenderTarget(m_pTheBitmap.Get(), &props, &m_target);

    But if I want to apply ID2D1Effect to this bitmap, I can do that only if I have ID2D1DeviceContext. How do I get a ID2D1DeviceContext for a IWICBitmap? I do not want to render anything on the screen.

    Friday, July 27, 2012 5:42 AM

Answers

  • Hi Pragos,

    You can set the target of a device context to a bitmap and can draw over it with in BeginDraw() and

    EndDraw().

    Please use the following code snippet.

    	// Create temporary bitmap to render to in the background.
    	ComPtr<ID2D1Bitmap1> renderTarget;
    	D2D1_BITMAP_PROPERTIES1 renderTargetProperties = D2D1::BitmapProperties1(
    		D2D1_BITMAP_OPTIONS_TARGET,
    		D2D1::PixelFormat(
    		DXGI_FORMAT_B8G8R8A8_UNORM,
    		D2D1_ALPHA_MODE_PREMULTIPLIED
    		)
    		);
    
    	DX::ThrowIfFailed(
    		m_d2dContext->CreateBitmap(
    		m_imagePixelSize, 
    		nullptr,
    		0,
    		&renderTargetProperties,
    		&renderTarget
    		)
    		);
    	// Draw to temporary target in background. This ensures GPU processes image / populates mip levels.
    	// This drawing code is blocking, which is why we have to perform it on a background thread to allow
    	// the thumbnail to be continously rendered in the foreground.
    	m_d2dContext->SetTarget(renderTarget.Get());

    thanks,

    Bhash

    • Marked as answer by pragos Friday, July 27, 2012 4:04 PM
    Friday, July 27, 2012 2:23 PM

All replies

  • Hi Pragos,

    You can set the target of a device context to a bitmap and can draw over it with in BeginDraw() and

    EndDraw().

    Please use the following code snippet.

    	// Create temporary bitmap to render to in the background.
    	ComPtr<ID2D1Bitmap1> renderTarget;
    	D2D1_BITMAP_PROPERTIES1 renderTargetProperties = D2D1::BitmapProperties1(
    		D2D1_BITMAP_OPTIONS_TARGET,
    		D2D1::PixelFormat(
    		DXGI_FORMAT_B8G8R8A8_UNORM,
    		D2D1_ALPHA_MODE_PREMULTIPLIED
    		)
    		);
    
    	DX::ThrowIfFailed(
    		m_d2dContext->CreateBitmap(
    		m_imagePixelSize, 
    		nullptr,
    		0,
    		&renderTargetProperties,
    		&renderTarget
    		)
    		);
    	// Draw to temporary target in background. This ensures GPU processes image / populates mip levels.
    	// This drawing code is blocking, which is why we have to perform it on a background thread to allow
    	// the thumbnail to be continously rendered in the foreground.
    	m_d2dContext->SetTarget(renderTarget.Get());

    thanks,

    Bhash

    • Marked as answer by pragos Friday, July 27, 2012 4:04 PM
    Friday, July 27, 2012 2:23 PM
  • Ok, I guess I got it, I got this sample code...

    http://code.msdn.microsoft.com/windowsapps/SaveAsImageFile-68073cb0

    Friday, July 27, 2012 4:25 PM