Loading bitmap image

I am having trouble getting the bitmap image to actually load, I've tried using different file formats from .png to .bmp and no success. The resource is embedded in the program and I've tried using the full file path as a parameter as well. Any help please? The GDI is just an incremental part to get to directx, so for demonstrational purposes, this is how I have to code for now.

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
133
134
135
136
137
138
139
140
141
142
143
144
#include <windows.h>
#include <winuser.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define APPTITLE "Dungeon Runners"

LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
ATOM MyRegisterClass(HINSTANCE);
BOOL InitInstance(HINSTANCE,int);
void DrawBitmap(HDC,char*,int,int);
void Game_Init();
void Game_Run();
void Game_End();

HWND global_hwnd;
HDC global_hdc;

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	global_hwnd = hWnd;
	global_hdc = GetDC(hWnd);
	switch(message)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	// create the window and fill with info
	WNDCLASSEX wc; 
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC)WinProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = NULL;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = APPTITLE;
	wc.hIconSm = NULL;
	return RegisterClassEx(&wc);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;
	hWnd = CreateWindow(
		APPTITLE,
		APPTITLE,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		500,
		400,
		NULL,
		NULL,
		hInstance,
		NULL);
	if(!hWnd) // test for errors
		return FALSE;

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	return TRUE;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, // windows main function
				   LPSTR lpCmdLine, int nCmdShow)
{
	MessageBox(NULL, "A Game by Joseph Tomlin!\nA game of great adventure and treasure.",
		"Welcome to Dungeon Runners!", MB_OK | MB_ICONEXCLAMATION);
	MSG msg;
	int done = 0;
	MyRegisterClass(hInstance);

	if(!InitInstance(hInstance, nCmdShow)) // initialize application
		return FALSE;

	Game_Init(); // initialize the game

	while(!done) // main game loop
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT) // loop continuation test
				break;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		Game_Run(); // function to run game regardless of messages
	}
	Game_End();
	return msg.wParam;
}

void Game_Init()
{
	// initialize game, load all assets, initialize random number generator
	srand(time(NULL));
}

void Game_Run()
{
	// refreshes the bitmap every frame
	RECT rect;
	GetClientRect(global_hwnd, &rect);
	if(rect.right > 0)
	{
		DrawBitmap(global_hdc, "FirstBitmap.bmp", 0, 0);
	}
}

void Game_End()
{
}

void DrawBitmap(HDC hdcDest, char* filename, int x, int y)
{
	HBITMAP image;
	BITMAP bm;
	HDC hdcMem;
	image = (HBITMAP)LoadImage(0, "FirstBitmap.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	GetObject(image, sizeof(BITMAP), &bm);
	hdcMem = CreateCompatibleDC(global_hdc);
	SelectObject(hdcMem, image);
	BitBlt(
		global_hdc,
		x, y,
		bm.bmWidth, bm.bmHeight,
		hdcMem,
		0, 0,
		SRCCOPY);
	DeleteDC(hdcMem);
	DeleteObject((HBITMAP)image);
}
You are doing a couple of things wrong here:

1. You should not really hang on to a Display Context the way you are doing it. You should get the DC use it and release it.
1
2
3
4
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	global_hwnd = hWnd;
	//global_hdc = GetDC(hWnd);//So get rid of this line and the associated global variable. 


2. The major problem that was causing your bitmap not to display was the way you were selecting
it in and out of the context.
Try this (Note picture.bmp was just a bitmap I had to test with):
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
void Game_Run()
{
	// refreshes the bitmap every frame
	RECT rect;
	GetClientRect(global_hwnd, &rect);

    //Select the destination Context
    HDC hdc = GetDC(global_hwnd);

	if(rect.right > 0)
	{
		DrawBitmap(hdc, "picture.bmp", 0, 0);
	}

    //Release the DC
    ReleaseDC(global_hwnd, hdc);
}

void Game_End()
{
}

void DrawBitmap(HDC hdcDest, char* filename, int x, int y)
{
	HBITMAP image, hOldBitmap;
	BITMAP bm;
	HDC hdcMem;
	image = (HBITMAP)LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	GetObjectA(image, sizeof(BITMAP), &bm);
	hdcMem = CreateCompatibleDC(hdcDest);
	
    //Important line - save the existing context bitmap
    hOldBitmap = (HBITMAP)SelectObject(hdcMem, image);//<<========****
	
    
    BitBlt(
		hdcDest,
		x, y,
		bm.bmWidth, bm.bmHeight,
		hdcMem,
		0, 0,
		SRCCOPY);
    
    //Restore the saved bitmap
    SelectObject(hdcMem, hOldBitmap);//<<======*****
	
    DeleteDC(hdcMem);
	DeleteObject((HBITMAP)image);
}

Last edited on
Topic archived. No new replies allowed.