WriteableBitmap.PixelBuffer プロパティ

定義

WriteableBitmap の各ピクセルが書き込まれる直接バッファーのアクセス権を取得します。

public:
 property IBuffer ^ PixelBuffer { IBuffer ^ get(); };
IBuffer PixelBuffer();
public IBuffer PixelBuffer { get; }
var iBuffer = writeableBitmap.pixelBuffer;
Public ReadOnly Property PixelBuffer As IBuffer

プロパティ値

ピクセル バッファーへの参照。

このコード例では、WriteableBitmapPixelBuffer プロパティを使用して、そのピクセル コンテンツに書き込みます。

C# の例は、より大きなコード サンプルである SDK XAML イメージ のサンプルに由来します。 示されている C# コードは、最終的に WriteableBitmapImage.Source 値として使用し、イメージを表示するコード変換シナリオの一部です。

他の言語の例は、もう少しスコープや自己完結型です。

using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) 
{
    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); 
    // Scale image to appropriate size 
    BitmapTransform transform = new BitmapTransform() {  
        ScaledWidth = Convert.ToUInt32(Scenario4WriteableBitmap.PixelWidth), 
        ScaledHeight = Convert.ToUInt32(Scenario4WriteableBitmap.PixelHeight)
    }; 
    PixelDataProvider pixelData = await decoder.GetPixelDataAsync( 
        BitmapPixelFormat.Bgra8, // WriteableBitmap uses BGRA format 
        BitmapAlphaMode.Straight, 
        transform, 
        ExifOrientationMode.IgnoreExifOrientation, // This sample ignores Exif orientation 
        ColorManagementMode.DoNotColorManage
    ); 

    // An array containing the decoded image data, which could be modified before being displayed 
    byte[] sourcePixels = pixelData.DetachPixelData(); 

    // Open a stream to copy the image contents to the WriteableBitmap's pixel buffer 
    using (Stream stream = Scenario4WriteableBitmap.PixelBuffer.AsStream()) 
    { 
        await stream.WriteAsync(sourcePixels, 0, sourcePixels.Length); 
    }                     
}
// You'll need to add the Pictures Library capability to your Package.appxmanifest file.

// MainPage.xaml
...
<Image x:Name="anyExampleImage" Width="100" Height="100"/>
...

// pch.h
...
#include <winrt/Windows.Graphics.Imaging.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.UI.Xaml.Media.Imaging.h>
struct __declspec(uuid("905a0fef-bc53-11df-8c49-001e4fc686da")) IBufferByteAccess : ::IUnknown
{
    virtual HRESULT __stdcall Buffer(uint8_t** value) = 0;
};
...

// MainPage.h
...
struct MainPage : MainPageT<MainPage>
{
    ...
    Windows::Foundation::IAsyncAction ClickHandler(Windows::Foundation::IInspectable const&, Windows::UI::Xaml::RoutedEventArgs const&);
private:
    Windows::UI::Xaml::Media::Imaging::WriteableBitmap m_writeableBitmap{ nullptr };
};
...

// MainPage.cpp
...
Windows::Foundation::IAsyncAction MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&)
{
    uint32_t scaledSize = 100;
    m_writeableBitmap = Windows::UI::Xaml::Media::Imaging::WriteableBitmap(scaledSize, scaledSize);

    Windows::Storage::StorageFolder picturesFolder{ Windows::Storage::KnownFolders::PicturesLibrary() };
    auto anyExampleImageFile{ co_await picturesFolder.GetFileAsync(L"anyexampleimage.png") };
    Windows::Storage::Streams::IRandomAccessStream fileStream{ co_await anyExampleImageFile.OpenAsync(Windows::Storage::FileAccessMode::Read) };
    auto decoder{ co_await Windows::Graphics::Imaging::BitmapDecoder::CreateAsync(fileStream) };

    // Scale the image to the appropriate size.
    Windows::Graphics::Imaging::BitmapTransform transform;
    transform.ScaledWidth(scaledSize);
    transform.ScaledHeight(scaledSize);

    Windows::Graphics::Imaging::PixelDataProvider pixelData{ co_await decoder.GetPixelDataAsync(
        Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, // WriteableBitmap uses BGRA format 
        Windows::Graphics::Imaging::BitmapAlphaMode::Straight,
        transform,
        Windows::Graphics::Imaging::ExifOrientationMode::IgnoreExifOrientation, // This sample ignores Exif orientation 
        Windows::Graphics::Imaging::ColorManagementMode::DoNotColorManage
    ) };

    // An array containing the decoded image data, which could be modified before being displayed 
    winrt::com_array<uint8_t> sourcePixels{ pixelData.DetachPixelData() };

    // COMMENT OUT EXACTLY ONE OF TECHNIQUE 1/2
    // TECHNIQUE 1; QI for IBufferByteAccess.
    auto bufferByteAccess{ m_writeableBitmap.PixelBuffer().as<::IBufferByteAccess>() };
    uint8_t * pTargetBytes{ nullptr };
    bufferByteAccess->Buffer(&pTargetBytes);
    // TECHNIQUE 2; use a C++/WinRT helper function (and delete the definition of IBufferByteAccess in pch.h).
    //uint8_t * pTargetBytes{ m_writeableBitmap.PixelBuffer().data() };

    for (auto & element : sourcePixels)
    {
        *(pTargetBytes++) = element;
    }

    anyExampleImage().Source(m_writeableBitmap);
}
...
// pch.h
...
#include <robuffer.h>
...

