News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Displaying a .BMP

Started by Roger, June 27, 2006, 11:04:58 PM

Previous topic - Next topic

Roger

Hi All,

I am trying to translate the sample given in 'Opening and Displaying a .BMP File' in Win32.hlp into assembler (GoAsm). I have a difficulty with two parts:-

/*  Create a bitmap from the data stored in the .BMP file. */

hbm = CreateDIBitmap(hdc, &bmih, CBM_INIT, lpvBits, lpbmi, DIB_RGB_COLORS);

/* Unlock the global memory objects and close the .BMP file.  */

GlobalUnlock(hmem1);
GlobalUnlock(hmem2);
CloseHandle(hfbm);

/* Set the fDisplayBitmap flag. */

if (hbm)
     fDisplayBitmap = TRUE;
else
     TextOut(hdc, 100, 100, "LoadBitmap Failed", 17);

/* Paint the window (and draw the bitmap). */

GetClientRect(hwnd, &rect);
InvalidateRect(hwnd, &rect, TRUE);
UpdateWindow(hwnd);

Once the bitmap data is retrieved, the bitmapped image can be drawn in the application's client area.
The following code sample is used to draw the bitmap.

case WM_PAINT: 
    BeginPaint(hwnd, &ps); 
        if (fDisplayBitmap) {
            hdcMem = CreateCompatibleDC(ps.hdc);
            SelectObject(hdcMem, hbm);
            GetObject(hbm, sizeof(BITMAP), (LPSTR) &bm);
            BitBlt(ps.hdc, 0, 0, bm.bmWidth, bm.bmHeight,
                hdcMem, 0, 0, SRCCOPY);
            DeleteDC(hdcMem);
        }
    EndPaint(hwnd, &ps);
    break;

I am stuck by hdc used in CreateDIBitmap and TextOut. The code sample does not setup hdc and as far as I can see the code has not created a device context at this stage. I have tried a CreateCompatableDC just before the CreateDIBitmap but I am not happy with this first because the code example does not do this and second because, as yet, it hasn't worked.

The bitmap is passed to WM_PAINT by its handle hbm, but how does the error string from TextOut get there? All the examples of TextOut I have seen put TextOut into the WM_PAINT code so it can use the handle provided by BeginPaint.

Regards Roger

MichaelW

The example displays on the client area of the window, so you would use GetDC to get the device context, and ReleaseDC to release it when you have finished using it. TextOut just needs a valid DC; it should work OK outside of BeginPaint - EndPaint.

eschew obfuscation

Tedd

You don't need to create a separate dc to draw to the screen with - this is done for you when you call BeginPaint; and the handle for this is returned by BeginPaint (so you could say "hDC = BeginPaint(hwnd,&ps)"). This hdc is also stored in the paintstruct anyway, which is why BitBlt uses "ps.hdc".
Although, you do need to create the dc for selecting the bitmap into first, so you can then draw it to the screen dc.

QuoteThe bitmap is passed to WM_PAINT by its handle hbm, but how does the error string from TextOut get there? All the examples of TextOut I have seen put TextOut into the WM_PAINT code so it can use the handle provided by BeginPaint.
Nothing is passed to wm_paint - it has no parameters; and due to the way it's actually handled internally, you shouldn't be sending wm_paint messages either - InvalidateRect will cause one to be generated as required. "hbm" is a global variable, so it can be accessed from anywhere, including within handling of wm_paint, you could the same for (a pointer to) the error string if needed. Though the usual method is more along the lines of a messagebox or text in an edit control.


[bitmap displaying example - from resource object rather than file, but once it's loaded into memory, the procedure is the same.]

[attachment deleted by admin]
No snowflake in an avalanche feels responsible.

Roger

Hi Michael & Tedd,

Thank you for your replies, I have it working - after a fashion - using GetDC/ Release DC.

Quote from: Tedd on June 28, 2006, 11:28:24 AM
Nothing is passed to wm_paint - it has no parameters; and due to the way it's actually handled internally, you shouldn't be sending wm_paint messages either - InvalidateRect will cause one to be generated as required. "hbm" is a global variable, so it can be accessed from anywhere, including within handling of wm_paint, you could the same for (a pointer to) the error string if needed. Though the usual method is more along the lines of a messagebox or text in an edit control.
Perhaps I expressed myself badly. I wasn't trying to explicitly pass the bitmap to WM_PAINT because, as you say, it is handled internally. However there must be some way in which the 'internals' can determin what it is that I want painted. As you say hbm works for the bitmap but not for the Text.

Having spent several decades programming embeded systems where everything is documented and all code is availiable for examination, I find using Windows APIs like fighting blindfold; especially when help files appear to have vital bits missing.


Regards Roger

Tedd

Quote from: Roger on June 30, 2006, 08:26:52 PM
Perhaps I expressed myself badly. I wasn't trying to explicitly pass the bitmap to WM_PAINT because, as you say, it is handled internally. However there must be some way in which the 'internals' can determin what it is that I want painted. As you say hbm works for the bitmap but not for the Text.

hbm works for the bitmap because it identifies the image data that you want painted. To paint text, you give the address of the text (string of characters) you want painted and it is rendered into pixels for you; there isn't any handle for the text, and it must be re-rendered each time (unless you go to the complication of rendering it into a bitmap and then painting that each time - which isn't worth it.)
The problem you may be having is with trying to use hdc outside of the wm_paint handling, where you shouldn't really assume hdc is still the same as it was previously (it shouldn't change very often, but it does change.)
If you want to post (semi-)working code, then we can have a poke at it (and it's easier than guessing) :wink
No snowflake in an avalanche feels responsible.

Roger

Tedd,

Quote from: Tedd on July 04, 2006, 11:55:11 AM
If you want to post (semi-)working code, then we can have a poke at it (and it's easier than guessing) :wink

Thank you for your offer. I have the .bmp displayed and, at last, staying displayed so I have moved on to creating new problems code to actually use the .bmp. Since this doesn't need any APIs I expect to make rapid progess :lol

Regards Roger

jorgon

Roger

I just came across your earlier post, but I thought you may be interested in another easier way to display a BMP file directly from the file, using GDI+!
Author of the "Go" tools (GoAsm, GoLink, GoRC, GoBug)