[gelöst] GDI+: Problem beim Erstellen eines Image-Objekts



  • Hallo,

    ich möchte etwas ganz Einfaches machen: ein PNG in meiner WinAPI darstellen lassen. Das Problem ist, dass ich kein Objekt der erzeugen kann. Mein Code:

    //Global pointer
    ULONG_PTR token;
    ...
    
    	case WM_CREATE:
    		{
    			Gdiplus::GdiplusStartupInput startupInput;
    			Gdiplus::GdiplusStartup(&token, &startupInput, NULL);
    			return 0;
    
    		} break;
    
    	case WM_PAINT:
    		{ 
    			PAINTSTRUCT ps; 
    			HDC hdc = BeginPaint(hWndDlgNew, &ps); 
    
    			using namespace Gdiplus;
    
    			Graphics graphics(hdc);
    			Image image(L"D:\\UbuntuSHARE\\Help1greyblur.png");
    			graphics.DrawImage(&image, 5, 10);
    
    			EndPaint(hWndDlgNew, &ps); 
    
    			return 0;
    		} break;
    

    Bei dem Code in der Form erhalte ich einen Laufzeitfehler:

    First-chance exception at 0x772463d2 in OptSync.exe: 0xC0000005: Access violation reading location 0x00000044.
    Unhandled exception at 0x772463d2 in OptSync.exe: 0xC0000005: Access violation reading location 0x00000044.

    Der Debugger zeigt auf die Methode
    Ich habe auch Folgendes probiert, leider auch erfolglos (wahrscheinlich weil diese Methode nur für bmp-Dateien geeignet ist):

    ...
    			const wchar_t path[33] = L"D:\\UbuntuSHARE\\Help1greyblur.png";
    			Gdiplus::Bitmap *pBitmap = Gdiplus::Bitmap::FromFile(path);
    
    			if( pBitmap )
    			{
    				Gdiplus::Graphics g(hdc);
    				g.DrawImage(pBitmap, 10, 10);
    			}
    			else
    				MessageBox(hWndDlgNew, "Could not draw.", "no", NULL);
    
    			delete pBitmap;
    ...
    

    Hier wird die entsprechende MessageBox aufgerufen. Kann mir jemand helfen?

    Grüße,
    Rewind.



  • Es hat sich herausgestellt, dass GDI+ nicht in der Windows-Prozedur gestartet und beendet werden muss, sondern in der WinMain:

    int WINAPI WinMain (HINSTANCE hI, HINSTANCE hPrI, PSTR szCmdLine, int iCmdShow)
    {
    	char szName[] = "OSmainClass";
    	WNDCLASS wc;
    
    	Gdiplus::GdiplusStartupInput StartupInput;
    	ULONG_PTR                    Token;
    
    	Gdiplus::GdiplusStartup(&Token, &StartupInput, NULL);
    
    	wc.style         = CS_HREDRAW | CS_VREDRAW;   // CS = "class style"
    	//... [styles, etc.]
    
    	ShowWindow   (hWnd, iCmdShow);
    	UpdateWindow (hWnd);
    
    	// Message loop
    	MSG msg;
    	while (GetMessage (&msg, NULL, 0, 0))
    	{
    		TranslateMessage (&msg);
    		DispatchMessage (&msg);
    	}
    
    	Gdiplus::GdiplusShutdown(Token);
    
    	return msg.wParam;
    }
    

    Ich dachte, ich komme nie auf die Lösung...


Anmelden zum Antworten