Sets the source image for a BitmapSource by accessing a stream and processing the result asynchronously.
Syntax
Parameters
- streamSource
-
Type: IRandomAccessStream
The stream source that sets the image source value.
Return value
Type: IAsyncAction
An asynchronous handler called when the operation is complete.
Remarks
Setting an image source by calling this method avoids blocking the UI thread. This is actually similar to behavior you get if you set an image source in markup, but the markup parse equivalent doesn't expose the async infrastructure to you. See Quickstart: Calling asynchronous APIs.
If the app changes the image source again via SetSourceAsync, SetSource or UriSource while a SetSourceAsync call is already in progress, the pending SetSourceAsync action will throw a TaskCanceledException and set the Status to Canceled.
Examples
This example shown here uses a file stream (obtained using a file picker, not shown) to load an image source by calling SetSourceAsync. The file picker, stream and call to SetSourceAsync are all asynchronous. The code shown here comes from a larger code sample, the SDK XAML images sample.
// Ensure the stream is disposed once the image is loaded using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) // Set the image source to the selected bitmap BitmapImage bitmapImage = new BitmapImage(); bitmapImage.DecodePixelHeight = decodePixelHeight; bitmapImage.DecodePixelWidth = decodePixelWidth; await bitmapImage.SetSourceAsync(fileStream); Scenario2Image.Source = bitmapImage; }
Requirements
|
Minimum supported client | Windows 8 |
|---|---|
|
Minimum supported server | Windows Server 2012 |
|
Namespace |
|
|
Metadata |
|
See also
Build date: 12/4/2012