Showing Bitmaps

[purple]I'm learning how bitmap images can be shown in a window. I got some questions regarding this bitmap loading and displaying.

1. I tried a .jpg file to load by converting it to .bmp by an image editor. I used [blue]LoadBitmap()[/blue] to load the bitmap. It returned NULL. I clearly stated the path of the .bmp file in resource file. Why it returned NULL?

2. Well, I could successfully load other .bmp files. The book I am following used the following code fragment in WM_PAINT case in window function.[code]
hdc = BeginPaing(hwnd, &ps);

memdc = CreateCompatibleDC(hdc);
SelectObject(memdc, hbit);

//Two call to BitBlt() to show same image
//in two different locations of window

EndPaint(hwnd, &ps);
DeleteDC(memdc);
[/code]
Here are the types used in the code above:
[code]
hdc HDC
memdc HDC
ps PAINTSTRUCT
hbit HBITMAP (loaded at WM_CREATE case of
window function)
hwnd HWND
[/code]
The explanation provided in the book is not enough. The author said, a compatible device context is obtained by the call of [blue]CreateCompatibleDC()[/blue]. The bitmap (hbit) is selected by a call through [blue]SelectObject()[/blue]. These I understand by observing the function names. But my questions are:
>> what is a compatible device context? hdc and memdc are declared as [blue]HDC[/blue]. Why they are not compatible? Why a seperate call to [blue]CreateCompatibleDC()[/blue] is required?
>> Why memdc must hold the bitmap in order to display in hdc (device context associated with the window)?
>> Why the bitmap has to be selected? It is already loaded in hbit.
[/purple]
[hr][purple]~Donotalo()[/purple]

