WriteableBitmap.AddDirtyRect Method (Int32Rect)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Specifies the area of the bitmap that changed.

Namespace:   System.Windows.Media.Imaging
Assembly:  PresentationCore (in PresentationCore.dll)

public void AddDirtyRect(
	Int32Rect dirtyRect


Type: System.Windows.Int32Rect

An Int32Rect representing the area that changed. Dimensions are in pixels.

Exception Condition

The bitmap has not been locked by a call to the Lock or TryLock methods.


dirtyRect falls outside the bounds of the WriteableBitmap.

Call the AddDirtyRect method to indicate changes your code has made to the back buffer.

When you call this method multiple times, the changed areas are accumulated in a sufficient, but not necessarily minimal, representation. For efficiency, only the areas that are marked as dirty are guaranteed to be copied forward to the front buffer. However, any portion of the bitmap may be copied forward, so you must ensure that the entire back buffer is always valid.

Call the AddDirtyRect method only between calls to the Lock and Unlock methods, as described in the WriteableBitmap class remarks.

The following code example shows how to specify the area of the back buffer that changed by using the AddDirtyRect method.

// The DrawPixel method updates the WriteableBitmap by using
// unsafe code to write a pixel into the back buffer.
static void DrawPixel(MouseEventArgs e)
    int column = (int)e.GetPosition(i).X;
    int row = (int)e.GetPosition(i).Y;

    // Reserve the back buffer for updates.

        // Get a pointer to the back buffer.
        int pBackBuffer = (int)writeableBitmap.BackBuffer;

        // Find the address of the pixel to draw.
        pBackBuffer += row * writeableBitmap.BackBufferStride;
        pBackBuffer += column * 4;

        // Compute the pixel's color.
        int color_data = 255 << 16; // R
        color_data |= 128 << 8;   // G
        color_data |= 255 << 0;   // B

        // Assign the color data to the pixel.
        *((int*) pBackBuffer) = color_data;

    // Specify the area of the bitmap that changed.
    writeableBitmap.AddDirtyRect(new Int32Rect(column, row, 1, 1));

    // Release the back buffer and make it available for display.


for access to unmanaged resources. Security action: Demand. Associated enumeration: SecurityPermissionFlag.UnmanagedCode

.NET Framework
Available since 3.0
Return to top