如何使用 FilePicker 將影像載入 Direct2D 效果

示範如何使用 Windows::Storage::P ickers::FileOpenPicker 將影像載入 Direct2D 效果。 如果您想要讓使用者從 Windows 市集應用程式中的儲存體選取影像檔,建議您使用 FileOpenPicker

您所需了解的事情

技術

必要條件

指示

步驟 1:開啟檔案選擇器

建立 FileOpenPicker 物件,並設定 ViewModeSuggestedStartLocationFileTypeFilter 以選取影像。 呼叫 PickSingleFileAsync 方法。

    FileOpenPicker^ openPicker = ref new FileOpenPicker();
    openPicker->ViewMode = PickerViewMode::Thumbnail;
    openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
    openPicker->FileTypeFilter->Append(".jpg");
    auto pickOperation = openPicker->PickSingleFileAsync();

PickSingleFileAsync完成之後,您會從傳回的IAsyncOperation介面取得檔案資料流程。

步驟 2:取得檔案資料流程

宣告要在檔案選擇器非同步作業傳回之後執行的完成處理常式。 使用 GetResults 方法來擷取檔案,並取得檔案資料流程物件。

    pickOperation->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>(
          [=](IAsyncOperation<StorageFile^> ^operation, AsyncStatus status)
    {
        auto file = operation->GetResults();
        if (file) // If file == nullptr, the user did not select a file.
        {
                             auto openOperation = file->OpenAsync(FileAccessMode::Read);
                             openOperation->Completed = ref new
                                      AsyncOperationCompletedHandler<IRandomAccessStream^>(
                                      [=](IAsyncOperation<IRandomAccessStream^> ^operation, AsyncStatus status)
                             {
                                      auto fileStream = operation->GetResults();

                                      // Pass IRandomAccessStream^ into DirectXApp for decoding/processing.
                                      OpenFile(fileStream);
                             });
        }
    });

在下一個步驟中,您會將IRandomAccessStream物件轉換成可傳遞至WICIStream

步驟 3:轉換檔案資料流程

使用 CreateStreamOverRandomAccessStream 函式來轉換檔案資料流程。 Windows 執行階段 API 代表使用IRandomAccessStream的資料流程,而WIC則會取用IStream

    ComPtr<IStream> istream;
    DX::ThrowIfFailed(
        CreateStreamOverRandomAccessStream(
        reinterpret_cast<IUnknown*>(fileStream),
        IID_PPV_ARGS(&istream)
        )
    );

注意

若要使用 CreateStreamOverRandomAccessStream 函式,您應該在專案中包含 shcore.h

 

步驟 4:建立 WIC 解碼器並取得框架

使用IWICImagingFactory::CreateDecoderFromStream方法建立IWICBitmapDecoderDecoder物件。

    ComPtr<IWICBitmapDecoder> decoder;
    DX::ThrowIfFailed(
          m_wicFactory->CreateDecoderFromStream(
                    istream.Get(),
                    nullptr,
                    WICDecodeMetadataCacheOnDemand,
                    &decoder
                    )
          );

使用 IWICBitmapDecoder::GetFrame 方法,從解碼器取得影像的第一個畫面格。

    ComPtr<IWICBitmapFrameDecode> frame;
    DX::ThrowIfFailed(
        decoder->GetFrame(0, &frame)
        );

步驟 5:建立 WIC 轉換器並初始化

使用 WIC將影像轉換為 BGRA 色彩格式。 IWICBitmapFrameDecode 會傳回影像的原生像素格式,例如 JPEG 儲存在GUID_WICPixelFormat24bppBGR中。 不過,作為 Direct2D 的效能優化,建議您轉換成 WICPixelFormat32bppPBGRA。

  1. 使用IWICImagingFactory::CreateFormatConverter方法建立IWICFormatConverter物件。

        ComPtr<IWICFormatConverter> converter;
        DX::ThrowIfFailed(
            m_wicFactory->CreateFormatConverter(&converter)
            ); 
    
    
  2. 初始化格式轉換器,以使用 WICPixelFormat32bppPBGRA 並傳入點陣圖框架。

       DX::ThrowIfFailed(
            converter->Initialize(
                frame.Get(),
                GUID_WICPixelFormat32bppPBGRA,
                WICBitmapDitherTypeNone,
                nullptr,
                0.0f,
                WICBitmapPaletteTypeCustom  // premultiplied BGRA has no paletting, so this is ignored
                )
            );
    

