?
Solved

Problem loading image bitmap

Posted on 2003-03-09
7
Medium Priority
?
486 Views
Last Modified: 2013-12-03
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");
0
Comment
Question by:librazone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
ID: 8100526
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
 
LVL 1

Author Comment

by:librazone
ID: 8101906
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
 
LVL 1

Author Comment

by:librazone
ID: 8101920
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 1

Author Comment

by:librazone
ID: 8103634
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
 
LVL 1

Author Comment

by:librazone
ID: 8103654
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 8106143
>>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
 
LVL 1

Author Comment

by:librazone
ID: 8109257
Sorry my fault...I hadnt added my rc file to the project.The syntax error wasnt in the program.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question