Export (0) Print
Expand All
2 out of 9 rated this helpful - Rate this topic

WriteableBitmap Class

Provides a BitmapSource that can be written to and updated.

Namespace:  System.Windows.Media.Imaging
Assembly:  System.Windows (in System.Windows.dll)
public sealed class WriteableBitmap : BitmapSource

The WriteableBitmap type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneWriteableBitmap(BitmapSource)Initializes a new instance of the WriteableBitmap class using the provided BitmapSource.
Public methodSupported by Silverlight for Windows PhoneWriteableBitmap(Int32, Int32)Initializes a new instance of the WriteableBitmap class using the provided dimensions.
Public methodSupported by Silverlight for Windows PhoneWriteableBitmap(UIElement, Transform)Initializes a new instance of the WriteableBitmap class using the provided element and transform.
Top
  NameDescription
Public propertySupported by Silverlight for Windows PhoneDispatcherGets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public propertySupported by Silverlight for Windows PhonePixelHeightGets the height of the bitmap in pixels. (Inherited from BitmapSource.)
Public propertySupported by Silverlight for Windows PhonePixelsGets an array representing the 2-D texture of the bitmap.
Public propertySupported by Silverlight for Windows PhonePixelWidthGets the width of the bitmap in pixels. (Inherited from BitmapSource.)
Top
  NameDescription
Public methodSupported by Silverlight for Windows PhoneCheckAccessDetermines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows PhoneClearValueClears the local value of a dependency property. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows PhoneEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetAnimationBaseValueReturns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows PhoneGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetValueReturns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows PhoneInvalidateRequests a draw or redraw of the entire bitmap.
Protected methodSupported by Silverlight for Windows PhoneMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneReadLocalValueReturns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows PhoneRenderRenders an element within the bitmap.
Public methodSupported by Silverlight for Windows PhoneSetSourceSets the source of the BitmapSource. (Inherited from BitmapSource.)
Public methodSupported by Silverlight for Windows PhoneSetValueSets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows PhoneToStringReturns a string that represents the current object. (Inherited from Object.)
Top
  NameDescription
Public Extension MethodCopyTo(Texture2D)Overloaded. Copies a bitmap image to a texture. (Defined by BitmapSourceExtensions.)
Public Extension MethodCopyTo(TextureCube, CubeMapFace)Overloaded. Copies a bitmap image to a cube map face. (Defined by BitmapSourceExtensions.)
Public Extension MethodCopyTo(Texture2D, Int32, Nullable<Rectangle>, Int32, Int32)Overloaded. Copies a sub-region of bitmap to a sub-region of the texture. (Defined by BitmapSourceExtensions.)
Public Extension MethodCopyTo(TextureCube, CubeMapFace, Int32, Nullable<Rectangle>, Int32, Int32)Overloaded. Copies a sub-region of bitmap to a sub-region of the cube map face. (Defined by BitmapSourceExtensions.)
Top

Use the WriteableBitmap class to update and render a bitmap on a per-frame basis. The following are some example scenarios where a WriteableBitmap is useful, and a list of the relevant WriteableBitmap API that enables the scenario:

Generate algorithmic imaging content such as a fractal image

  • Construct an initially empty but dimensioned WriteableBitmap using WriteableBitmap(Int32, Int32).

  • Get the pixel array from Pixels.

  • Loop through the array, setting the individual pixel values as integer values that are evaluated as premultiplied ARGB32.

  • Call Invalidate.

  • To display the image in UI, use the WriteableBitmap as the source for an imaging control such as Image, or as the source image for an ImageBrush.

Generate a bitmap snapshot of a visual tree starting from any UIElement

  • Construct a WriteableBitmap using WriteableBitmap.WriteableBitmap.

  • To display the image in UI, use the WriteableBitmap as the source for an imaging control such as Image, or as the source image for an ImageBrush.

  • If you want to render the bitmap multiple times and reuse the instance for additional snapshots, use the Render method. You will need to call Invalidate after Render to render the new image.

  • You have the option to preserve any existing RenderTransform on the UIElement by passing the transform to the constructor, or canceling any existing transform by passing an identity transform or null.

