WriteableBitmap.AddDirtyRect(Int32Rect) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定更改的位图区域。
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)
参数
- 属性
例外
对 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仅在调用Lock和Unlock方法之间调用该方法,如类备注中所述WriteableBitmap。