How to read bitmap file as bitmap structure in WinUI3 with GetObject error

hasechon 60 Reputation points
2023-07-30T17:43:29.3233333+00:00

I am developing a desktop application in WinUI3 that deals with bitmap images. I need the image data to be stored in the bitmap structure defined in wingdi.h for compatibility purposes.

Using the Win32API, I've written the following code:

void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
{
    BITMAP bmp;
    LPCWSTR path = L"C:\\test.bmp";
    HBITMAP hbm = (HBITMAP)LoadImage(NULL, path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);

    GetObject(hbm, sizeof(BITMAP), &bmp);
}

However, I'm encountering the following error when building:

Error LNK2019 unresolved external symbol __imp_GetObjectW referenced in function "public: void __cdecl winrt::LoadBmpTest::implementation::MainWindow::myButton_Click(struct winrt::Windows::Foundation::IInspectable const &,struct winrt::Microsoft::UI::Xaml::RoutedEventArgs const &)" (?myButton_Click@MainWindow@implementation@LoadBmpTest@winrt@@QEAAXAEBUIInspectable@Foundation@Windows@4@AEBURoutedEventArgs@Xaml@UI@Microsoft@4@@Z) LoadBmpTest

there an issue with using GetObject in a WinUI3 environment? In any case, I'm interested in storing the image data in a bitmap structure. Are there alternative methods that I can try?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,727 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,846 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 119.6K Reputation points
    2023-07-30T19:45:51.9266667+00:00

    Try to add this line to your code:

    #pragma comment(lib, "gdi32")
    

    Or add Gdi32.lib to Linker, Input, Additional Dependencies field of Project Properties (for all configurations).

    2 people found this answer helpful.
    0 comments No comments

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.