NoteNote:

WriteableBitmap does not render popup controls as UIElement input. This includes Popup and ToolTip. It also includes the items popup area of an unresolved ComboBox (the image will capture the ComboBox as its single-item resolved output).

Grab a frame of a running video from MediaElement

This scenario is a variation of the above scenario. Pass the desired MediaElement as the UIElement construction source, and construct the WriteableBitmap at the point in time when the displayed video is at the desired frame.

A WriteableBitmap obtained from media content potentially has security restrictions on its API. For more information, see "Protected Content" section of this topic.

Grab an image snapshot from a webcam device

For more information on this scenario, see CaptureSource.CaptureImageAsync. WriteableBitmap is involved in this scenario because it is the value obtained from the data of the asynchronous return event, CaptureImageCompleted. To render this image, you do not need to call Invalidate.

Get a pixel array from an existing image

  • Construct a WriteableBitmap using WriteableBitmap.WriteableBitmap. The original source might have been loaded through URL or through a stream. If from a URL, this URL cannot be cross-domain. See "Protected Content" section of this topic.

  • Get the pixel array from Pixels.

  • Loop through the array, getting the individual pixel values as integer values that are evaluated as premultiplied ARGB32.

WriteableBitmap Pixel Format in Silverlight

When assigning colors to pixels in your bitmap, use pre-multiplied colors. The format used by the Silverlight WriteableBitmap is ARGB32 (premultiplied RGB). The format becomes relevant if you are populating the integer values in the Pixels array.

The initial pixel values in the dimensioned array are 0, which will render as black if left unaltered.

Protected Content

The WriteableBitmap class has a security model that restricts access to the Pixels array, if the WriteableBitmap is constructed using cross-domain content. For example, a WriteableBitmap that is constructed using a BitmapImage referencing a URL that comes from another domain does not permit access to its Pixels array. The restriction extends to any UI element that uses a URL-derived property for setting some or all of its content. In particular, this restriction applies to the "Grab a frame of a running video from MediaElement" scenario. If the MediaElement.Source references a video file from another domain, the WriteableBitmap created by referencing the MediaElement as the element source restricts access to the Pixels array.

You can still access PixelHeight and PixelWidth properties of WriteableBitmap even if created from protected content. This information can be useful for determining the natural size of content, for example for a MediaElement that leaves its sizing up to the source.

The following example demonstrates how to use a WriteableBitmap to take snapshots of a playing video and display those snapshots as thumbnails. Click on the video as it plays to create the thumbnails.

Run this sample


<StackPanel Orientation="Vertical" 
    HorizontalAlignment="Stretch" Margin="10">

    <Border BorderBrush="AntiqueWhite" BorderThickness="2" 
        CornerRadius="1" Margin="4" 
        HorizontalAlignment="Center" VerticalAlignment="Top">
        <Border>
            <MediaElement x:Name="myMediaElement" 
                Source="xbox.wmv" Stretch="None" 
                MouseLeftButtonDown="me_MouseLeftButtonDown"/>
        </Border>
    </Border>

    <!-- thumbnails go here -->
    <StackPanel x:Name="thumbs" Orientation="Horizontal" 
        Margin="10,40,10,10">
    </StackPanel>

</StackPanel>



private void me_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{

    // Create a WriteableBitmap and set it to the MediaElement (video).
    // The WriteableBitmap represents a "snapshot" of the video.
    WriteableBitmap wb = new WriteableBitmap(myMediaElement, null);

    // Create an image of the desired size and set its source to
    // the WriteableBitmap representing a snapshot of the video.
    Image image = new Image();
    image.Height = 64;
    image.Margin = new Thickness(10);
    image.Source = wb;

    // Display the snapshot of the video among the thumbnails. 
    thumbs.Children.Add(image);
}


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.