draw bitmap to screen

hello,
i've been looking for how to do this, all over the internet for the past two days and i think its about time to ask for some help

im trying to draw a bitmap the the screen/ desktop/ or what ever handle i want to

ive tried the SFML libraries and a bunch or functions that i looked up on MSDN, but i didnt understand and get a clear way to draw that bitmap.

im guessing i need to use SetDIBitsToDevice() but i dont understand how to do that

can somebody please give me an example of how to do it without having to set every pixel seperatly <which is what i've been doing FML>
SFML has a nice example with basic functionality here:

http://www.sfml-dev.org/documentation/1.6/


Drawing a bitmap is simply:

1
2
3
4
5
6
sf::Image img;
img.LoadFromFile("yourimage.bmp");

sf::Sprite spr(img);

yourwindow.Draw(spr);



EDIT: or are you trying to draw to a non SFML window??
Last edited on
non SFML window -.-

if you take a look at this post
http://www.cplusplus.com/forum/beginner/5954/

i have the same problem with the crosshair issue

and one single pixel being changed is very small for me to be able to aim and i made a BMP with the shape i want to be set at the cordanits 512,384
Last edited on
My mistake.

SFML will be of little help, then.

WinGDI isn't fun. Here you go:


1) Load the bitmap with LoadImage (http://msdn.microsoft.com/en-us/library/ms648045(VS.85).aspx). This will give you an HBITMAP

2) Create an offscreen DC with CreateCompatibleDC (use NULL as the parameter): http://msdn.microsoft.com/en-us/library/aa922550.aspx

3) Put the HBITMAP you got into the offscreen DC with SelectObject: http://msdn.microsoft.com/en-us/library/dd162957(VS.85).aspx Be sure to keep the returned value for cleanup.

---- Image is loaded ----
---- keep the HBITMAP and HDC obtained above to draw the image multiple times ----

4) Get the HWND of whatever window you want to draw to (I assume you know how to do this since you said you had a handle)

5) Get that window's DC with GetDC http://msdn.microsoft.com/en-us/library/dd144871(VS.85).aspx

6) "Blit" (copy) the image from your offscreen DC to the window's DC with BitBlt: http://msdn.microsoft.com/en-us/library/dd183370(VS.85).aspx

7) Release the window's DC with ReleaseDC http://msdn.microsoft.com/en-us/library/dd162920(VS.85).aspx

---- Repeat steps 4-7 for as many times as you want to draw the image ----


To cleanup:

8) Select the "old" bitmap back into your offscreen DC with SelectObject (the "old" bitmap is the returned value from the first call to SelectObject)

9) Delete the offscreen DC with DeleteDC() http://msdn.microsoft.com/en-us/library/dd183533(VS.85).aspx

10) Delete the loaded HBITMAP with DeleteObject() http://msdn.microsoft.com/en-us/library/dd183539(VS.85).aspx
thx, will try now
are you sure that #1 give me a HBITMAP, because i get a compiler error, something about converting LPCWSTR, and can u please give me a random sample code

very much appreciated
You sure that error isn't related to the string you are passing as the name of the file to the function?
here is what i have of the code and its not working, can you plz take a look at it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <windows.h>

int main()
{
	HBITMAP menu = LoadImage(NULL,(LPCWSTR)"E:\\Xhair",IMAGE_BITMAP,20,20,LR_LOADFROMFILE);
	HDC hdc = CreateCompatibleDC(NULL);
	SelectObject(hdc,menu);
	while (1)
	{
		HDC hdc_x = GetDC(HWND_DESKTOP);
		BitBlt(hdc_x,0,0,20,20,hdc,0,0,SRCCOPY);	
		ReleaseDC(HWND_DESKTOP,hdc_x);
		Sleep(5);
	}

	return 0;
}
On line 5 you cannot just cast "E:\\Xhair", and char string to a wide-char string. Use L"E:\\Xhair" instead or the Windows macro.
As Zhuge mentioned....

(LPCWSTR)"E:\\Xhair" <-- don't cast around compiler errors unless you know the cast is correct. In this case, that cast is incorrect.

Here are the right ways to do it:

1
2
3
4
HBITMAP menu = LoadImageA(  // note the A 
                 NULL,
                 "E:/Xhair",  // normal char string
                  ....

or
1
2
3
4
HBITMAP menu = LoadImageW( // note the W
                 NULL,
                 L"E:/Xhair",  // wide string (note the L)
                 ...

or
1
2
3
4
HBITMAP menu = LoadImage(  // note neither A nor W
                 NULL
                 _T("E:/Xhair"),  // note the _T() macro
                 ...



LoadImage takes a TCHAR string
LoadImageA takes a char string
LoadImageW takes a wchar_t string

See this for more: http://cplusplus.com/forum/articles/16820/
i keep getting an error that menu is supposed to be a HANDLE and not a HBITMAP,, i read the LoadImage documentation, and it also says that it returns a HANDLE not a HBITMAP ..
That error you can cast around.

An HBITMAP is a HANDLE (the H in HBITMAP stands for handle). It's just that it's C so there's no inheritence, but the idea is the same.

 
HBITMAP menu = (HBITMAP)LoadImage( ...
sry guys but i feel like im becoming a pain in the neck, but i really want to fix this ..

after i did everything mentioned above,i get a blank black box, and the plus sign crosshair doesnt show up ..

here is the code i have right now ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <windows.h>
#include <tchar.h>

int main()
{
	HDC hdc = CreateCompatibleDC(NULL);
	HBITMAP cross = (HBITMAP)LoadImage(NULL, _T("E:\\cross.bmp") ,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
	SelectObject(hdc, cross);
	while (1)
	{
		HDC hdc_x = GetDC(HWND_DESKTOP);
		BitBlt(hdc_x,488,359,48,48,hdc,0,0,LR_LOADFROMFILE);	
		ReleaseDC(HWND_DESKTOP,hdc_x);
	}

	return 0;
}
Look at the C++ Sourcecode page of this site: http://www.cplusplus.com/src/ :scroll to GIF View, it gives an example of how to load and display a BMP there.
i cant do any of those examples cuz i want to draw to the screen and not to a new window..
Hello,

Have you created the window? It not then begin with loading its icon.
It works pretty similar to one in question in conjunction with The Brush context.
im trying to create a console application that would load a bitmap from file and draw it to the screen at the coordinates 512,384 can somebody just write a sample program or help me out with the code that i wrote untill now, to be honest im getting really fustrated, and ive been trying to do this fore 8 days now ..
It isn't worth to feel like that. The logic underneath is quite simple.
A graphic device is one for all programs no matter what GUI or text meaning
an OS has no choice except for lining them up in some kind of queue that is The Device
context, I believe, which is not something visible on a screen.
Begin from the window creation then make sure it is created and ready to be painted on before anything appears on a screen.
At last select the brush&pen starting the drawing still images on an user demand
on the window.
i figered it out !!
i had a problem with 1 of my arguments
here is the solution for other people to look at
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <windows.h>
#include <tchar.h>

int main()
{
	HDC hdc = CreateCompatibleDC(NULL);
	HBITMAP cross = (HBITMAP)LoadImage(NULL, _T("E:\\cross.bmp") ,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
	SelectObject(hdc, cross);
	while (1)
	{
		HDC hdc_x = GetDC(HWND_DESKTOP);
		BitBlt(hdc_x,488,359,48,48,hdc,0,0,SRCCOPY);	
		ReleaseDC(HWND_DESKTOP,hdc_x);
	}

	return 0;
}

Topic archived. No new replies allowed.