如何接收影像 (使用 C#/VB/C++ 和 XAML 的 Windows 執行階段應用程式)

Applies to Windows and Windows Phone

影像與相片是使用者經常分享的內容類型。我們將在這裡示範如何接收分享到您應用程式的單一影像。

這個主題中的程式碼使用 GetBitmap 取得點陣圖影像。使用者通常分享的影像不是這種格式。因此,建議您的應用程式也支援 StorageItems,這可以是任何檔案類型的集合。如需支援 StorageItems 的方法,請參閱如何接收檔案

您必須知道的事

技術

先決條件

  • 您應該熟悉 Visual Studio 以及相關範本。
  • 您應該熟悉 C# 開發。

指示

步驟 1: 支援分享協定。

在您的應用程式接收分享的內容之前,您必須宣告它支援目標協定。這份協定讓系統知道應用程式可以接收內容。如果您使用 Visual Studio 範本來建立自己的應用程式,下列是支援分享目標協定的方法:

  1. 開啟資訊清單檔案,這個檔案的名稱可能像是 package.appxmanifest
  2. 開啟 [宣告] 索引標籤。
  3. 從 [可用宣告]**** 清單中選擇 [分享目標]。

步驟 2: 指定應用程式支援點陣圖。

如果要指定支援點陣圖做為資料格式:

  1. 開啟資訊清單檔案。
  2. 在 [資料格式]**** 區段中,按一下 [加入新的]。
  3. 輸入 bitmap。

步驟 3: 新增必要的命名空間

對於目標應用程式,您需要 Windows.ApplicationModel.ActivationWindows.ApplicationModel.DataTransfer,以及 Windows.ApplicationModel.DataTransfer.ShareTarget 命名空間:

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

步驟 4: 處理分享啟用

當使用者選取您的應用程式來分享內容時,系統會呼叫應用程式的 Application.OnShareTargetActivated 方法以啟用您的應用程式。您必須覆寫這個方法以取得使用者想分享的內容。

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

步驟 5: 取得 ShareOperation 物件。

ShareOperation 物件包含應用程式所需的全部資料,讓應用程式取得使用者想分享的內容。

ShareOperation shareOperation = args.ShareOperation;

步驟 6: 檢查 DataPackageView 是否包含點陣圖。

ShareOperation 物件包含一個 DataPackageView 物件。這個物件本質上是 DataPackage 物件 (來源應用程式用來建立資料) 的唯讀版本。使用此物件來查看要分享的內容是否為點陣圖格式。

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

即使您的應用程式只支援影像,還是應該新增與之前程式碼類似的陳述式。這樣可以更方便地支援您之後選擇的其他資料類型和檔案格式。

步驟 7: 取得縮圖影像 (如果有的話)。

通常分享影像的應用程式會包含這些影像的縮圖版本。下列程式碼會檢查是否有縮圖存在。如果存在,程式碼會將縮圖新增為 Image 控制項的來源。

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;
}

步驟 8: 取得點陣圖。

若要取得點陣圖,請呼叫 DataPackageView.GetBitmap 方法。

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;

步驟 9: 呼叫 ReportCompleted。

在您的應用程式完成處理分享的內容後,請呼叫 ReportCompleted。您必須呼叫這個方法,讓系統知道不再需要您的應用程式。

shareOperation.ReportCompleted();

備註

如果想要了解應用程式如何接收分享的影像,最好的方法就是參閱程式碼範例。不過,下列範例也提供您一個很好的完整函式範例。

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;
}              

相關主題

分享內容目標應用程式範例

分享和交換資料

如何接收 HTML

如何接收連結

如何接收文字

快速入門:接收分享的內容

DataPackage

Windows.ApplicationModel.DataTransfer

Windows.ApplicationModel.DataTransfer.Share

對目標應用程式偵錯的指導方針