Thread: Problem With My Bitmap

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Unhappy Problem With My Bitmap

    Hi, I have a bitmap located in the same directory as my program.

    I compiled it successfully with my resource editor, specifying the identifyer as "PICTURE".

    Then, in the program I declared a new HBITMAP variable "pic".

    In "case WM_PAINT:" I did:

    pic = LoadBitmap(hInst,"PICTURE");

    if(!pic) {MessageBox(hwnd, "Failed To Load The Bitmap.", "Error",
    MB_OK | MB_ICONEXCLAMATION);
    return -1;
    }

    When the program starts up, the message box pops up, and of course, no bitmap!

    Can anyone spot my mistake?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    Registered User The15th's Avatar
    Join Date
    Aug 2001
    Posts
    125
    it has been awhile since i have displayed a bitmap in any of my programs, but if memory serves me correctly i think you need to have a two hdc handles. i would put that code you have in a WM_CREATE message along with this

    hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
    hBitmap = LoadBitmap (hInst, TEXT ("PICTURE")) ;
    GetObject (hBitmap, sizeof (BITMAP), &bitmap) ;
    cxSource = bitmap.bmWidth ;
    cySource = bitmap.bmHeight ;

    and then in your WM_PAINT message have

    hdc = BeginPaint (hwnd, &ps) ;
    hdcM = CreateCompatibleDC (hdc) ;
    SelectObject (hdcM, hBitmap) ;
    for (y = 0 ; y < cyClient ; y += cySource)
    for (x = 0 ; x < cxClient ; x += cxSource)
    {
    BitBlt (hdc, x, y, cxSource, cySource, hdcM, 0, 0, SRCCOPY) ;
    }
    DeleteDC (hdcM) ;
    EndPaint (hwnd, &ps) ;

    that is some code out of an old project i did. it should work fine.

    oh you may need a WM_SIZE message or else your picture will size to 0 so add this

    case WM_SIZE:
    cxClient = LOWORD (lParam) ;
    cyClient = HIWORD (lParam) ;
    return 0 ;

    anyways i hope i helped.
    arrh, i got nothing good to say.
    http://www.praxis1.vic.edu.au/home/dcola/

  3. #3
    I really should register sometime...
    Guest
    I honestly did this just like two minutes ago. this is how i did it (I couldn't get LoadBitmap to work for me.
    Code:
    pic = (HBITMAP) LoadImage(hInst, "picture.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Change your nickname. You are screwing up this message boards window. Your name is too big.

  5. #5
    Caffienated jinx's Avatar
    Join Date
    Oct 2001
    Posts
    234

    Exclamation Try this....

    Try putting the picture.bmp in your res folder, or the same folder that your icons are stored into. Maybe that'll work.
    Weeel, itss aboot tieme wee goo back too Canada, eeehy boyss.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hmm, LoadImage() is working in the sense that the message box does not pop up telling me it failed to load.

    But still no image.

    Thanks for getting me a little closer!

    Also, I might add, I really hate the fact that all images must be compiled into a resource before using such functions as LoadBitmap(). I really love the fact that LoadImage() can directly grab the file! Awesome.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I THINK I'M ALMOST THERE!

    Here's what I have so far:

    Code:
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
        
        HBITMAP ball;
        HDC hdc = NULL;
        HDC compat_hdc;
        PAINTSTRUCT ps;
        RECT rc;
    
        switch (message)
        {  
    
           case WM_CREATE:
    
            UpdateWindow(hwnd);
    
    if( ball = (HBITMAP) LoadImage(hin, "ball.bmp", IMAGE_BITMAP, rc.right, rc.bottom, LR_LOADFROMFILE) == 0)
           {
                MessageBox(hwnd, "Load of resources failed.", "Error",
                   MB_OK | MB_ICONEXCLAMATION);
           }
    
    if(GetClientRect(hwnd, &rc) == 0)
            {
               MessageBox(hwnd, "GetClientrect failed.", "Error",
                   MB_OK | MB_ICONEXCLAMATION);
            }
    
    if( (compat_hdc = CreateCompatibleDC(hdc)) == NULL)
            {
                MessageBox(hwnd, "CreateCompatibleDC failed.", "Error",
                MB_OK | MB_ICONEXCLAMATION);
            }
    
    if(SelectObject(chdc,ball) == 0)
            {
                MessageBox(hwnd, "Selection failed.", "Error",
                   MB_OK | MB_ICONEXCLAMATION);
            }
    
    if(BitBlt(hdc,0,0,10,10,chdc,0,0,SRCCOPY) == 0)
            {
              MessageBox(hwnd, "Blit failed.", "Error",
                   MB_OK | MB_ICONEXCLAMATION);
            }
            
    
    
            DeleteObject(ball);
    
            DeleteDC(chdc);

    Now the failure is only during the BitBlt() operation! Any Ideas?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Okay. I really don't get it!

    I changed the first parameter of BitBlt() to "compat_hdc" instead of "hdc" ( Shoulda done that in the first place!), and changed the rest to look like this:

    BitBlt(compat_hdc,0,0,rc.right,rc.bottom,compat_hd c,0,0,SRCCOPY)

    ...and no more error message boxes pop up!

    So you'd think I'd have the image by now, but not so!

    Anyway, that's where I'm at.
    Last edited by Sebastiani; 10-27-2001 at 11:47 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Have you tried MAKEINTRESOURCE?
    ie
    LoadBitmap(hInst,MAKEINTRESOURCE(PICTURE));

    Get a DC with GetDC()
    Created a CompatibleDC()
    Created a bitmap with CreateCompatibleBitmap()
    or load the bitmap into the hdc with SelectObject()?
    Then StretchBlt() or BitBlt() it to the hdc?
    BeginPaint returns one or use the one inside the PAINTSTRUCT.

    Code:
    hdc=GetDC(hWnd);
    hdcFrame=CreateCompatibleDC(hdc);
    hFrameBMP=CreateCompatibleBitmap(hdc,ClientRect.right-ClientRect.left,ClientRect.bottom-ClientRect.top);
    hSysFrameBMP=SelectObject(hdcFrame,hFrameBMP);
    ReleaseDC(hWnd,hdc);
    Remember to put the hdc's back EXACTLY as you found them or lose GDI resources.


    Nothing should be done in a paint function except BeginPaint()
    a BitBlt() or if you HAVE to a StretchBlt()
    EndPaint()
    everything else shoould be done to a buffer and them copied to the frame at the last minute. THEN call paint with an UpdateWindow() or InvalidateRect() to copy to the screen.
    Last edited by novacain; 10-29-2001 at 12:05 AM.

  10. #10
    Unregistered
    Guest
    Having looked closer at your code you are not 'Getting' a DC but creating a compatible to NULL one.
    Try GetDC() on the handle of the control/dialog you want to draw on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Bitmap saving problem
    By geek@02 in forum Windows Programming
    Replies: 3
    Last Post: 10-01-2007, 08:44 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. Bitmap Displaying Problem
    By MadCow257 in forum Game Programming
    Replies: 0
    Last Post: 01-25-2005, 05:21 PM
  5. Reading Bitmap File Header Problem.
    By StanleyC in forum C Programming
    Replies: 7
    Last Post: 08-26-2004, 05:54 PM