WriteableBitmap Class
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Provides a BitmapSource that can be written to and updated.
Inheritance Hierarchy
System.Object
System.Windows.DependencyObject
System.Windows.Media.ImageSource
System.Windows.Media.Imaging.BitmapSource
System.Windows.Media.Imaging.WriteableBitmap
Namespace: System.Windows.Media.Imaging
Assembly: System.Windows (in System.Windows.dll)
Syntax
'Declaration
Public NotInheritable Class WriteableBitmap _
Inherits BitmapSource
public sealed class WriteableBitmap : BitmapSource
The WriteableBitmap type exposes the following members.
Constructors
Name | Description | |
---|---|---|
WriteableBitmap(BitmapSource) | Initializes a new instance of the WriteableBitmap class using the provided BitmapSource. | |
WriteableBitmap(Int32, Int32) | Initializes a new instance of the WriteableBitmap class using the provided dimensions. | |
WriteableBitmap(UIElement, Transform) | Initializes a new instance of the WriteableBitmap class using the provided element and transform. |
Top
Properties
Name | Description | |
---|---|---|
Dispatcher | Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.) | |
PixelHeight | Gets the height of the bitmap in pixels. (Inherited from BitmapSource.) | |
Pixels | Gets an array representing the 2-D texture of the bitmap. | |
PixelWidth | Gets the width of the bitmap in pixels. (Inherited from BitmapSource.) |
Top
Methods
Name | Description | |
---|---|---|
CheckAccess | Determines whether the calling thread has access to this object. (Inherited from DependencyObject.) | |
ClearValue | Clears the local value of a dependency property. (Inherited from DependencyObject.) | |
Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) | |
GetAnimationBaseValue | Returns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.) | |
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
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. | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ReadLocalValue | Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.) | |
Render | Renders an element within the bitmap. | |
SetSource | Sets the source of the BitmapSource. (Inherited from BitmapSource.) | |
SetValue | Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
Top
Extension Methods
Name | Description | |
---|---|---|
CopyTo(Texture2D) | Overloaded. Copies a bitmap image to a texture. (Defined by BitmapSourceExtensions.) | |
CopyTo(TextureCube, CubeMapFace) | Overloaded. Copies a bitmap image to a cube map face. (Defined by BitmapSourceExtensions.) | |
CopyTo(Texture2D, Int32, Nullable<Rectangle>, Int32, Int32) | Overloaded. Copies a sub-region of bitmap to a sub-region of the texture. (Defined by BitmapSourceExtensions.) | |
CopyTo(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
Remarks
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.
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.
Examples
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.
<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);
}
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.