locked
How to create a Bitmap from ID2D1DeviceContext

    Question

  • Hi,

    I have a ID2D1DeviceContext and I draw a bitmap(data read from a file having IRandomAccessStream) and line by using this context to a SwapChainBackgroundPanel.

    Now i need to save/render the new changes in "m_d2d1Context" to the original file.

    How can I do it?

    I have the code for saving a ID2D1Bitmap1 to the stream. am confused in rendering/preparing m_d2d1Context to ID2D1Bitmap1.

    I need to Save the data after I use 

    m_d2d1Context->BeginDraw();
    m_d2dContext->DrawBitmap();
    m_d2dContext->DrawLine();
    m_d2d1Context->EndDraw();

    SaveFunction(); - here a saving required;

    Thanks in adv.

    Friday, November 16, 2012 5:06 AM

Answers

  • Maybe you can write another render function with D2D context.

    As we set the target to the image
    m_d2dContext->SetTarget(targetBitmap.Get());
    So that we can draw something will show in the bitmap not the screen.

    After you drawn, you should set the target to screen again.
    m_d2dContext->SetTarget(m_d2dTargetBitmap.Get());

    Best regards,
    Jesse


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

    • Marked as answer by _Sujith Monday, November 19, 2012 11:50 AM
    Monday, November 19, 2012 8:30 AM
    Moderator

All replies

  • Hi,

    First, you should create a bitmap with D2DContext
    m_d2dContext->CreateBitmap

    And then set the D2D target to image, render once and then restore the render target.

      m_d2dContext->SetTarget(targetBitmap.Get());
    Render();
    m_d2dContext->SetTarget(m_d2dTargetBitmap.Get());

    At last, saving the image with WIC encoding.

    For the detail codes, please follow the Direct2D save to image file sample
    http://code.msdn.microsoft.com/windowsapps/SaveAsImageFile-68073cb0

    Best regards,
    Jesse


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

    Monday, November 19, 2012 2:29 AM
    Moderator
  • Hi Jesse,

    I have already drawn some lines and images to m_d2dContext.

    A new Render is not usable here in my situation. Am drawing many shapes to context. When I use CreateBitmap() my screen goes black.

    Is there any ID2D1bitmap1 available/upated when I draw to a context.

    Monday, November 19, 2012 4:44 AM
  • Maybe you can write another render function with D2D context.

    As we set the target to the image
    m_d2dContext->SetTarget(targetBitmap.Get());
    So that we can draw something will show in the bitmap not the screen.

    After you drawn, you should set the target to screen again.
    m_d2dContext->SetTarget(m_d2dTargetBitmap.Get());

    Best regards,
    Jesse


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

    • Marked as answer by _Sujith Monday, November 19, 2012 11:50 AM
    Monday, November 19, 2012 8:30 AM
    Moderator