// MainPage.xaml.cpp
auto writeableBitmap{ ref new Windows::UI::Xaml::Media::Imaging::WriteableBitmap(100, 100) };

::IUnknown* pUnk{ reinterpret_cast<IUnknown*>(writeableBitmap->PixelBuffer) };
Microsoft::WRL::ComPtr<Windows::Storage::Streams::IBufferByteAccess> bufferByteAccess;
HRESULT hr{ pUnk->QueryInterface(IID_PPV_ARGS(&bufferByteAccess)) };

byte *pBuffer{ nullptr };
bufferByteAccess->Buffer(&pBuffer);

// Now, write into the WriteableBitmap by using pBuffer. For example, make the first pixel red.
*pBuffer = 0xFF; ++pBuffer;
*pBuffer = 0xFF; ++pBuffer;
*pBuffer = 0x0; ++pBuffer;
*pBuffer = 0x0;

注釈

注意

コード例のプログラミング 言語 を選択するには、(タイトルの近くにある) 言語ピッカーを使用します。

PixelBuffer によって返される IBuffer は、 に直接書き込むことができません。 ただし、言語固有の手法を使用して、バッファー内の基になるピクセル コンテンツに書き込むことができます。

  • C# または Microsoft Visual Basic からピクセル コンテンツにアクセスするには、 WindowsRuntimeBufferExtensions.AsStream メソッド を使用して、基になるバッファーにストリームとしてアクセスできます。 これは、C# のコード例に示されています。
  • C++/WinRT からピクセル コンテンツにアクセスするには、3 つの選択肢があります。 でない using namespace winrt;限り、SDK ヘッダー ファイル robuffer.h を含めて 、IBufferByteAccess COM インターフェイスの定義を取り込むことができます。 ただし、 は非常に一般的であるため using namespace winrt; 、プロジェクト内の 1 か所で IBufferByteAccess インターフェイスを定義することもできます (その方法については、C++/WinRT コード例を参照してください)。 これら 2 つの手法のいずれかを使用して IBufferByteAccess を定義したら、IBufferByteAccess のインスタンスに対して PixelBuffer に対してクエリを実行できます。 次に 、IBufferByteAccess::Buffer メソッド を呼び出して、ピクセル コンテンツを表すバイトのバッファーへのポインターを取得します。 これは、C++/WinRT コード例に示されています。 3 番目の代替方法 (C++/WinRT コード例でも示されています) は、 で呼び出WriteableBitmap.PixelBuffer().data()すことができるヘルパー関数から返される をuint8_t*取得することで、IBufferByteAccess を完全に使用しないようにすることです。
  • C++/CX からピクセル コンテンツにアクセスするには、COM インターフェイスである IBufferByteAccess インターフェイスに対して PixelBuffer に対してクエリを実行します。 robuffer.h を含めます。 その後、 IBufferByteAccess::Buffer メソッド を呼び出して、ピクセル コンテンツを表すバイトバッファーへのポインターを取得できます。 これは、C++/CX コード例に示されています。

適用対象

こちらもご覧ください