[Win32API] Loading PNG with GDI+

Jun 6, 2012 at 3:20pm
Hello,

can someone link me some tutorial on how to load PNG image with GDI+? plain winapi, not MFC or any other shit (I'm actually really mad because I can't find it anywhere for fuckin 4 hours, only MFC shit everywhere, I found one but it's not working - source code is at second reply), or just write little WORKING example

I've tried SEVERAL ways, from msdn and this forum, but nothing is working... I'm using dialog created with CreateDialog as main window, everything is in source code...
Last edited on Jun 6, 2012 at 5:58pm
Jun 6, 2012 at 3:33pm
The most recent SDK should have all you require. GDI+ is not a new thing. If you have the latest Windows SDK you also have the ability to code against GDI+.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms533798(v=vs.85).aspx

That's the first link in a Google search for "GDI+". It should explain how it is used.
Jun 6, 2012 at 3:57pm
ooookay, I have this, however it won't display my image... (I found this here on cplusplus.com, maybe you can recommend me some tutorial since I'm soooo mad cuz I'm trying to find something NOT for MFC for like 2 hours and nothing is working)

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <Windows.h>
#include "resource.h"
#include <string>
#include <fstream>
#include <GdiPlus.h>
using namespace std;
using namespace Gdiplus;

GdiplusStartupInput startupInput;
ULONG_PTR token;
Image* ourImage;
HGLOBAL hGlobal;
LPSTREAM pStream;

void getImage();

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(lib, "gdiplus.lib")

BOOL CALLBACK MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

#define MY_WM_INITDIALOG (WM_USER + 1)

INT WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR lpCmdLine, INT nShowCmd)
{
	UNREFERENCED_PARAMETER(hPrevInst);
	UNREFERENCED_PARAMETER(lpCmdLine);

	HMODULE hmodRichEdit = LoadLibrary("Riched20.dll");
	if(hmodRichEdit == NULL)
	{
		MessageBox(NULL, "Can't load RichEdit", NULL, MB_ICONERROR | MB_OK);
		return -1;
	}

	WNDCLASSEX wClass;
	ZeroMemory(&wClass,sizeof(WNDCLASSEX));
	wClass.cbClsExtra=0;
	wClass.cbSize=sizeof(WNDCLASSEX);
	wClass.cbWndExtra=DLGWINDOWEXTRA;
	wClass.hbrBackground=(HBRUSH)(COLOR_BTNFACE + 1);
	wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wClass.hIcon=NULL;
	wClass.hIconSm=NULL;
	wClass.hInstance=hInst;
	wClass.lpfnWndProc=(WNDPROC)MainDlgProc;
	wClass.lpszClassName="Window Class";
	wClass.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);
	wClass.style=CS_HREDRAW | CS_VREDRAW;

	if(!RegisterClassEx(&wClass))
	{
		int nResult = GetLastError();
		MessageBox(NULL, "Window Class Creation Failed!", NULL, MB_ICONERROR | MB_OK);
		return 0;
	}

	HWND hWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, NULL);

	if(!hWnd)
	{
		int nResult = GetLastError();
		MessageBox(NULL, "Dialog Creation Failed!", NULL, MB_ICONERROR | MB_OK);
		return 0;
	}

	MSG msg;
	ZeroMemory(&msg,sizeof(MSG));

	while(GetMessage(&msg, NULL, 0, 0) == TRUE)
	{
		if(!IsDialogMessage(hWnd, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return msg.wParam;
}

BOOL CALLBACK MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	Graphics* gh;
	UNREFERENCED_PARAMETER(lParam);
	switch(msg)
	{
	case MY_WM_INITDIALOG:
		{
			GdiplusStartup(&token,&startupInput,NULL);
			getImage();
		}
		break;
	case WM_PAINT:
		{
			hdc = BeginPaint(hWnd, &ps);
			gh = new Graphics(hdc);
			gh->DrawImage(ourImage, 10, 10);
			delete(gh);
			EndPaint(hWnd, &ps);
		}
	case WM_CREATE:
		{
			PostMessage(hWnd, MY_WM_INITDIALOG, 0, 0);
		}
		break;
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		}
		break;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}

void getImage()
{
	HRSRC hRsrc = FindResource(NULL, "IDR_0_1", RT_RCDATA);
	HGLOBAL hGlob1 = LoadResource(NULL, hRsrc);
	int size = SizeofResource(NULL, hRsrc);
	hGlobal = GlobalAlloc(GMEM_FIXED, size);
	LPVOID resPtr = LockResource(hGlob1);
	memcpy(hGlobal, resPtr, size);
	FreeResource(hGlobal);

	CreateStreamOnHGlobal(hGlobal, true,&pStream);
	ourImage = new Image(pStream, false);
}


resource.rc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Generated by ResEdit 1.5.11
// Copyright (C) 2006-2012
// http://www.resedit.net

#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"




LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDR_0_1            0              "..\\Release\\Untitled.png"



//
// Menu resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDR_MENU1 MENU
{
    POPUP "File"
    {
        MENUITEM "Save", IDM_SAVE1
        MENUITEM "Load", IDM_LOAD1
        MENUITEM "Exit", IDM_EXIT1
    }
    POPUP "Text"
    {
        MENUITEM "Change Color", IDM_CHANGE_COLOR1
        MENUITEM "Change Scale", IDM_CHANGE_SCALE1
    }
    POPUP "Help"
    {
        MENUITEM "About", IDM_ABOUT1
    }
}



//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG1 DIALOG 0, 0, 294, 190
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
EXSTYLE WS_EX_WINDOWEDGE
CAPTION "Dream Journal"
MENU IDR_MENU1
CLASS "Window Class"
FONT 8, "Ms Shell Dlg"
{
    //CONTROL         "", IDC_TEXT1, RICHEDIT_CLASS, WS_TABSTOP | WS_VSCROLL | WS_BORDER | ES_AUTOVSCROLL | ES_NUMBER | ES_MULTILINE | ES_WANTRETURN, 0, 0, 233, 175
    //CONTROL         "", IDC_DATE1, DATETIMEPICK_CLASS, WS_TABSTOP | DTS_RIGHTALIGN, 234, 0, 59, 14
    //DEFPUSHBUTTON   "Save Dream", IDC_SAVE1, 239, 22, 50, 14
    //PUSHBUTTON      "Load Dream", IDC_LOAD1, 239, 37, 50, 14
}


resource.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define IDD_DIALOG1                             101
#define IDR_MENU1                               103
#define IDR_0_1                                 104
#define IDC_DATE1                               1000
#define IDC_TEXT1                               1003
#define IDC_SAVE1                               1007
#define IDC_LOAD1                               1008
#define IDM_SAVE1                               40000
#define IDM_LOAD1                               40001
#define IDM_EXIT1                               40002
#define IDM_CHANGE_COLOR1                       40003
#define IDM_CHANGE_SCALE1                       40004
#define IDM_ABOUT1                              40005 
Last edited on Jun 6, 2012 at 4:00pm
Jun 7, 2012 at 5:43am
bump
Topic archived. No new replies allowed.