BMP not appearing

Below is the part of the source code for my program. I've created a standard file open dialog. After obtaining the path name for the file, I use LoadBitmap() function to display the bmp..but nothing happens..Did i do something wrong?

This portion is from the CNNDlg.cpp file for fileopendialog

void CNNDlg::OnFileOpenfile()

      char sBMPFile[500];
        //string containing path and file name
      char FileTitle[100];
        //string containing only file name without path

            OPENFILENAME ofn;            
                ofn.lStructSize = sizeof(OPENFILENAME);

            ofn.lpstrFilter =
                TEXT("Bitmap picture files *.bmp\0*.bmp\0ALL Files *.*\0*.*\0\0");

            ofn.nMaxFileTitle= 99;
            ofn.lpstrInitialDir = NULL;
            ofn.lpstrTitle = "Open Fingerprint File";
            ofn.Flags = OFN_FILEMUSTEXIST;
            ofn.lpstrDefExt = "BMP";



            if (sBMPFile[0] == '\0')

            hbitmap =
                    ::LoadBitmap(m_hInstance, sBMPFile);


This portion is from the OnPaint() in the CNNDlg.cpp file
void CNNDlg::OnPaint()
      if (IsIconic())
      CPaintDC dc(this); // device context for painting

      SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

            // Center icon in client rectangle
            int cxIcon = GetSystemMetrics(SM_CXICON);
            int cyIcon = GetSystemMetrics(SM_CYICON);
            CRect rect;
            int x = (rect.Width() - cxIcon + 1) / 2;
            int y = (rect.Height() - cyIcon + 1) / 2;

            // Draw the icon
            dc.DrawIcon(x, y, m_hIcon);
      if (hbitmap!=NULL)
            CPaintDC dc(this);

            // Create a memory DC
            HDC hMemDC = ::CreateCompatibleDC(NULL);

            //Select the bitmap in the memory dc

            //Copy the memory dc into the screen dc
            ::StretchBlt(dc.m_hDC, //destination
                               hMemDC, //Source
            // Delete the memory DC and the Bitmap
            :: DeleteDC(hMemDC);
            :: DeleteObject (hbitmap);


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tommy HuiEngineerCommented:
There are several potential problems I see.

1. Only create one CPaintDC for the entire duration of the OnPaint method. The destructor of CPaintDC calls EndPaint which validates the area and hence you can't draw on the area again.

2. Don't send the WM_ERASEBKGND message because that's already been done for you. And especially don't send somebody else your DC because they may do something to it that you don't expect.

stefiAuthor Commented:
Sorry, I think you have to help me out abit more. I'm having difficulty in understanding what you wrote.

What I noticed is that, the functions yu mentioned where actually written for me by the Appwizard from VC++ 4.0. So do i just delete off the part before
if (hbitmap!=NULL) ?

If not, could yu modify my code so that it works?


1. As I know LoadBitmap is for loading from a resource not from a file
2. Bmp files stored on disks are normaly device independent bitmaps that first must be converted to device depend bitmaps
so you should have a look on DIB inside the online help.
There is also an example DIBLOOK that can show you what you have to do if you want to load a Bitmap from the disk

To show you  here what you have to do ist to much but I think the sample DIBLOOK will show the most.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

stefiAuthor Commented:
Thank you for your answer. I now understand that I cannot use the LoadBitmap function to load a bmp from file.
I've looked at the DIBLOOK program and the source code. If it is possible, could yu help me out here. I've printed all the source code out and am trying to figure out with portion of the code refers to what and how I may be able to use it.
In 'myfile.cpp' there is a portion that loads a DIB from a file. How do I extract that and use it in my program. Basically, what I'm not sure is how I can include this in my program. DO i just type include<myfile.cpp> ? or the h file or what. Please. I'm very new at this.
Something fundamental:
A program written in C or C++ normaly contains more than one source file (.C or .CPP) . Within these files there is the source code of the program. The .H files contains normaly only declarations.
If you want to add a complete source file you do not include the cpp File. If you are using Visual C++ you insert the file into your project.
I have VC++ 4.2 there is a menu entry Insert and within the insert menu there is a entry Files into Project ... there you select the file to add. If you want to have access to functions within the added file from other files the compiler has to know that the functions exists therefore are the header files I make an example:
#include "File1.h"
#include "File2.h"

void MyFuncThatCallsFuncOfOtherProgram()
       printf("The other func returned %d\",CallOtherFuncThatAdds2Int(1,2));
if the compiler compiles these file he has to know something about CallOtherFuncThatAdds2Int
therefore the header files:
void MyFuncThatCallsFuncOfOtherProgram();//declaration of function from file1
int CallOtherFuncThatAdds2Int(int first,int second);

now the compiler knows if he compiles the File1 that there is anywhere a function CallOtherFuncThatAdds2Int that gets 2 integers and returns 1 integer
int CallOtherFuncThatAdds2Int(int first,int second)
  return first+second;
after both files are compiled the linker concatenate the both files to an executebal and seraches for references between the 2 files
the linker now knows where the adresses of the functions are and can concatenate the relations.

if you don't want to add a complete file simple copy the parts you need from one to an other
for example the Ondraw from diblook to the ondraw of your progam

Note: The Diblook program was originally written for Windows 3.x
Thats the reason for the GlobalAlloc GlobalLock and GlobalFree calls.
Because if you use win 95 or win NT there is no real global memory you can replace the mysterious Global... functions and the casting by new and delete.
for example:
HGLOBAL handle =GlobalAlloc(SomeFlags,sizeof(BitmapHeaderInfo);
char* ptr=(LPSTR)GlobalLock(handle);
replace with
BitmapHeaderInfo* ptr=new BitmapHeaderInfo
delete ptr

stefiAuthor Commented:
Thank you for your instruction. I think I can figure it out now..
Hopefully I'm successful. Thanks again

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.