Provides a BitmapSource that can be written to and updated.
Public NotInheritable Class WriteableBitmap _ Inherits BitmapSource
public sealed class WriteableBitmap : BitmapSource
The WriteableBitmap type exposes the following members.
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:
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.
Construct a WriteableBitmap using WriteableBitmap..::.WriteableBitmap.
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.
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).
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.
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.
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.
Loop through the array, getting the individual pixel values as integer values that are evaluated as premultiplied ARGB32.
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.
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); }
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.