How to receive an image (XAML)

Applies to Windows and Windows Phone

Images and photos are common types of content that users like to share. Here, we'll show you how to receive a single image that is shared to your app.

The code in this topic uses GetBitmap to get a bitmap image. Often, users share images that aren't in this format. Therefore, we recommend that your app also support StorageItems, which can be a collection of any type of files. We describe how to support StorageItems in How to receive files.

What you need to know

Technologies

Prerequisites

  • You should be familiar with Visual Studio and its associated templates.
  • You should be familiar with C# development.

Instructions

Step 1: Support the Share contract.

Before your app can receive shared content, you have to declare that it supports the Share contract. This contract lets the system know that your app is available to receive content. If you're using a Visual Studio template to create your app, here's how you support the Share contract:

  1. Open the manifest file. It should be called something like package.appxmanifest.
  2. Open the Declarations tab.
  3. Choose Share Target from the Available Declarations list.

Step 2: Specify that your app supports bitmaps.

To specify that you support bitmaps as a data format:

  1. Open the manifest file.
  2. In the Data Formats section, click Add New.
  3. Type bitmap.

Step 3: Adding the necessary namespaces

For a target app, you'll need the Windows.ApplicationModel.Activation, Windows.ApplicationModel.DataTransfer, and Windows.ApplicationModel.DataTransfer.ShareTarget namespaces:

using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.DataTransfer;
using Windows.ApplicationModel.DataTransfer.ShareTarget;

Step 4: Handling share activation

When a user selects your app to share content, the system activates your app by calling the app's Application.OnShareTargetActivated method. You need to override this method to get the content that a user wants to share.

protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
    // Code to handle activation goes here. 
}

Step 5: Get the ShareOperation object.

The ShareOperation object contains all the data that your app needs, to get the content that a user wants to share.

ShareOperation shareOperation = args.ShareOperation;

Step 6: Check to see if the DataPackageView contains a bitmap.

The ShareOperation object contains a DataPackageView object. This object is essentially a read-only version of the DataPackage object that the source app used to create the data. Use this object to see if the content being shared is available in bitmap format.

if (shareOperation.Data.Contains(StandardDataFormats.Bitmap))
{
    // Code to process images goes here.
}

Even if your app supports only images, you should add a statement similar to the preceding code. It makes it easier to support other data types and file formats later if you choose.

Step 7: Get the thumbnail image, if available.

Often, apps that share images include thumbnail versions of those images. The following code checks to see if a thumbnail exists. If so, it adds the thumbnail as the source for an Image control.

RandomAccessStreamReference thumbnail = shareOperation.Data.Properties.Thumbnail;
if (thumbnail != null)
{
    IRandomAccessStreamWithContentType thumbnailStream = await thumbnail.OpenReadAsync();
    BitmapImage thumbnailImage = new BitmapImage();
    thumbnailImage.SetSource(thumbnailStream);
    // You need to add an Image control to display the thumbnail.
    thumbnailHolder.Source = thumbnailImage;
}

Step 8: Get the bitmap.

To get the bitmap, call the DataPackageView.GetBitmap method.

IRandomAccessStreamReference imageReceived = await shareOperation.Data.GetBitmapAsync();
IRandomAccessStreamWithContentType stream = await imageReceived.OpenReadAsync();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
// You need to add an Image control to display the bitmap.
imageHolder.Source = bitmapImage;

Step 9: Call ReportCompleted.

After your app finishes processing the shared content, call ReportCompleted. You must call this method so the system knows your app is no longer needed.

shareOperation.ReportCompleted();

Remarks

The best way to see how an app can receive an image as part of sharing is by checking out our code sample. However, the following example should also give you a good idea of how a completed function might look.

if (shareOperation.Data.Contains(StandardDataFormats.Bitmap))
{
    RandomAccessStreamReference thumbnail = this.shareOperation.Data.Properties.Thumbnail;
    if (thumbnail != null) 
    {
        IRandomAccessStreamWithContentType thumbnailStream = await thumbnail.OpenReadAsync();
        BitmapImage thumbnailImage = new BitmapImage();
        thumbnailImage.SetSource(thumbnailStream);
        thumbnailHolder.Source = thumbnailImage;
    }

    IRandomAccessStreamReference imageReceived = await shareOperation.Data.GetBitmapAsync();
    IRandomAccessStreamWithContentType stream = await imageReceived.OpenReadAsync();
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.SetSource(stream);
    imageHolder.Source = bitmapImage;
}              

Sharing content target app sample

Sharing and exchanging data

How to receive HTML

How to receive a link

How to receive text

Quickstart: Receiving shared content

DataPackage

Windows.ApplicationModel.DataTransfer

Windows.ApplicationModel.DataTransfer.Share

Guidelines for debugging target apps