Comments

  • [b][red]This message was edited by AsmGuru62 at 2006-4-30 4:30:35[/red][/b][hr]
    : [purple]I'm learning how bitmap images can be shown in a window. I got some questions regarding this bitmap loading and displaying.
    :
    : 1. I tried a .jpg file to load by converting it to .bmp by an image editor. I used [blue]LoadBitmap()[/blue] to load the bitmap. It returned NULL. I clearly stated the path of the .bmp file in resource file. Why it returned NULL?
    :
    : 2. Well, I could successfully load other .bmp files. The book I am following used the following code fragment in WM_PAINT case in window function.[code]
    : hdc = BeginPaing(hwnd, &ps);
    :
    : memdc = CreateCompatibleDC(hdc);
    : SelectObject(memdc, hbit);
    :
    : //Two call to BitBlt() to show same image
    : //in two different locations of window
    :
    : EndPaint(hwnd, &ps);
    : DeleteDC(memdc);
    : [/code]
    : Here are the types used in the code above:
    : [code]
    : hdc HDC
    : memdc HDC
    : ps PAINTSTRUCT
    : hbit HBITMAP (loaded at WM_CREATE case of
    : window function)
    : hwnd HWND
    : [/code]
    : The explanation provided in the book is not enough. The author said, a compatible device context is obtained by the call of [blue]CreateCompatibleDC()[/blue]. The bitmap (hbit) is selected by a call through [blue]SelectObject()[/blue]. These I understand by observing the function names. But my questions are:
    : >> what is a compatible device context? hdc and memdc are declared as [blue]HDC[/blue]. Why they are not compatible? Why a seperate call to [blue]CreateCompatibleDC()[/blue] is required?
    : >> Why memdc must hold the bitmap in order to display in hdc (device context associated with the window)?
    : >> Why the bitmap has to be selected? It is already loaded in hbit.
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    [blue]This is how Win32 API works with bitmaps. Simply accept it as a given thing. BitBlt() transfers bitmap between contexts. From memory DC it goes to screen DC. (It can go in an opposite way too - from screen to memory - this is how the screenshots are made into bitmaps)

    It calls compatible, because the memory should be compatible with screen. Bitmap can have many color formats, but here we need same format, so CreateCompatibleDC 'looks' into given DC and creates the DC in memory with the identical color settings.

    Then the HBITMAP should be selected into a memory DC, as any other tool you select: pen, brush, font, etc.

    When BitBlt() is done - you have to deselect bitmap out of memory DC, before deleting it.

    Simply read MSDN on bitmaps - it is all provided there.

    Also, look into code samples on my site: www.codexxi.com - I have some bitmap samples.[/blue]


  • [purple]thanks AsmGuru62 for the explanation. i accept that as a given bless :-)

    now i'm presenting another problem i couldn't understand. look at the following fragment of window function code below:[code]
    static int maxX, maxY;
    static HDC memdc;
    static HBRUSH hbrush;
    static HBITMAP hbit;
    HDC hdc;
    PAINTSTRUCT ps;

    switch(message) {
    case WM_CREATE:
    maxX = GetSystemMetrics(SM_CXSCREEN);
    maxY = GetSystemMetrics(SM_CYSCREEN);

    hdc = GetDC(hwnd);
    memdc = CreateCompatibleDC(hdc);
    hbit = CreateCompatibleBitmap(hdc, maxX, maxY);
    [red]SelectObject(memdc, hbit);[/red]
    hbrush = (HBRUSH) GetStockObject(WHITE_BRUSH);
    [red]SelectObject(memdc, hbrush);[/red]
    PatBlt(memdc, 0, 0, maxX, maxY, PATCOPY);
    ReleaseDC(hwnd, hdc);
    break;
    //....
    [/code]
    the above code is creating a virtual window for repaint problem. it selects the handle to the bitmap, and then the brush. isn't it overwriting [blue]memdc[/blue]? how does [blue]memdc[/blue] hold both of the objects?
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • : [purple]thanks AsmGuru62 for the explanation. i accept that as a given bless :-)
    :
    : now i'm presenting another problem i couldn't understand. look at the following fragment of window function code below:[code]
    : static int maxX, maxY;
    : static HDC memdc;
    : static HBRUSH hbrush;
    : static HBITMAP hbit;
    : HDC hdc;
    : PAINTSTRUCT ps;
    :
    : switch(message) {
    : case WM_CREATE:
    : maxX = GetSystemMetrics(SM_CXSCREEN);
    : maxY = GetSystemMetrics(SM_CYSCREEN);
    :
    : hdc = GetDC(hwnd);
    : memdc = CreateCompatibleDC(hdc);
    : hbit = CreateCompatibleBitmap(hdc, maxX, maxY);
    : [red]SelectObject(memdc, hbit);[/red]
    : hbrush = (HBRUSH) GetStockObject(WHITE_BRUSH);
    : [red]SelectObject(memdc, hbrush);[/red]
    : PatBlt(memdc, 0, 0, maxX, maxY, PATCOPY);
    : ReleaseDC(hwnd, hdc);
    : break;
    : //....
    : [/code]
    : the above code is creating a virtual window for repaint problem. it selects the handle to the bitmap, and then the brush. isn't it overwriting [blue]memdc[/blue]? how does [blue]memdc[/blue] hold both of the objects?
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    :
    [blue]There is a room inside DC for some GDI objects. It can hold both pen and brush, but not two brushes - there is only one slot for one object type.

    SelectObject() works for a few types of objects. Device Context can hold inside: brush, pen, font, bitmap, palette and region (I think) - you can check MSDN, but first four types of objects it has for sure. Every time someting selected into DC - the previous (default object, given by Windows) handle is pushed out as a return value from SelectObject(). Important: these default objects should be restored BEFORE releasing DC back to windows with functions, like ReleaseDC(), DeleteDC(), EndPaint(), etc.[/blue]
  • : [b][red]This message was edited by AsmGuru62 at 2006-4-30 4:30:35[/red][/b][hr]
    : : [purple]I'm learning how bitmap images can be shown in a window. I got some questions regarding this bitmap loading and displaying.
    : :
    : : 1. I tried a .jpg file to load by converting it to .bmp by an image editor. I used [blue]LoadBitmap()[/blue] to load the bitmap. It returned NULL. I clearly stated the path of the .bmp file in resource file. Why it returned NULL?
    : :
    : : 2. Well, I could successfully load other .bmp files. The book I am following used the following code fragment in WM_PAINT case in window function.[code]
    : : hdc = BeginPaing(hwnd, &ps);
    : :
    : : memdc = CreateCompatibleDC(hdc);
    : : SelectObject(memdc, hbit);
    : :
    : : //Two call to BitBlt() to show same image
    : : //in two different locations of window
    : :
    : : EndPaint(hwnd, &ps);
    : : DeleteDC(memdc);
    : : [/code]
    : : Here are the types used in the code above:
    : : [code]
    : : hdc HDC
    : : memdc HDC
    : : ps PAINTSTRUCT
    : : hbit HBITMAP (loaded at WM_CREATE case of
    : : window function)
    : : hwnd HWND
    : : [/code]
    : : The explanation provided in the book is not enough. The author said, a compatible device context is obtained by the call of [blue]CreateCompatibleDC()[/blue]. The bitmap (hbit) is selected by a call through [blue]SelectObject()[/blue]. These I understand by observing the function names. But my questions are:
    : : >> what is a compatible device context? hdc and memdc are declared as [blue]HDC[/blue]. Why they are not compatible? Why a seperate call to [blue]CreateCompatibleDC()[/blue] is required?
    : : >> Why memdc must hold the bitmap in order to display in hdc (device context associated with the window)?
    : : >> Why the bitmap has to be selected? It is already loaded in hbit.
    : : [/purple]
    : : [hr][purple]~Donotalo()[/purple]
    : :
    : [blue]This is how Win32 API works with bitmaps. Simply accept it as a given thing. BitBlt() transfers bitmap between contexts. From memory DC it goes to screen DC. (It can go in an opposite way too - from screen to memory - this is how the screenshots are made into bitmaps)
    :
    : It calls compatible, because the memory should be compatible with screen. Bitmap can have many color formats, but here we need same format, so CreateCompatibleDC 'looks' into given DC and creates the DC in memory with the identical color settings.
    :
    : Then the HBITMAP should be selected into a memory DC, as any other tool you select: pen, brush, font, etc.
    :
    : When BitBlt() is done - you have to deselect bitmap out of memory DC, before deleting it.
    :
    : Simply read MSDN on bitmaps - it is all provided there.
    :
    : Also, look into code samples on my site: www.codexxi.com - I have some bitmap samples.[/blue]
    :


    This is only half the truth, actually :-)

    There are two types of bitmaps in Windows: device dependant and device independant bitmaps (DIBs). What you have talked of so far is device dependant bitmaps - they are linked to the project using resource files etc. You load them using LoadBitmap and BitBlt them to the window.

    DIBs are the bmp files stored on your HD. If you want to use those in your program (if you are making your own painting program for example), you have read them as binary files in runtime using file handling. You can send them to the window using SetDIBitsToDevice().

    Everything is described in detail in Petzold's "Programming Windows".
    Get that book if you don't have it already!
  • : : [b][red]This message was edited by AsmGuru62 at 2006-4-30 4:30:35[/red][/b][hr]
    : : : [purple]I'm learning how bitmap images can be shown in a window. I got some questions regarding this bitmap loading and displaying.
    : : :
    : : : 1. I tried a .jpg file to load by converting it to .bmp by an image editor. I used [blue]LoadBitmap()[/blue] to load the bitmap. It returned NULL. I clearly stated the path of the .bmp file in resource file. Why it returned NULL?
    : : :
    : : : 2. Well, I could successfully load other .bmp files. The book I am following used the following code fragment in WM_PAINT case in window function.[code]
    : : : hdc = BeginPaing(hwnd, &ps);
    : : :
    : : : memdc = CreateCompatibleDC(hdc);
    : : : SelectObject(memdc, hbit);
    : : :
    : : : //Two call to BitBlt() to show same image
    : : : //in two different locations of window
    : : :
    : : : EndPaint(hwnd, &ps);
    : : : DeleteDC(memdc);
    : : : [/code]
    : : : Here are the types used in the code above:
    : : : [code]
    : : : hdc HDC
    : : : memdc HDC
    : : : ps PAINTSTRUCT
    : : : hbit HBITMAP (loaded at WM_CREATE case of
    : : : window function)
    : : : hwnd HWND
    : : : [/code]
    : : : The explanation provided in the book is not enough. The author said, a compatible device context is obtained by the call of [blue]CreateCompatibleDC()[/blue]. The bitmap (hbit) is selected by a call through [blue]SelectObject()[/blue]. These I understand by observing the function names. But my questions are:
    : : : >> what is a compatible device context? hdc and memdc are declared as [blue]HDC[/blue]. Why they are not compatible? Why a seperate call to [blue]CreateCompatibleDC()[/blue] is required?
    : : : >> Why memdc must hold the bitmap in order to display in hdc (device context associated with the window)?
    : : : >> Why the bitmap has to be selected? It is already loaded in hbit.
    : : : [/purple]
    : : : [hr][purple]~Donotalo()[/purple]
    : : :
    : : [blue]This is how Win32 API works with bitmaps. Simply accept it as a given thing. BitBlt() transfers bitmap between contexts. From memory DC it goes to screen DC. (It can go in an opposite way too - from screen to memory - this is how the screenshots are made into bitmaps)
    : :
    : : It calls compatible, because the memory should be compatible with screen. Bitmap can have many color formats, but here we need same format, so CreateCompatibleDC 'looks' into given DC and creates the DC in memory with the identical color settings.
    : :
    : : Then the HBITMAP should be selected into a memory DC, as any other tool you select: pen, brush, font, etc.
    : :
    : : When BitBlt() is done - you have to deselect bitmap out of memory DC, before deleting it.
    : :
    : : Simply read MSDN on bitmaps - it is all provided there.
    : :
    : : Also, look into code samples on my site: www.codexxi.com - I have some bitmap samples.[/blue]
    : :
    :
    :
    : This is only half the truth, actually :-)
    :
    : There are two types of bitmaps in Windows: device dependant and device independant bitmaps (DIBs). What you have talked of so far is device dependant bitmaps - they are linked to the project using resource files etc. You load them using LoadBitmap and BitBlt them to the window.
    [purple]
    as far as i have learnt, the location of a DIB has to be defined in the resource file like menus etc. it is loaded using LoadBitmap() providing the name used in the resource file. using the returned handle, it can be shown to window by BitBlt(). I dont know about linking the DDB in resource file!
    [/purple]
    :
    : DIBs are the bmp files stored on your HD. If you want to use those in your program (if you are making your own painting program for example), you have read them as binary files in runtime using file handling. You can send them to the window using SetDIBitsToDevice().
    :
    : Everything is described in detail in Petzold's "Programming Windows".
    : Get that book if you don't have it already!
    :
    [purple]I will check the function SetDIBitsToDevice().

    unfortunately, Petzold's "Programming Windows" is not available in our local market. i was looking for that as u said before.
    [/purple]
    [hr][purple]~Donotalo()[/purple]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories