WriteableBitmap WriteableBitmap WriteableBitmap Class

Definition

Provides a BitmapSource that can be written to and updated.

public : sealed class WriteableBitmap : BitmapSource, IWriteableBitmap
public sealed class WriteableBitmap : BitmapSource, IWriteableBitmap
Public NotInheritable Class WriteableBitmap Inherits BitmapSource Implements IWriteableBitmap
Inheritance
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Examples

This example writes to the PixelBuffer property of WriteableBitmap as part of a transcoding scenario that will eventually use the WriteableBitmap as an Image.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); 
    }                     
}

Remarks

The image source data of a WriteableBitmap is an underlying pixel buffer. WriteableBitmap.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.

Constructors

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

Initializes a new instance of the WriteableBitmap class.

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

The width of the bitmap in pixels.

pixelHeight
int Int32 Int32

The height of the bitmap in pixels.

Properties

Dispatcher Dispatcher Dispatcher

Inherited from DependencyObject

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 IBuffer
Value
IBuffer IBuffer IBuffer

A reference to the pixel buffer.

Examples

This example writes to the PixelBuffer property of WriteableBitmap as part of a transcoding scenario that will eventually use the WriteableBitmap as an Image.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); 
    }                     
}

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.
See Also

PixelHeight PixelHeight PixelHeight

Inherited from BitmapSource

PixelHeightProperty PixelHeightProperty PixelHeightProperty

Inherited from BitmapSource

PixelWidth PixelWidth PixelWidth

Inherited from BitmapSource

PixelWidthProperty PixelWidthProperty PixelWidthProperty

Inherited from BitmapSource

Methods

ClearValue(DependencyProperty) ClearValue(DependencyProperty) ClearValue(DependencyProperty)

Inherited from DependencyObject

GetAnimationBaseValue(DependencyProperty) GetAnimationBaseValue(DependencyProperty) GetAnimationBaseValue(DependencyProperty)

Inherited from DependencyObject

GetValue(DependencyProperty) GetValue(DependencyProperty) GetValue(DependencyProperty)

Inherited from DependencyObject

Invalidate() Invalidate() Invalidate()

Requests a draw or redraw of the entire bitmap.

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

ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty)

Inherited from DependencyObject

RegisterPropertyChangedCallback(DependencyProperty,DependencyPropertyChangedCallback) RegisterPropertyChangedCallback(DependencyProperty,DependencyPropertyChangedCallback) RegisterPropertyChangedCallback(DependencyProperty,DependencyPropertyChangedCallback)

Inherited from DependencyObject

SetSource(IRandomAccessStream) SetSource(IRandomAccessStream) SetSource(IRandomAccessStream)

Inherited from BitmapSource

SetSourceAsync(IRandomAccessStream) SetSourceAsync(IRandomAccessStream) SetSourceAsync(IRandomAccessStream)

Inherited from BitmapSource

SetValue(DependencyProperty,Object) SetValue(DependencyProperty,Object) SetValue(DependencyProperty,Object)

Inherited from DependencyObject

UnregisterPropertyChangedCallback(DependencyProperty,Int64) UnregisterPropertyChangedCallback(DependencyProperty,Int64) UnregisterPropertyChangedCallback(DependencyProperty,Int64)

Inherited from DependencyObject

See Also