WriteableBitmap WriteableBitmap WriteableBitmap Class

Provides a BitmapSource that can be written to and updated.

Syntax

Declaration

public sealed class WriteableBitmappublic sealed class WriteableBitmapPublic NotInheritable Class WriteableBitmap

Inheritance Hierarchy

Remarks

The image source data of a WriteableBitmap is an underlying pixel buffer. PixelBuffer cannot be written to directly, however, you can use language-specific techniques to access the buffer and change its contents.

  • To access the pixel content from C# or Microsoft Visual Basic, you can use the AsStream extension method to access the underlying buffer as a stream.
  • To access the pixel content from C++, you can query for the IBufferByteAccess type (defined in Robuffer.h) and directly access its Buffer property.

Some of the scenarios where you might have used a WriteableBitmap class in previous XAML frameworks can be accomplished in the Windows Runtime by using the RenderTargetBitmap class instead. For more info see RenderTargetBitmap.

Examples

This example writes to the PixelBuffer property of WriteableBitmap as part of a transcoding scenario that will eventually use the WriteableBitmap as an Source value and display the image. The code shown here comes from a larger code sample, the SDK XAML images sample.

using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) 
{
    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); 
    // Scale image to appropriate size 
    BitmapTransform transform = new BitmapTransform() {  
        ScaledWidth = Convert.ToUInt32(Scenario4WriteableBitmap.PixelWidth), 
        ScaledHeight = Convert.ToUInt32(Scenario4WriteableBitmap.PixelHeight)
    }; 
    PixelDataProvider pixelData = await decoder.GetPixelDataAsync( 
        BitmapPixelFormat.Bgra8, // WriteableBitmap uses BGRA format 
        BitmapAlphaMode.Straight, 
        transform, 
        ExifOrientationMode.IgnoreExifOrientation, // This sample ignores Exif orientation 
        ColorManagementMode.DoNotColorManage
    ); 

    // An array containing the decoded image data, which could be modified before being displayed 
    byte[] sourcePixels = pixelData.DetachPixelData(); 

    // Open a stream to copy the image contents to the WriteableBitmap's pixel buffer 
    using (Stream stream = Scenario4WriteableBitmap.PixelBuffer.AsStream()) 
    { 
        await stream.WriteAsync(sourcePixels, 0, sourcePixels.Length); 
    }                     
}

Constructors summary

Initializes a new instance of the WriteableBitmap class.

Properties summary

Gets an access for the direct buffer where each pixel of the WriteableBitmap is written to.

Methods summary

Requests a draw or redraw of the entire bitmap.

Constructors

  • WriteableBitmap(Int32, Int32)
    WriteableBitmap(Int32, Int32)
    WriteableBitmap(Int32, Int32)
    WriteableBitmap(Int32, Int32)

    Initializes a new instance of the WriteableBitmap class.

    public WriteableBitmap(Int32 pixelWidth, Int32 pixelHeight)public New(Int32 pixelWidth, Int32 pixelHeight)Public Sub New(pixelWidth As Int32, pixelHeight As Int32)public WriteableBitmap(Int32 pixelWidth, Int32 pixelHeight)

    Parameters

    • pixelWidth
      System.Int32
      System.Int32
      System.Int32

      The width of the bitmap in pixels.

    • pixelHeight
      System.Int32
      System.Int32
      System.Int32

      The height of the bitmap in pixels.

Properties

  • PixelBuffer
    PixelBuffer
    PixelBuffer
    PixelBuffer

    Gets an access for the direct buffer where each pixel of the WriteableBitmap is written to.

    public IBuffer PixelBuffer { get; }public IBuffer PixelBuffer { get; }Public ReadOnly Property PixelBuffer As IBufferpublic IBuffer PixelBuffer { get; }

    Property Value

    Remarks

    PixelBuffer cannot be written to directly, however, you can use language-specific techniques to access the buffer and change its contents.

    • To access the pixel content from C# or Microsoft Visual Basic, you can use the AsStream extension method to access the underlying buffer as a stream.
    • To access the pixel content from C++, you can query for the IBufferByteAccess type (defined in Robuffer.h) and directly access its Buffer property.

    Examples

    This example writes to the PixelBuffer property of WriteableBitmap as part of a transcoding scenario that will eventually use the WriteableBitmap as an Source value and display the image. The code shown here comes from a larger code sample, the SDK XAML images sample.

    using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) 
    {
        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); 
        // Scale image to appropriate size 
        BitmapTransform transform = new BitmapTransform() {  
            ScaledWidth = Convert.ToUInt32(Scenario4WriteableBitmap.PixelWidth), 
            ScaledHeight = Convert.ToUInt32(Scenario4WriteableBitmap.PixelHeight)
        }; 
        PixelDataProvider pixelData = await decoder.GetPixelDataAsync( 
            BitmapPixelFormat.Bgra8, // WriteableBitmap uses BGRA format 
            BitmapAlphaMode.Straight, 
            transform, 
            ExifOrientationMode.IgnoreExifOrientation, // This sample ignores Exif orientation 
            ColorManagementMode.DoNotColorManage
        ); 
    
        // An array containing the decoded image data, which could be modified before being displayed 
        byte[] sourcePixels = pixelData.DetachPixelData(); 
    
        // Open a stream to copy the image contents to the WriteableBitmap's pixel buffer 
        using (Stream stream = Scenario4WriteableBitmap.PixelBuffer.AsStream()) 
        { 
            await stream.WriteAsync(sourcePixels, 0, sourcePixels.Length); 
        }                     
    }
    

Methods

  • Invalidate()
    Invalidate()
    Invalidate()
    Invalidate()

    Requests a draw or redraw of the entire bitmap.

    public void Invalidate()public void Invalidate()Public Function Invalidate() As voidpublic void Invalidate()

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

Windows.Foundation.Metadata.ActivatableAttribute
Windows.Foundation.Metadata.ContractVersionAttribute
Windows.Foundation.Metadata.MarshalingBehaviorAttribute
Windows.Foundation.Metadata.ThreadingAttribute
Windows.Foundation.Metadata.WebHostHiddenAttribute

Details

Assembly

Windows.UI.Xaml.Media.Imaging.dll