Share via


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에서 픽셀 콘텐츠에 액세스하려면 세 가지 대안이 있습니다. 이 아닌 using namespace winrt;, IBufferByteAccess COM 인터페이스의 정의를 가져올 SDK 헤더 파일을 robuffer.h 포함할 수 있습니다. 그러나 using namespace winrt; 매우 일반적이므로 프로젝트의 한 곳에서 IBufferByteAccess 인터페이스를 정의할 수 있습니다(방법을 보려면 C++/WinRT 코드 예제 참조). IBufferByteAccess가 정의되면 이러한 두 가지 기술 중 하나를 사용하여 PixelBuffer에서 IBufferByteAccess의 instance 쿼리할 수 있습니다. 그런 다음 IBufferByteAccess::Buffer 메서드 를 호출하여 픽셀 콘텐츠를 나타내는 바이트 버퍼에 대한 포인터를 검색합니다. C++/WinRT 코드 예제에 표시됩니다. 세 번째 대안(C++/WinRT 코드 예제에도 표시됨)은 를 사용하여 호출WriteableBitmap.PixelBuffer().data()할 수 있는 도우미 함수에서 반환된 를 검색하여 uint8_t*IBufferByteAccess를 완전히 사용하지 않도록 하는 것입니다.
  • C++/CX에서 픽셀 콘텐츠에 액세스하려면 COM 인터페이스인 IBufferByteAccess 인터페이스에 대해 PixelBuffer를 쿼리할 수 있습니다. robuffer.h를 포함합니다. 그런 다음 IBufferByteAccess::Buffer 메서드 를 호출하여 픽셀 콘텐츠를 나타내는 바이트 버퍼에 대한 포인터를 검색할 수 있습니다. C++/CX 코드 예제에 표시됩니다.

적용 대상

추가 정보