Share via

WriteableBitmap.AddDirtyRect(Int32Rect) Method


Specifies the area of the bitmap that changed.

 void AddDirtyRect(System::Windows::Int32Rect dirtyRect);
public void AddDirtyRect (System.Windows.Int32Rect dirtyRect);
public void AddDirtyRect (System.Windows.Int32Rect dirtyRect);
member this.AddDirtyRect : System.Windows.Int32Rect -> unit
member this.AddDirtyRect : System.Windows.Int32Rect -> unit
Public Sub AddDirtyRect (dirtyRect As 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(Duration) methods.

dirtyRect falls outside the bounds of the WriteableBitmap.


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.
                IntPtr pBackBuffer = 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.


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.

Applies to