[Win32API] BlitBitmap

I have this code to load TGA, convert it to bitmap and then display it in WM_PAINT message, however when I run app nothing happens:

edit: also can someone tell me how do I convert TGA to BMP and display it in static control ? I tried this:

1
2
3
4
5
6
7
8
acImage::Image img;
			int a = acImage::LoadTga("icon.tga", img);
			if(a < 0)
				MessageBox(hWnd, "Error loading Hero Image!", NULL, MB_ICONERROR); return 0;
			acImage::Image bmpTga;
			acImage::ConvertToARGB(bmpTga, img);
			HBITMAP bmp = CreateBitmap(64, 64, 1, 32, bmpTga.data);
			SendMessage(GetDlgItem(hWnd, IDC_HEROIMAGE), STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bmp);



1
2
3
4
5
6
7
8
9
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hdc;
			hdc = BeginPaint(hWnd, &ps);
			LoadAndBlitBitmap("icon.tga", hdc);
			EndPaint(hWnd, &ps);
			break;
		}


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
bool LoadAndBlitBitmap(string FileName, HDC hWinDC)
{
	acImage::Image img;
	int a = acImage::LoadTga("icon.tga", img);
	if(a < 0)
		MessageBox(NULL, "Error loading Hero Image!", NULL, MB_ICONERROR); return false;
	acImage::Image bmpTga;
	acImage::ConvertToARGB(bmpTga, img);
	HBITMAP bmp = CreateBitmap(64, 64, 1, 32, bmpTga.data);

	HDC hLocalDC;
	hLocalDC = CreateCompatibleDC(hWinDC);

	if (hLocalDC == NULL) 
	{
		MessageBox(NULL, "CreateCompatibleDC Failed", "Error", MB_OK);
		return false;
	}

	BITMAP qBitmap;
	int iReturn = GetObject((HGDIOBJ)bmp, sizeof(BITMAP), (LPVOID)&qBitmap);

	if (!iReturn) 
	{
		MessageBox(NULL, "GetObject Failed", "Error", MB_OK);
		return false;
	}

	HBITMAP hOldBmp = (HBITMAP)SelectObject(hLocalDC, bmp);
	if (hOldBmp == NULL)
	{
		MessageBox(NULL, "SelectObject Failed", "Error", MB_OK);
		return false;
	}

	BOOL qRetBlit = BitBlt(hWinDC, 210, 31, 64, 64, hLocalDC, 0, 0, SRCCOPY);
	if (!qRetBlit)
	{
		MessageBox(NULL, "Blit Failed", "Error", MB_OK);
		return false;
	}

	SelectObject(hLocalDC, hOldBmp);
	DeleteDC(hLocalDC);
	DeleteObject(bmp);
	return true;
}


I've also tried another code from google and modified it a little bit, same result :/

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
	case WM_PAINT:
		{
			/*PAINTSTRUCT ps;
			HDC hdc;
			hdc = BeginPaint(hWnd, &ps);
			LoadAndBlitBitmap("icon.tga", hdc);
			EndPaint(hWnd, &ps);*/
			acImage::Image img;
			int a = acImage::LoadTga("icon.tga", img);
			if(a < 0)
				MessageBox(hWnd, "Error loading Hero Image!", NULL, MB_ICONERROR); return 0;
			acImage::Image bmpTga;
			acImage::ConvertToARGB(bmpTga, img);
			HBITMAP bmp = CreateBitmap(64, 64, 1, 32, bmpTga.data);

			HDC hDC, DCHero;
			PAINTSTRUCT Ps;

			hDC = BeginPaint(hWnd, &Ps);
			DCHero = CreateCompatibleDC(hDC);
			SelectObject(DCHero, bmp);
			BitBlt(hDC, 210, 31, 64, 64, DCHero, 0, 0, SRCCOPY);
			DeleteDC(DCHero);
			DeleteObject(bmp);
			EndPaint(hWnd, &Ps);

			break;
		}
Last edited on
You need to read up on how the if statement works:
Then you will see why there is a problem with the following :
1
2
3
4
if(a < 0)
MessageBox(hWnd, "Error loading Hero Image!", NULL, MB_ICONERROR); return 0; //This return statement will always be executed - even if the file load is OK!!!!
acImage::Image bmpTga;
acImage::ConvertToARGB


I leave you to sort that out.


There may or may not be other problems but the one I mentioned above is a FATAL error
Last edited on
jesus christ -.-' I had NO idea this could cause problem, I thought that any statement without {} will read next line only not only one command :/

thanks, anyway, how do I RESIZE that bitmap since it's too large and when I use 64,64 it will display only part of it
Last edited on
A statement ends with a semicolon. Put two statements on the same line, you still have two statements, not one.
how do I resize bitmap? Ive tried

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
bool LoadAndBlitBitmap(string FileName, HDC hWinDC)
{
	acImage::Image img;
	int a = acImage::LoadTga("icon.tga", img);
	if(a < 0)
		MessageBox(NULL, "Error loading Hero Image!", NULL, MB_ICONERROR);
	acImage::Image bmpTga;
	acImage::ConvertToARGB(bmpTga, img);
	HBITMAP bmp = CreateBitmap(bmpTga.width, bmpTga.height, 1, 32, bmpTga.data);
	SetBitmapDimensionEx(bmp, 64, 64, NULL);

	HDC hLocalDC;
	hLocalDC = CreateCompatibleDC(hWinDC);

	if (hLocalDC == NULL) 
	{
		MessageBox(NULL, "CreateCompatibleDC Failed", "Error", MB_OK);
		return false;
	}

	BITMAP qBitmap;
	int iReturn = GetObject((HGDIOBJ)bmp, sizeof(BITMAP), (LPVOID)&qBitmap);

	if (!iReturn) 
	{
		MessageBox(NULL, "GetObject Failed", "Error", MB_OK);
		return false;
	}

	HBITMAP hOldBmp = (HBITMAP)SelectObject(hLocalDC, bmp);
	if (hOldBmp == NULL)
	{
		MessageBox(NULL, "SelectObject Failed", "Error", MB_OK);
		return false;
	}

	BOOL qRetBlit = BitBlt(hWinDC, 210, 31, bmpTga.width, bmpTga.height, hLocalDC, 0, 0, SRCCOPY);
	if (!qRetBlit)
	{
		MessageBox(NULL, "Blit Failed", "Error", MB_OK);
		return false;
	}

	SelectObject(hLocalDC, hOldBmp);
	DeleteDC(hLocalDC);
	DeleteObject(bmp);
	return true;
}


but it won't change bitmap at all
Last edited on
I would say:

1. Create the bitmap to the actual size of the bitmap as loaded from file;
HBITMAP bmp = CreateBitmap(bmpTga.width, bmpTga.height, 1, 32, bmpTga.data);


2. Use StretchBlt function instead of BitBlt function when displaying the bitmap.
Something like:
StretchBlt(hDC, 210, 31, 64, 64, DCHero, 0, 0, bmpTga.width, bmpTga.height,SRCCOPY);


See how you get on with that.

Last edited on
Thanks! stretchblt works fine!
Topic archived. No new replies allowed.