SetSourceAsync method
Collapse the table of content
Expand the table of content

BitmapSource.SetSourceAsync method

Sets the source image for a BitmapSource by accessing a stream and processing the result asynchronously.


public IAsyncAction SetSourceAsync(
  IRandomAccessStream 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.


Setting an image source by calling the asynchronous SetSourceAsync method rather than the similar SetSource method avoids blocking the UI thread. The SetSourceAsync behavior is similar to what the system does internally when you set an image source as a URI in markup: the system doesn't wait to retrieve and decode, but it does run layout again once the image source is available. The markup parsing equivalent doesn't expose the async infrastructure, but the SetSourceAsync method does. For more info on how to use async, await, or how to work with an IAsyncAction value, see Call asynchronous APIs in C# or Visual Basic.

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.

If you have a Microsoft .NET stream that you want to use as a source, you can use the AsRandomAccessStream extension method to convert it to the IRandomAccessStream type that's needed as input for SetSourceAsync.

In low memory situations (most likely on lower-memory phones), it is possible for an exception to be raised with the message "The image is unrecognized" and an HRESULT of 0x88982F60. While this exception ordinarily indicates bad data, if your app is close to its memory limit then the cause of the exception is likely to be low memory. In that case, we recommend that you free memory and try again.


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 (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0


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



Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]


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



See also

Call asynchronous APIs in C# or Visual Basic
XAML images sample



© 2016 Microsoft