Load a .bmp or .pcx using C++

I need experts out here to give me source codes that will enable a bitmap or pcx file to load on a C++ program.

Example an object that will load onto a page.

I am only a beginner in writing C++ program and I need lots of help. Please give me the source code and the bitmap or pcx file you have used in creating your program. I am using Visual C++ 6.0 in Windows 98 and NT 4.0 environment.

You can send any source code and image files to asteroids_88@hotmail.com if you want to.

Thanks
hongjun
LVL 33
hongjunAsked:
Who is Participating?
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.

nietodCommented:
The easiest way to load an image from a file is to use the LoadImage() windows API function.  Like

HBITMAP BmpHnd = LoadImage(NULL,"BitMapFileName",IMAGE_BITMAP,0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION);
0
hongjunAuthor Commented:
I got an error when trying to run the below program using nietod's proposed method.

#include <windows.h>

void main()
{
      HBITMAP BmpHnd = LoadImage(NULL,"a",IMAGE_BITMAP,0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION);
}

Error message is shown below when i compile:
'initializing' : cannot convert from 'void *' to 'struct HBITMAP__ *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast

Please tell me why. Here I assume that the filename of the bitmap to be loaded is named as a.

Please help
hongjun
0
nietodCommented:
It is not necessary to reject an answer if you need clarification or additional help.  You can simply post a comment.  You only need to reject an answer if the expert is wrong, which is not the case.

The problem is that LoadImage() returns a generic handle type (HJANDLE) and I was trying to store it in a bitmap handle type--which would be appropriate for this call to LoadImage().  By default you cannot convert one type of windows handle to another type, you can turn this off by adding

#undef STRICT

