This documentation is archived and is not being maintained.

WriteableBitmap.AddDirtyRect Method

Specifies the area of the bitmap that changed.

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

public void AddDirtyRect(
	Int32Rect dirtyRect
You cannot use methods in XAML.


Type: System.Windows.Int32Rect

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


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.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1, 3.0 SP2