This topic has not yet been rated - Rate this topic

WriteableBitmap.Unlock Method

Releases the back buffer to make it available for display.

Namespace:  System.Windows.Media.Imaging
Assembly:  PresentationCore (in PresentationCore.dll)
member Unlock : unit -> unit
ExceptionCondition
InvalidOperationException

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

The Unlock method decrements the lock count. When the lock count reaches 0, a render pass is requested if the AddDirtyRect method has been called.

The following code example shows how to release the back buffer by using the Unlock 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.
    writeableBitmap.Lock();

    unsafe
    {
        // 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.
    writeableBitmap.Unlock();
}

.NET Framework

Supported in: 4.5, 4, 3.5 SP1, 3.0 SP2

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.