Problem loading image bitmap

There is nothing wrong with the code...it compiles and links without problem.But the image does not show up.
Anyone have any ideas or are there any include files that are needed for bitmap operations? Thanks.


BOOL WINAPI LoadBMPIntoDC ( HDC hDC, char* bmpfile ) {

    HDC dcmem;

    if ( ( NULL == hDC ) || ( NULL == bmpfile ) )  {MessageBox(hwnd,"Could not get HDC or bmpfile","ERROR",MB_OK|MB_ICONERROR); return FALSE;}

    hBmp = LoadImage (NULL,bmpfile,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

  // HBITMAP hBmp = LoadImage ( hInstance, MAKEINTRESOURCE(imageid), IMAGE_BITMAP, 0, 0,
   //LR_DEFAULTCOLOR );

     if (NULL == hBmp)  {MessageBox(hwnd,"Could not get loadimage","ERROR",MB_OK|MB_ICONERROR);return FALSE; }

   dcmem = CreateCompatibleDC (hDC );

   if ( NULL == SelectObject(dcmem,hBmp) ) {  MessageBox(hwnd,"Could not get selectobject","ERROR",MB_OK|MB_ICONERROR);
        DeleteDC ( dcmem );
   return FALSE;
   }

    GetObject (hBmp,sizeof(bm),&bm);

   if (BitBlt(hDC,0,0,bm.bmWidth,bm.bmHeight,dcmem,0,0,SRCCOPY) == 0) {
    MessageBox(hwnd,"Could not get bitblt","ERROR",MB_OK|MB_ICONERROR);
    DeleteDC ( dcmem );
   return FALSE;
   }


 DeleteDC(dcmem );

 return TRUE;
   
}

called with..


     dc =GetDC(hwnd);
     LoadBMPIntoDC(dc,"C:\\windows\\setup.bmp");
LVL 1
librazoneAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
DanRollinsConnect With a Mentor Commented:
Good news!  Your code works.  Well, you lied just a teensy bit when you said:

>> it compiles and links without problem

But after added a BITMAP strucure and made a few other minor tweeks, it worked perfectly.

BOOL WINAPI LoadBMPIntoDC ( HDC hDC, char* bmpfile ) {

     HDC dcmem;
     BITMAP bm;
     HBITMAP hBmp;

     if ( ( NULL == hDC ) || ( NULL == bmpfile ) )  {
          MessageBox(0,"Could not get HDC or bmpfile","ERROR",MB_OK|MB_ICONERROR); return FALSE;
     }
     hBmp= (HBITMAP)LoadImage(NULL,bmpfile,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
     if (NULL == hBmp)  {
          MessageBox(0,"Could not get loadimage","ERROR",MB_OK|MB_ICONERROR);return FALSE;
     }
     dcmem= CreateCompatibleDC( hDC );
     if ( NULL == SelectObject(dcmem,hBmp) ) {  
          MessageBox(0,"Could not get selectobject","ERROR",MB_OK|MB_ICONERROR);
          DeleteDC ( dcmem );
          return FALSE;
     }
     GetObject( hBmp, sizeof(bm),&bm );
     if ( BitBlt(hDC,0,0,bm.bmWidth,bm.bmHeight,dcmem,0,0,SRCCOPY) == 0) {
          MessageBox(0,"Could not get bitblt","ERROR",MB_OK|MB_ICONERROR);
          DeleteDC ( dcmem );
          return FALSE;
     }
     DeleteDC(dcmem );
     return TRUE;
}
=-=-=-=-=-=-=-=-
And I called it with this:

     HDC dc= ::GetDC( m_hWnd );
     LoadBMPIntoDC(dc,"C:\\temp\\test.bmp");

=-=-=-=-=-=-=-=-
Here is something that you may not know:  If you just draw a bitmap onto a window, then the next time that window does a normal update, your drawing will vanish.  For instance, if you are using an IDE and single-step debugging, then odds are, your program windo is getting covered up by the debugging window.  You draw the bitmap and then the debugger covers the window, then the next time your window gets uncovered, the image is gone!

In Windows programming, you need to do all drawing in the WM_PAINT handler.  If you need more help. let me know.

-- Dan
0
 
librazoneAuthor Commented:
Yeah ok thanks.Actually I had been trying it both ways with the function and also via the WM_PAINT command.But I couldnt get either to work.
One question why do we need a HBITMAP cast for the LoadImage...doesnt it automatically return that type.
0
 
librazoneAuthor Commented:
Is that all I have to change is the HBITMAP cast and also do the struct declarations have to be local.I had two of them global.
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
librazoneAuthor Commented:
It now works fine from the module but still having problems from the WM_CREATE
with the following

case WM_PAINT:

          {  
     HDC hdc,hmemdc;

             
        hdc= BeginPaint(hwnd,&ps);
        hmemdc= CreateCompatibleDC(hdc);
     
        SelectObject(hmemdc,hBmp);
        GetClientRect(hwnd,&rc);
     GetObject(hmemdc, sizeof(bm), &bm);

    BitBlt(hdc,0,0,rc.right,rc.bottom,hmemdc,0,0,SRCCOPY);
       DeleteDC(hmemdc);
       EndPaint(hwnd,&ps);
  }
 
      break;

using

hBmp=(HBITMAP)LoadBitmap(hInst,MAKEINTRESOURCEIDB_MYBITMAP));

in WM_CREATE
0
 
librazoneAuthor Commented:
It now works fine from the module but still having problems from the WM_CREATE
with the following

case WM_PAINT:

          {  
     HDC hdc,hmemdc;

             
        hdc= BeginPaint(hwnd,&ps);
        hmemdc= CreateCompatibleDC(hdc);
     
        SelectObject(hmemdc,hBmp);
        GetClientRect(hwnd,&rc);
     GetObject(hmemdc, sizeof(bm), &bm);

    BitBlt(hdc,0,0,rc.right,rc.bottom,hmemdc,0,0,SRCCOPY);
       DeleteDC(hmemdc);
       EndPaint(hwnd,&ps);
  }
 
      break;

using

hBmp=(HBITMAP)LoadBitmap(hInst,MAKEINTRESOURCEIDB_MYBITMAP));

in WM_CREATE
0
 
DanRollinsCommented:
>>hBmp=(HBITMAP)LoadBitmap(hInst,MAKEINTRESOURCEIDB_MYBITMAP));

There is a syntax error there.  So that is your problem.  Onece you fix the syntax error, may sure that IDB_MYBITMAP is a value resource in your EXE (hBmp will be 0 after the call if it is an invalid resource identifier).

Place break points.  Examine variables.  That is how to solve problems.  If that does not help, take a few moments to describe exactly what is going wrong.  A comment like "but still having problems..." is not too enlightening to me, it provides no clue as to the nature of the problem.

-- Dan
0
 
librazoneAuthor Commented:
Sorry my fault...I hadnt added my rc file to the project.The syntax error wasnt in the program.
0
All Courses

From novice to tech pro — start learning today.