before including windows.h.  That will allow all windows handles to be treated the same.   This tends to be easier, but does allow you to make more mistakes.   So alternatly you can leave strict on (don't include the #undef STRICT) and force the compiler toconvert the handle returned by LoadImage() by doing

HBITMAP BmpHnd = (HBITMAP) LoadImage(...);
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

AlexVirochovskyCommented:
to Nietod: hongjun asked same Q in Windows area and i 've sent him working code, but he can't translate if ,
becouse he don't undestand difference
between Console/GUI mode and can't set GUI flag in VC. Can you explain him this?
0
nietodCommented:
There is no GUI flag.

It sounds like he needs to create a GUI application project.

Go to:
"File" menu
"New" menu item
From the dialog that appears, choose the "Projects" tab
Choose "Win32 Application" from the list of project types.
Type a name in the "Project name" box.
Press "Ok"
Follow the wizard that appears to select the appropriate type of default options.
0
KangaRooCommented:
I suppose that rather then undefining strict, a single cast would do:
HBITMAP BmpHnd = (HBITMAP)LoadImage(NULL,"BitMapFileName",IMAGE_BITMAP,0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION);
0
nietodCommented:
Right, I suggested that too.
0
KangaRooCommented:
Sorry I missed that, saw the undef STRICT shouting at me. I should have known better.
0
gashevCommented:
Work with windows graphics possible only in Windows, but function main use for dos. If you want work in windows, you use WinMain.
0
nietodCommented:
Actually a win32 console program (one with main()) has full access to the windows API.  
0
hongjunAuthor Commented:
Below is the program I have typed. Though there isn't any compilation error, when I run the program, there isn't any output. I have created a bitmap named a.bmp.

#include <windows.h>

void main()
{
      HBITMAP BmpHnd = (HBITMAP)LoadImage(NULL,"a",IMAGE_BITMAP,0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION);
}

Can you explain why?

Thanks
hongjun
0
KangaRooCommented:
Because you don't do anything with the Bitmap you have loaded, what do you want to do with it?
0
hongjunAuthor Commented:
The output is nothing. This means even the bitmap is not displayed at all!

hongjun
0
KangaRooCommented:
>> there isn't any output
>> ...
>> Can you explain why?
The given program does not produce any output becouse it only loads a Bitmap into memory. A whole more needs to be done to get it on screen, or on a printer, or converted, or ....

What output did you expect and what output do you want.

0
hongjunAuthor Commented:
Oh I see.
I need it to be able to load it onto the screen. Please give me the code.

Thanks
hongjun
0
nietodCommented:
Hongjun, You asked how to load the bitmap, I explaind it, you rejected my answer.  You asked how to create a windows gui progject in VC.  that is a seperate question.  I explained it, you rejected my answer.  Now you are asking a 3rd question...

The easy answer is that you use BitBlt() to copy the bitmap to your window.  

However, your program doesn't have a window.  If you want to know how to do that, that is a 4th question and worth far far more than 100 points just by itself.  If you are not familar with windows programming, you need to get a few books on windows programming and studdy them.  There is a lot of information you need to know and it is not realistic to ask us to supply it--there are whole books on the subject because whole books are needed.  After you read a book or two, then you should ask questions if ther are parts that are not clear.
0
hongjunAuthor Commented:
Adjusted points to 120
0
hongjunAuthor Commented:
Sorry nietod.
Actually my main objective of this question is to display a bitmap onto a screen. Maybe my question is a bit misleading.

I have increased the points for this question to 120.
0
nietodCommented:
Do you know how to write a windows program?  i.e how to create windows, how to write a windo procedure for them? How to create controls, etc?  You have to be able to do that before you can display a bitmap on a window.  And we can't really explain that here.  It takes whole books to do so.  Try "Programming Windows" by Charles Petzold.

Once you have a window and window procedure, you can display the bitmap in the window procedure when a WM_PAINT occurs, like

case WM_PAINT:
{
    PAINTSTRUCT PntStr;
    HDC DstDC = BeginPaint(WndHnd,&PntStr); // DC to paint to.
    HDC SrcDC = CreateCompatibleDC(DstDC); // DC to hold the source bitmap.
    HBITMAP OldBmp = SelectObject(SrcDC,BmpHnd); // Select in your bitmap and save the old bitmap.
    RECT  CltRct;

    GetClientRectangle(WndHnd,&CltRct); // Get client rectangle.
    BitBlt(DstDC,0,0,CltRct.right,CltRct.bottom,SrcDC,0,0,SRCCPY); // copy bitmap to display.
    SelectObject(SrcDC,OldBmp); // Return original bitmap to the DC
    DeleteDC(SrcDC); // Delete the source DC.
    EndPaint(WndHnd,&PntStr); // Finish painting.
}
0

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
nietodCommented:
Here is a sample basic windows program.  But it is no substitute for reading about windows programming.

// Basic window procedure.  This is where you will do most
// of your work to "customize" the window to do what you want.
LRESULT                                          // Result.  Interpretation depends on the        //
                                                           // message being handled.
CALLBACK WndPrc(HWND   WndHnd,                   // >> window.
                UINT   Msg,                      // Message to be handled.
                WPARAM WrdPrm,                   // Word parameter.  Interpretation depends on    
                                                 // message being handled.
                LPARAM LngPrm)                   // Long parameter.  Interpretation depends on
                                                 // message being handled.
{
   BOOL    Handled = TRUE;                       // Was message handled by this routine?
   LRESULT RetVal  = 0;                          // Value to be returned.                         //

   switch (Msg)                                  // Branch based on message.
   {
   case WM_DESTROY:                              // If this is a destroy window message, then    
   {
      // Make application quite when window is closed.
      PostQuitMessage(0);                        // Post a quite message.
      Handled = FALSE;                           // Indicate message was not handled.
      break;
   }
   default:                                      // otherwise, if any other message, then         //
      Handled = FALSE;                           // Indicate message was not handled.             //
   }

   if (!Handled)                                 // If message was not handled, then
      RetVal = DefWindowProc(WndHnd,Msg,WrdPrm,LngPrm);
   return RetVal;                                // Return the return value.  
}
//------------------------------------------------------------------------------------------------//
// Procedure to register the window class.
void
RegWndCls(void)
{
   HCURSOR    ArwCrs = LoadCursor(NULL,IDC_ARROW);  // Default cursor.
   HBRUSH     GryBrh = GetStockObject(LTGRAY_BRUSH); // Background brush.
   WNDCLASSEX WndCls = {sizeof(WNDCLASSEX),   // Create window class parameter block.
                        CS_DBLCLKS,
                        WndPrc,
                        0,
                        0,
                        GetModuleHandle(NULL),
                        NULL,
                        ArwCrs,
                        GryBrh,
                        NULL,
                        "WndClsNam",
                        NULL};
   RegisterClassEx(&WndCls);                     // Try to register the class.
}
//------------------------------------------------------------------------------------------------//
// Procedure to create a window.
HWND  // >> window created.
CrtWnd(char *Ttl) // Window title.
{
   return CreateWindow("QWnd",Ttl,
                       WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                       CW_USEDEFAULT,CW_USEDEFAULT,
                       CW_USEDEFAULT,CW_USEDEFAULT,
                       NULL,NULL,GetModuleHandle(NULL),NULL);
}
//------------------------------------------------------------------------------------------------//
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,
                   int nShowCmd)
{
   RegWndCls(); // Register the window class.

   MSG      WndMsg;
   HWND WndHnd = CrtWnd("Window Title"); // Create a window.

   // Process messages until a quit message.
   while (GetMessage(&WndMsg,NULL,0,0))
   {
      DispatchMessage(&WndMsg);
   }
   return WndMsg.wParam;
};
0
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
C++

From novice to tech pro — start learning today.