Thread: Displaying a Bitmap

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    648

    Displaying a Bitmap

    Ok, I have an MFC application with all my interface laid out. I have a CStatic control which I extended into the VideoProcessor class. I have a HBITMAP which I want to display. Now lets pretend, this HBITMAP is an image file (which in reality it isn't). In the OnPaint() function, I want to display this image.

    I've spent 2 days trying to figure this out. I've read tutorial after tutorial, tried code chunk by code chunk. I can draw lines using the CPaintDC class but I can't BitBlt! I've tried to draw it on the parent form's paint event but with no luck either.

    Here's my initialization:
    Code:
    videoBox.Create("", WS_CHILD | WS_VISIBLE | SS_BLACKFRAME, CRect(40, 40, 200, 160), this);
    Note: I've also tried SS_BITMAP instead of SS_BLACKFRAME.

    Code:
    afx_msg void VideoProcessor::OnPaint()
    {
       CPaintDC paint(this);
       HBITMAP bmp = ... ;
       int width = 160, height = 120;
    
       // draw bitmap here
    
       DeleteObject(bmp);
    }
    Please help, I can't believe I'm having so much trouble on such a trivial thing. No wonder people prefer C# or VB. But I *need* to draw this bitmap, asap! Thanks.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You should try to understand the tutuorials/references, instead of copy-n-pasting chunks of code from them.

    Here is the OnPaint() of the attached code:
    Code:
        // our paint DC
        CPaintDC dc(this);
    
        // load up our bitmap into bmME
        CBitmap cbmME;
        
        // for resources use:
        //    cbmME.LoadBitmap(IDB_BITMAP_ME);
    
        // for files use this:
        HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, "avatar.bmp", IMAGE_BITMAP,
                                            0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
        if (hBmp == NULL)
        {
            TRACE0("::LoadImage('avator.bmp') failed");
            return;
        }//if
       
        cbmME.Attach(hBmp);
        
        // use our hidden static frame for bitmap placement
        CRect framerect;
        GetDlgItem(IDC_STATIC_ME)->GetWindowRect(framerect);
        ScreenToClient(framerect);
    
        // create a memory DC
        CDC dcMem;
        dcMem.CreateCompatibleDC(&dc);
    
        // select the bitmap into the memory dc
        CBitmap *old = dcMem.SelectObject(&cbmME);
    
        // get the raw bitmap for access to its attributes
        BITMAP bm;
        cbmME.GetBitmap(&bm);
    
        // blit it
        dc.BitBlt(framerect.left, framerect.top, 
                  bm.bmWidth, bm.bmHeight, 
                  &dcMem, 0, 0, SRCCOPY);
    
        dcMem.SelectObject(old);
        ::DeleteObject(hBmp);
    
        CDialog::OnPaint();
    gg

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Wow, thanks so much! I was reading tutorials but I couldn't figure out what was wrong. I think its because I was getting the DC of the screen or something. Thanks for the code!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmap Displaying Problem
    By MadCow257 in forum Game Programming
    Replies: 0
    Last Post: 01-25-2005, 05:21 PM
  2. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  3. Loading and Displaying Bitmap
    By Master_of_Error in forum C Programming
    Replies: 1
    Last Post: 02-02-2003, 01:21 PM
  4. Displaying a seperate bitmap for each menu button
    By Magriep in forum Windows Programming
    Replies: 1
    Last Post: 03-26-2002, 11:40 PM
  5. Displaying bitmap on db
    By JrKoas38 in forum C++ Programming
    Replies: 1
    Last Post: 09-19-2001, 06:55 PM