#windows gdiplus help

31 messages · Page 1 of 1 (latest)

fallen spade
#

need help trying to get an image to display on the screen using gdiplus and windows.h

relevant code:

#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "gdiplus.lib")
//#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <gdiplus.h>
#include <CommCtrl.h>
#include <Commdlg.h>
#include <string>
#include <fstream>

Image* g_Image = nullptr;
ULONG_PTR g_gdiplusToken{};

void LoadImageFile(HWND hwnd)
{
    wchar_t szFile[MAX_PATH]{};

    OPENFILENAMEW ofn{};
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = szFile;
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrFilter = L"Images\0*.png;*.jpg;*.bmp\0";
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

    if (GetOpenFileNameW(&ofn))
    {
        delete g_Image;
        g_Image = Image::FromFile(szFile, FALSE);

        if (g_Image->GetLastStatus() != Ok)
        {
            delete g_Image;
            g_Image = nullptr;
            MessageBoxW(hwnd, L"Failed to load image", L"GDI+ Error", MB_ICONERROR);
            return;
        }

        InvalidateRect(hwnd, nullptr, TRUE);
    }
}

obsidian brambleBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

fallen spade
#

this is all relevant code

#

Oh yeah i forgot to add the info about what does not work.

blazing viper
#

is g_image a valid pointer after construction?

fallen spade
#

it's a valid one but it has lastresult = 3 (out of memory)

#

g_Image = 0x000001d6cb461ef0 {nativeImage=0x0000000000000000 <NULL> lastResult=OutOfMemory (3) loadStatus=Ok (0) }

this is all info for g_Image after i've done fromfile();

blazing viper
#

Do you have read access to that file and it's not locked, correct?

fallen spade
#

well I did not do anything special to it afaik. I can try putting it in a different folder.

blazing viper
#

Also, is the path too long?

#

And is it an absolute path or a relative?

#

Also, sanity check, you've initialized GdiPlus, right?

fallen spade
#

its an absolute path.
C:\DAE\year 2\programming 3\Week_9\temp.png

i doubt this is too long

blazing viper
#

It's not

fallen spade
#
//at the top below includes
ULONG_PTR g_gdiplusToken{};

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow) 
{

    GdiplusStartupInput gdiplusStartupInput;
    GdiplusStartup(&g_gdiplusToken, &gdiplusStartupInput, nullptr);

    const wchar_t CLASS_NAME[] = L"BasicWin32App";
    WNDCLASSW wc{};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;
    wc.hbrBackground = CreateSolidBrush(RGB(255, 255, 255));
    wc.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(240));
    RegisterClassW(&wc);
    HWND hwnd = CreateWindowExW(0, CLASS_NAME, L"Textbox + Button Demo",
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        1200, 800, nullptr, nullptr, hInstance, nullptr);

    g_mainWindow = hwnd;


    ShowWindow(g_mainWindow, nCmdShow);

    MSG msg{};
    while (GetMessageW(&msg, nullptr, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
    return 0;
}
blazing viper
#

is it a valid image file?

#

OOM can be a lot of weirdness with GDI

fallen spade
#

it's a png file. Should i try changing it to a bmp one?

#

and i can open the file without trouble

#

outside of the program

blazing viper
#

it should support PNGs

fallen spade
#

uhm thanks

#

i dont know how

#

probably when i was messing around with saving text files

#

But i overwrote the data in the png

#

so it was in fact not a valid file

#

that was the issue

#

thanks

blazing viper
#

no problem

fallen spade
#

!resolved

obsidian brambleBOT
# obsidian bramble

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity