WriteableBitmap.AddDirtyRect(Int32Rect) 方法

定义

指定更改的位图区域。

public:
 void AddDirtyRect(System::Windows::Int32Rect dirtyRect);
[System.Security.SecurityCritical]
public void AddDirtyRect (System.Windows.Int32Rect dirtyRect);
public void AddDirtyRect (System.Windows.Int32Rect dirtyRect);
[<System.Security.SecurityCritical>]
member this.AddDirtyRect : System.Windows.Int32Rect -> unit
member this.AddDirtyRect : System.Windows.Int32Rect -> unit
Public Sub AddDirtyRect (dirtyRect As Int32Rect)

参数

dirtyRect
Int32Rect

一个 Int32Rect,表示更改的区域。 维数以像素为单位。

属性

例外

Lock()TryLock(Duration) 方法的调用未锁定位图。

dirtyRect 处于 WriteableBitmap 的界限外。

示例

下面的代码示例演示如何使用 AddDirtyRect 该方法指定已更改的后缓冲区区域。

    // 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;

        try{
            // Reserve the back buffer for updates.
            writeableBitmap.Lock();

            unsafe
            {
                // 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));
        }
        finally{
            // Release the back buffer and make it available for display.
            writeableBitmap.Unlock();
        }
    }

注解

调用该方法 AddDirtyRect 以指示代码对后缓冲区所做的更改。

多次调用此方法时,更改的区域累积在足够(但不一定最少)表示形式中。 为了提高效率,只能保证将标记为脏的区域向前复制到前缓冲区。 但是,可以向前复制位图的任何部分,因此必须确保整个后退缓冲区始终有效。

AddDirtyRect仅在调用LockUnlock方法之间调用该方法,如类备注中所述WriteableBitmap

适用于