Link to home
Start Free TrialLog in
Avatar of hongjun
hongjunFlag for Singapore

asked on

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
Avatar of nietod
nietod

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);
Avatar of hongjun

ASKER

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
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(...);
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?
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.
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);
Right, I suggested that too.
Sorry I missed that, saw the undef STRICT shouting at me. I should have known better.
Work with windows graphics possible only in Windows, but function main use for dos. If you want work in windows, you use WinMain.
Actually a win32 console program (one with main()) has full access to the windows API.  
Avatar of hongjun

ASKER

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
Because you don't do anything with the Bitmap you have loaded, what do you want to do with it?
Avatar of hongjun

ASKER

The output is nothing. This means even the bitmap is not displayed at all!

hongjun
>> 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.

Avatar of hongjun

ASKER

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

Thanks
hongjun
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.
Avatar of hongjun

ASKER

Adjusted points to 120
Avatar of hongjun

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;
};