Home > Net >  Convert Win32 BITMAP into WinUI3 C /WinRT ImageSource
Convert Win32 BITMAP into WinUI3 C /WinRT ImageSource

Time:08-08

I want to display files in a custom list view, I did all I needed besides displaying the icon for every file extension in the list view.

I found out how to take the HICON/BITMAP for the file extension, but I need to display it in the custom list view like an Image or something like that.

How can I achieve that?

I am working with WinUI3 C /WinRT, up to date packages.

CodePudding user response:

With the help of IInspectable's comment I was able to find the answer for my own question, many thanks to him.

You will need these headers:

#include <winrt/Windows.Storage.h>
#include <winrt/Microsoft.Ui.Xaml.Media.Imaging.h>

This is the async method:

::winrt::Windows::Foundation::IAsyncOperation<::winrt::Microsoft::UI::Xaml::Media::ImageSource>
GetIconFromFile(
    const ::winrt::hstring& file,
    ::winrt::Windows::Storage::FileProperties::ThumbnailMode thumbnailMode = ::winrt::Windows::Storage::FileProperties::ThumbnailMode::DocumentsView
) {
    const auto& fileStorage = co_await ::winrt::Windows::Storage::StorageFile::GetFileFromPathAsync(file);
    const auto& fileThumbnail = co_await fileStorage.GetThumbnailAsync(thumbnailMode);

    ::winrt::Microsoft::UI::Xaml::Media::Imaging::BitmapImage bitmapImage;
    bitmapImage.SetSource(fileThumbnail);

    co_return bitmapImage;
}

You can get rid of the namespaces by using them, but I decided not to do that because ambiguous errors could appear if you have things included from Windows namespace.

And you can use it like this:

// xaml
<Image x:Name="image" Width="40" Height="40"/>

//cpp
::winrt::Windows::Foundation::IAsyncAction LoadImageIcon() {
        const auto& filePath = TEXT("<your file path>");
        image().Source(co_await GetIconFromFile(filePath));
    }

CodePudding user response:

Here is a generic function that does that an HBITMAP => WinUI3 Image.Source conversion:

#pragma comment(lib, "gdi32")
#pragma comment(lib, "oleaut32")

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Storage::Streams; // for InMemoryRandomAccessStream
using namespace Windows::Graphics::Imaging; // for BitmapDecoder
using namespace Microsoft::UI::Xaml;
using namespace Microsoft::UI::Xaml::Controls;  // for Image
using namespace Microsoft::UI::Xaml::Media::Imaging;  // for SoftwareBitmapSource

IAsyncAction SetImageSourceFromHBITMAPAsync(HBITMAP hBitmap, Image image)
{
  PICTDESC pd = {};
  pd.cbSizeofstruct = sizeof(PICTDESC);
  pd.picType = PICTYPE_BITMAP;
  pd.bmp.hbitmap = hBitmap;

  com_ptr<IPicture> picture;
  if (FAILED(OleCreatePictureIndirect(&pd, IID_IPicture, FALSE, (LPVOID*)picture.put()))) // #include <olectl.h> in pch.h
    return;

  InMemoryRandomAccessStream memStream; // #include <winrt/Windows.Storage.Streams.h> in pch.h
  com_ptr<IStream> stream;
  if (FAILED(CreateStreamOverRandomAccessStream(winrt::get_unknown(memStream), IID_PPV_ARGS(&stream)))) // #include <shcore.h> in pch.h
    return;

  if (FAILED(picture->SaveAsFile(stream.get(), TRUE, nullptr)))
    return;

  auto decoder = co_await BitmapDecoder::CreateAsync(memStream); // #include <winrt/Windows.Graphics.Imaging.h> in pch.h
  auto bitmap = co_await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat::Bgra8, BitmapAlphaMode::Premultiplied);

  SoftwareBitmapSource bitmapSource; // #include <winrt/Microsoft.UI.Xaml.Media.Imaging.h> in pch.h
  co_await bitmapSource.SetBitmapAsync(bitmap);
  image.Source(bitmapSource);
}

And an example on how to use it:

namespace winrt::WinUIApp1CPP::implementation
{
    MainWindow::MainWindow()
    {
        InitializeComponent();
    }

    void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
    {
        auto hBitmap = (HBITMAP)LoadImage(nullptr, L"D:\\Downloads\\dog.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);
        if (!hBitmap)
            return;

        auto op{ SetImageSourceFromHBITMAPAsync(hBitmap, myImage()) }; // add this XAML somewhere <Image x:Name="myImage" />
        DeleteObject(hBitmap);
    }
}
  • Related