Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Somthing is wrong with Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes (not straight forward to call it) #934

Open
cosmin42 opened this issue Sep 16, 2023 · 0 comments

Comments

@cosmin42
Copy link

cosmin42 commented Sep 16, 2023

I managed to isolate this problem into a simple project: https://github.com/cosmin42/Win2DIssue

Using the CreateFromBytes static method in the below function seems to be impossible to build as it asks for array_view when passing IBuffer and then asking for array_view when passing IBuffer.

void CanvasControlDraw([[maybe_unused]] winrt::Microsoft::Graphics::Canvas::
    UI::Xaml::CanvasControl const& sender,
    [[maybe_unused]] winrt::Microsoft::Graphics::Canvas::
    UI::Xaml::CanvasDrawEventArgs const& args)
{
    auto session = args.DrawingSession();

    Microsoft::Graphics::Canvas::CanvasDevice device =
        Microsoft::Graphics::Canvas::CanvasDevice::GetSharedDevice();

    winrt::array_view<uint8_t const> view_name;

    const auto format = Microsoft::Graphics::DirectX::DirectXPixelFormat::
        B8G8R8A8UIntNormalized;
    auto const& f2 = format;

    winrt::Microsoft::Graphics::Canvas::ICanvasResourceCreator const& resource =
        device;

    winrt::Windows::Storage::Streams::IBuffer buffer;
    Microsoft::Graphics::Canvas::CanvasBitmap bitmap =
        Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes(
            resource, view_name, (int32_t)3,
            (int32_t)3, f2);

    session.DrawImage(bitmap);
}

The build error:

