How do I create a SoftwaraBitmap from pixel data using C++/WinRT?

hasechon 60 Reputation points
2023-07-04T15:39:42.8533333+00:00

Hi.

I'm developing an application using C++/WinRT and want to display a SoftwareBitmap created from a vector with RGB information on the screen. I've searched the official documentation, but the provided samples are in C# and not C++/WinRT. I need help implementing this in C++/WinRT.

Similar C# sample. > Create or edit a SoftwareBitmap programmatically

Can you guide me on how to create a SoftwareBitmap from pixel data using C++/WinRT? For your reference, I've attached a code snippet (which throws an exception and stops) that I attempted.

winrt::Windows::Foundation::IAsyncAction MainPage::Async_DrawBitmap(int width, int height )
{
    auto lifetime = get_strong();

    SoftwareBitmap softwareBitmap(BitmapPixelFormat::Bgra8, width, height, BitmapAlphaMode::Premultiplied);
    BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode::Write);

    auto reference = buffer.CreateReference();
    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);

    for (int i = 0; i < bufferLayout.Height; i++) {
        for (int j = 0; j < bufferLayout.Width; j++) {
        byte value = (byte)(float)j / bufferLayout.Width * 255;
        reference.data()[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = value; // Blue
        reference.data()[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = value; // Green
        reference.data()[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = value; // Red
        reference.data()[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = 255; // Alpha
        }
    }

    buffer.Close();

    Media::Imaging::SoftwareBitmapSource source;
    co_await source.SetBitmapAsync(softwareBitmap);
    imageControl().Source(source);
}
Developer technologies | Universal Windows Platform (UWP)
Developer technologies | C++
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2023-07-05T07:00:07.78+00:00

    Can you guide me on how to create a SoftwareBitmap from pixel data using C++/WinRT?

    For example, this test creates a SoftwareBitmap with a blue rectangle and sets it to an Image control (Img1) :

      SoftwareBitmap softwareBitmap = GetSoftwareBitmap(400, 400);
      SetImageSourceFromSoftwareBitmap(softwareBitmap, Img1());
    
        SoftwareBitmap GetSoftwareBitmap(int nWidth, int nHeight)
        {
            SoftwareBitmap softwareBitmap(BitmapPixelFormat::Bgra8, nWidth, nHeight, BitmapAlphaMode::Premultiplied);
            BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode::Write);
            uint8_t* pixels = buffer.CreateReference().data();
            int nPixelsSize = nWidth * nHeight * 4;
            for (int i = 0; i < nPixelsSize; i += 4)
            {
                pixels[i] = 255;
                pixels[i + 1] = 0;
                pixels[i + 2] = 0;
            }
            return softwareBitmap;
        }
    
        IAsyncAction SetImageSourceFromSoftwareBitmap(SoftwareBitmap softwareBitmap, winrt::Windows::UI::Xaml::Controls::Image image)
        {
            SoftwareBitmapSource bitmapSource;
            co_await bitmapSource.SetBitmapAsync(softwareBitmap);
            image.Source(bitmapSource);
        }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.