IWICFormatConverter介面衍生自IWICBitmapSource介面,因此您可以將轉換器傳遞至點陣圖來源效果。

步驟 6:建立效果並傳入 IWICBitmapSource

使用CreateEffect方法,使用Direct2D裝置內容建立點陣圖來源ID2D1Effect物件。

使用 ID2D1Effect::SetValue 方法,將 D2D1_BITMAPSOURCE_PROP_WIC_BITMAP_SOURCE 屬性設定為 WIC格式轉換器

注意

點陣圖來源效果不會從SetInput方法取得輸入,例如許多Direct2D 效果。 相反地, 會將 IWICBitmapSource 物件指定為 屬性。

 

    ComPtr<ID2D1Effect> bitmapSourceEffect;

    DX::ThrowIfFailed(
        m_d2dContext->CreateEffect(CLSID_D2D1BitmapSource, &bitmapSourceEffect)
        );

    DX::ThrowIfFailed(
        bitmapSourceEffect->SetValue(D2D1_BITMAPSOURCE_PROP_WIC_BITMAP_SOURCE, converter.Get())
        );

    // Insert code using the bitmap source in an effect graph.

現在您已擁有 點陣圖來源 效果,您可以使用它做為任何 ID2D1Effect 的輸入,並建立效果圖。

完整範例

以下是此範例的完整程式碼。

ComPtr<ID2D1Effect> bitmapSourceEffect;

void OpenFilePicker()
{
    FileOpenPicker^ openPicker = ref new FileOpenPicker();
    openPicker->ViewMode = PickerViewMode::Thumbnail;
    openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
    openPicker->FileTypeFilter->Append(".jpg");
    auto pickOperation = openPicker->PickSingleFileAsync();
    
    pickOperation->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>(
          [=](IAsyncOperation<StorageFile^> ^operation, AsyncStatus status)
    {
        auto file = operation->GetResults();
        if (file)
        {
                             auto openOperation = file->OpenAsync(FileAccessMode::Read);
                             openOperation->Completed = ref new
                                      AsyncOperationCompletedHandler<IRandomAccessStream^>(
                                      [=](IAsyncOperation<IRandomAccessStream^> ^operation, AsyncStatus status)
                             {
                                      auto fileStream = operation->GetResults();

                                      // Pass IRandomAccessStream^ into DirectXApp for decoding/processing.
                                      OpenFile(fileStream);
                             });
        }
    });
}

void OpenFile(Windows::Storage::Streams::IRandomAccessStream^ fileStream)
{
    ComPtr<IStream> istream;
    DX::ThrowIfFailed(
        CreateStreamOverRandomAccessStream(
            reinterpret_cast<IUnknown*>(fileStream),
            IID_PPV_ARGS(&istream)
            )
        );

    ComPtr<IWICBitmapDecoder> decoder;
    DX::ThrowIfFailed(
          m_wicFactory->CreateDecoderFromStream(
                    istream.Get(),
                    nullptr,
                    WICDecodeMetadataCacheOnDemand,
                    &decoder
                    )
          );

    ComPtr<IWICBitmapFrameDecode> frame;
    DX::ThrowIfFailed(
        decoder->GetFrame(0, &frame)
        );

    ComPtr<IWICFormatConverter> converter;
    DX::ThrowIfFailed(
        m_wicFactory->CreateFormatConverter(&converter)
        );

    DX::ThrowIfFailed(
        converter->Initialize(
            frame.Get(),
            GUID_WICPixelFormat32bppPBGRA,
            WICBitmapDitherTypeNone,
            nullptr,
            0.0f,
            WICBitmapPaletteTypeCustom  // premultiplied BGRA has no paletting, so this is ignored
            )
        );

       ComPtr<ID2D1Effect> bitmapSourceEffect;

    DX::ThrowIfFailed(
        m_d2dContext->CreateEffect(CLSID_D2D1BitmapSource, &bitmapSourceEffect)
        );

    DX::ThrowIfFailed(
        bitmapSourceEffect->SetValue(D2D1_BITMAPSOURCE_PROP_WIC_BITMAP_SOURCE, converter.Get())
        );

    // Insert code using the bitmap source in an effect graph.
}