3 out of 10 rated this helpful - Rate this topic

WriteableBitmap class

Provides a BitmapSource that can be written to and updated.

Inheritance

Object
  DependencyObject
    ImageSource
      BitmapSource
        WriteableBitmap

Syntax


public sealed class WriteableBitmap : BitmapSource

Attributes

ActivatableAttribute(Windows.UI.Xaml.Media.Imaging.IWriteableBitmapFactory, NTDDI_WIN8)
MarshalingBehaviorAttribute(Agile)
ThreadingAttribute(Both)
VersionAttribute(NTDDI_WIN8)
WebHostHiddenAttribute()

Members

The WriteableBitmap class has these types of members:

Constructors

The WriteableBitmap class has these constructors.

ConstructorDescription
WriteableBitmap Initializes a new instance of the WriteableBitmap class.

 

Methods

The WriteableBitmap class has these methods. It also inherits methods from the Object class.

MethodDescription
ClearValue Clears the local value of a dependency property. (Inherited from DependencyObject)
GetAnimationBaseValue Returns any base value established for a dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject)
GetValue Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject)
Invalidate Requests a draw or redraw of the entire bitmap.
ReadLocalValue Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject)
SetSource Sets the source image for a BitmapSource by accessing a stream. Most callers should use SetSourceAsync instead. (Inherited from BitmapSource)
SetSourceAsync Sets the source image for a BitmapSource by accessing a stream and processing the result asynchronously. (Inherited from BitmapSource)
SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject)

 

Properties

The WriteableBitmap class has these properties.

PropertyAccess typeDescription

Dispatcher

Read-onlyGets the CoreDispatcher that this object is associated with. (Inherited from DependencyObject)

PixelBuffer

Read-onlyGets an access for the direct buffer where each pixel of the WriteableBitmap is written to.

PixelHeight

Read-onlyGets the height of the bitmap in pixels. (Inherited from BitmapSource)

PixelWidth

Read-onlyGets the width of the bitmap in pixels. (Inherited from BitmapSource)

 

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.

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); 
    }                     
}

Requirements

Minimum supported client

Windows 8 [Windows Store apps only]

Minimum supported server

Windows Server 2012 [Windows Store apps only]

Namespace

Windows.UI.Xaml.Media.Imaging
Windows::UI::Xaml::Media::Imaging [C++]

Metadata

Windows.winmd

See also

BitmapSource
Image
BitmapDecoder
Windows.Graphics.Imaging namespace
XAML images sample

 

 

Build date: 12/4/2012

© 2013 Microsoft. All rights reserved.