Thread: (GDI) Basic blitting a bitmap in a window (win7/win8.1 problem)

  1. #1
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135

    Exclamation (GDI) Basic blitting a bitmap in a window (win7/win8.1 problem)

    Hi I want to blit an image bitmap1.bmp using a code snippet I found online. I've tried with both windows 7 and windows 8.1

    As I've understood it, GDI is still supported on these operating systems. I wonder what is wrong, all I get is an empty window with no blitting whatsoever. Maybe you can compile it and try to find what's wrong if you have experience with win32/gdi

    Thanks

    Click for an image of the program with the empty window

    Code:
    #include <windows.h>
    
    
    HINSTANCE hInst;
    
    LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    
    
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    
                       LPSTR lpCmdLine, int nCmdShow)
    
    {
    
        WNDCLASSEX  WndCls;
    
        static char szAppName[] = "BitmapIntro";
    
        MSG         Msg;
    
    
    
    	hInst       = hInstance;
    
        WndCls.cbSize        = sizeof(WndCls);
    
        WndCls.style         = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
    
        WndCls.lpfnWndProc   = WindProcedure;
    
        WndCls.cbClsExtra    = 0;
    
        WndCls.cbWndExtra    = 0;
    
        WndCls.hInstance     = hInst;
    
        WndCls.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    
        WndCls.hCursor       = LoadCursor(NULL, IDC_ARROW);
    
        WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    
        WndCls.lpszMenuName  = NULL;
    
        WndCls.lpszClassName = szAppName;
    
        WndCls.hIconSm       = LoadIcon(hInstance, IDI_APPLICATION);
    
        RegisterClassEx(&WndCls);
    
    
    
        CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
    
                              szAppName,
    
                              "Bitmaps Fundamentals",
    
                              WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    
                              CW_USEDEFAULT,
    
                              CW_USEDEFAULT,
    
                              CW_USEDEFAULT,
    
                              CW_USEDEFAULT,
    
                              NULL,
    
                              NULL,
    
                              hInstance,
    
                              NULL);
    
    
    
        while( GetMessage(&Msg, NULL, 0, 0) )
    
        {
    
            TranslateMessage(&Msg);
    
            DispatchMessage( &Msg);
    
        }
    
    
    
        return (int)(Msg.wParam);
    
    }
    
    
    
    LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg,
    
    			   WPARAM wParam, LPARAM lParam)
    
    {
    
        HDC hDC, MemDCExercising;
    
        PAINTSTRUCT Ps;
    
        HBITMAP bmpExercising;
    
    
    
        switch(Msg)
    
        {
    
    	case WM_DESTROY:
    
    	    PostQuitMessage(WM_QUIT);
    
    	    break;
    
    	case WM_PAINT:
    
    	    hDC = BeginPaint(hWnd, &Ps);
    
    
    
    	    // Load the bitmap from the resource
    
    	    bmpExercising = LoadBitmap(hInst, "bitmap1.bmp");
    
    	    // Create a memory device compatible with the above DC variable
    
    	    MemDCExercising = CreateCompatibleDC(hDC);
    
                 // Select the new bitmap
    
                 SelectObject(MemDCExercising, bmpExercising);
    
    
    
    	    // Copy the bits from the memory DC into the current dc
    
    	    BitBlt(hDC, 10, 10, 450, 400, MemDCExercising, 0, 0, SRCCOPY);
    
    
    
    	    // Restore the old bitmap
    
    	    DeleteDC(MemDCExercising);
    
    	    DeleteObject(bmpExercising);
    
    	    EndPaint(hWnd, &Ps);
    
    	    break;
    
    	default:
    
    	    return DefWindowProc(hWnd, Msg, wParam, lParam);
    
        }
    
        return 0;
    
    }

  2. #2

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Replacing your LoadBitmap line with the following works for loading the bitmap from a file in the same directory as the exe (as opposed to a resource within the exe) :
    Code:
            bmpExercising = LoadImage(NULL, "bitmap1.bmp", IMAGE_BITMAP,
                                      0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by oogabooga View Post
    Replacing your LoadBitmap line with the following works for loading the bitmap from a file in the same directory as the exe (as opposed to a resource within the exe) :
    Code:
            bmpExercising = LoadImage(NULL, "bitmap1.bmp", IMAGE_BITMAP,
                                      0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
    Thanks

    I ended up using CreateBitmap with raw array pixel data. As you wrote, LoadImage is better used for loading a bitmap from a file.

    CreateBitmap function (Windows)

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Tux0r View Post
    I ended up using CreateBitmap with raw array pixel data. As you wrote, LoadImage is better used for loading a bitmap from a file.
    I didn't mean to suggest that LoadImage was better for loading a bitmap from a file. To load a bitmap from the exe module, use:
    Code:
    bmpExercising = LoadImage(hInst, "bitmap1.bmp", IMAGE_BITMAP,
                              0, 0, LR_DEFAULTCOLOR);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I would [not] use hard coded coordinates.
    What I meant.

    If you do scale your bitmap, then you may get better results using "SetMapMode(hDC, MM_ANISOTROPIC);"

    gg

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It should also be mentioned that all these functions have return values that can indicate an error. The load and create ones mentioned so far all return NULL on error, which you should check.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic OpelGLProgram on Win7 using VisualStudio2005
    By ToddlerGraphics in forum C Programming
    Replies: 1
    Last Post: 10-10-2011, 09:37 AM
  2. pipe failure problem between Win7 & XP
    By allynm in forum Networking/Device Communication
    Replies: 5
    Last Post: 03-22-2011, 02:05 PM
  3. Problem with blitting dibSections
    By samGwilliam in forum Windows Programming
    Replies: 4
    Last Post: 01-22-2007, 06:15 PM
  4. map blitting problem
    By Flikm in forum Game Programming
    Replies: 2
    Last Post: 04-17-2002, 03:32 PM
  5. blitting problem
    By cozman in forum Game Programming
    Replies: 5
    Last Post: 04-01-2002, 10:01 PM