1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(46,60): error C2665: 'winrt::Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes': no overloaded function could convert all the argument types (compiling source file MainWindow.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\Generated Files\winrt\Microsoft.Graphics.Canvas.h(4284,31): message : could be 'winrt::Microsoft::Graphics::Canvas::CanvasBitmap winrt::Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes(const winrt::Microsoft::Graphics::Canvas::ICanvasResourceCreator &,const winrt::Windows::Storage::Streams::IBuffer &,int32_t,int32_t,const winrt::Windows::Graphics::DirectX::DirectXPixelFormat &)' (compiling source file MainWindow.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(46,60): message : 'winrt::Microsoft::Graphics::Canvas::CanvasBitmap winrt::Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes(const winrt::Microsoft::Graphics::Canvas::ICanvasResourceCreator &,const winrt::Windows::Storage::Streams::IBuffer &,int32_t,int32_t,const winrt::Windows::Graphics::DirectX::DirectXPixelFormat &)': cannot convert argument 2 from 'winrt::array_view<const uint8_t>' to 'const winrt::Windows::Storage::Streams::IBuffer &' (compiling source file MainWindow.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(47,31): message : Reason: cannot convert from 'winrt::array_view<const uint8_t>' to 'const winrt::Windows::Storage::Streams::IBuffer' (compiling source file MainWindow.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(47,31): message : No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called (compiling source file MainWindow.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\Generated Files\winrt\Microsoft.Graphics.Canvas.h(4272,31): message : or       'winrt::Microsoft::Graphics::Canvas::CanvasBitmap winrt::Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes(const winrt::Microsoft::Graphics::Canvas::ICanvasResourceCreator &,winrt::array_view<const uint8_t>,int32_t,int32_t,const winrt::Windows::Graphics::DirectX::DirectXPixelFormat &)' (compiling source file MainWindow.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(46,60): message : 'winrt::Microsoft::Graphics::Canvas::CanvasBitmap winrt::Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes(const winrt::Microsoft::Graphics::Canvas::ICanvasResourceCreator &,winrt::array_view<const uint8_t>,int32_t,int32_t,const winrt::Windows::Graphics::DirectX::DirectXPixelFormat &)': cannot convert argument 5 from 'const winrt::Microsoft::Graphics::DirectX::DirectXPixelFormat' to 'const winrt::Windows::Graphics::DirectX::DirectXPixelFormat &' (compiling source file MainWindow.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(48,33): message : Reason: cannot convert from 'const winrt::Microsoft::Graphics::DirectX::DirectXPixelFormat' to 'const winrt::Windows::Graphics::DirectX::DirectXPixelFormat' (compiling source file MainWindow.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(48,33): message : Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or parenthesized function-style cast) (compiling source file MainWindow.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(46,60): message : while trying to match the argument list '(const winrt::Microsoft::Graphics::Canvas::ICanvasResourceCreator, winrt::array_view<const uint8_t>, int, int, const winrt::Microsoft::Graphics::DirectX::DirectXPixelFormat)' (compiling source file MainWindow.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(46,60): error C2665: 'winrt::Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes': no overloaded function could convert all the argument types (compiling source file App.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\Generated Files\winrt\Microsoft.Graphics.Canvas.h(4284,31): message : could be 'winrt::Microsoft::Graphics::Canvas::CanvasBitmap winrt::Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes(const winrt::Microsoft::Graphics::Canvas::ICanvasResourceCreator &,const winrt::Windows::Storage::Streams::IBuffer &,int32_t,int32_t,const winrt::Windows::Graphics::DirectX::DirectXPixelFormat &)' (compiling source file App.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(46,60): message : 'winrt::Microsoft::Graphics::Canvas::CanvasBitmap winrt::Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes(const winrt::Microsoft::Graphics::Canvas::ICanvasResourceCreator &,const winrt::Windows::Storage::Streams::IBuffer &,int32_t,int32_t,const winrt::Windows::Graphics::DirectX::DirectXPixelFormat &)': cannot convert argument 2 from 'winrt::array_view<const uint8_t>' to 'const winrt::Windows::Storage::Streams::IBuffer &' (compiling source file App.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(47,31): message : Reason: cannot convert from 'winrt::array_view<const uint8_t>' to 'const winrt::Windows::Storage::Streams::IBuffer' (compiling source file App.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(47,31): message : No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called (compiling source file App.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\Generated Files\winrt\Microsoft.Graphics.Canvas.h(4272,31): message : or       'winrt::Microsoft::Graphics::Canvas::CanvasBitmap winrt::Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes(const winrt::Microsoft::Graphics::Canvas::ICanvasResourceCreator &,winrt::array_view<const uint8_t>,int32_t,int32_t,const winrt::Windows::Graphics::DirectX::DirectXPixelFormat &)' (compiling source file App.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(46,60): message : 'winrt::Microsoft::Graphics::Canvas::CanvasBitmap winrt::Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes(const winrt::Microsoft::Graphics::Canvas::ICanvasResourceCreator &,winrt::array_view<const uint8_t>,int32_t,int32_t,const winrt::Windows::Graphics::DirectX::DirectXPixelFormat &)': cannot convert argument 5 from 'const winrt::Microsoft::Graphics::DirectX::DirectXPixelFormat' to 'const winrt::Windows::Graphics::DirectX::DirectXPixelFormat &' (compiling source file App.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(48,33): message : Reason: cannot convert from 'const winrt::Microsoft::Graphics::DirectX::DirectXPixelFormat' to 'const winrt::Windows::Graphics::DirectX::DirectXPixelFormat' (compiling source file App.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(48,33): message : Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or parenthesized function-style cast) (compiling source file App.xaml.cpp)
1>C:\Users\user\Win2DIssue\Example\MainWindow.xaml.h(46,60): message : while trying to match the argument list '(const winrt::Microsoft::Graphics::Canvas::ICanvasResourceCreator, winrt::array_view<const uint8_t>, int, int, const winrt::Microsoft::Graphics::DirectX::DirectXPixelFormat)' (compiling source file App.xaml.cpp)

Any idea why the polymorphism seems to fail in this case? The functions seem to be correctly defined under Microsoft.Graphics.Canvas.h .

The workaround is to convert explicitly format to (winrt::Windows::Graphics::DirectX::DirectXPixelFormat const&) when calling the function.

@cosmin42 cosmin42 changed the title Unusable Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes (cannot build) Somthing is wrong with Microsoft::Graphics::Canvas::CanvasBitmap::CreateFromBytes (not straight forward to call it) Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant