FillRectangle() and Clear() for Direct2D


  • In this example: XAML SurfaceImageSource DirectX interop sample


    Scenario1 showed us how to draw 50 rectangles in an XAML Image control with SurfaceImageSource.

    The idea is that we first call ID2D1DeviceContext->Clear() and then we call ID2D1DeviceContext->FillRectangle() fifty times.

    Seems that if we comment the line of ID2D1DeviceContext->Clear() and only use ID2D1DeviceContext->FillRectangle() in the sample, the background color will be updated randomly.

    My question is: do we have a way to call ID2D1DeviceContext->FillRectangle() only (without the Clear()) while the background keeps the original color?

    Tuesday, March 5, 2013 2:37 PM


  • You start with an uninitialized buffer, so the surface doesn't have an original color to keep. It is only once it has been Cleared to a specific color that we have a deterministic state. You can Clear it to a color with 0 alpha and the generated SurfaceImageSource will draw transparently.


    • Marked as answer by willjing Wednesday, March 6, 2013 2:19 PM
    Wednesday, March 6, 2013 12:23 AM