Results 1 to 19 of 19

Thread: LoadImage

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    LoadImage

    Every time I try loadimage I get a conversion error....I've tried about 20 different thiings and can't get it... does someone have a sample laying around that uses LoadImage()?

  2. #2
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    This is the code I wrote for a dialog(testing purpose) in a control button clicked function.

    Later, I'm going to do a SDI to make use of the CFileDialog to load the bitmap.

    Code:
    	//The code below, loads the bitmap into a mem DC
    	//then from the memory DC blit to the dialog window.
    	
    	
    	//** Create a paint DC from the dialog window
    	CClientDC clientDC(this);
    
    	//Win32 code below
    	//char Path[]="E:\\svbitmaps\\sv24bit.bmp";
    	//Change the Path and file to the one u want
                    char Path[]="E:\\svbitmaps\\sv8bitbig.bmp";
    
    	HANDLE hImage=LoadImage(NULL,Path,IMAGE_BITMAP,
    		0,0,LR_LOADFROMFILE);
    	if (hImage==NULL){
    		CHAR szBuf[80]; 
    		DWORD dw = GetLastError(); 
     
    		sprintf(szBuf, "%s failed: GetLastError returned %u\n", 
            "LoadImage", dw); 
     
    		//Don't be mistaken; This is MFC version of MessageBox
    		MessageBox(szBuf, "Error", MB_OK); 
    		OnOK();
    	}
    
    	//Structure to hold information about the Bitmap
    	BITMAP Bitmap;
    	//Holds the dimensions of the Bitmap
    	SIZE ImageSize;
    
    	//Get info about the bitmap
    	GetObject(hImage, sizeof(Bitmap), &Bitmap);
    	ImageSize.cx=Bitmap.bmWidth;
    	ImageSize.cy=Bitmap.bmHeight;
    
    	//MFC code starts here
    	//Create compatible memory device context
    	CDC memDC;
    	memDC.CreateCompatibleDC(&clientDC);
    
    	// ** Select it into our memory device context
    	memDC.SelectObject(CBitmap::FromHandle((HBITMAP) hImage));
    
    
    	//do blitting here.
        clientDC.BitBlt(0,0,ImageSize.cx,ImageSize.cy,&memDC,0,0,SRCCOPY);
    	//Delete DC for the Bitmap
    	DeleteObject(hImage);
    If u are going to redraw, u have to modify a bit.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    perfect! thanks soo much!

  4. #4
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Too bad I just found out my code is not perfect.

    I CAN'T do a OnPaint(WM_PAINT). If I tried to call the above routine, it hung my Win98.

    Nvm, here's the solution.
    Code:
    void CLoadBitmapsvDlg::OnDraw() 
    {
    	// TODO: Add your control notification handler code here
    	
    	//The code below, loads the bitmap into a mem DC
    	//then from the memory DC blit to the dialog window.
    	
    	
    	//** Create a paint DC from the dialog window
    	CPaintDC paintDC(this);
    
    	//Win32 code below
    	//char Path[]="E:\\svbitmaps\\sv24bit.bmp";
    	char Path[]="E:\\svbitmaps\\sv8bitbig.bmp";
    
    	HANDLE hImage=LoadImage(NULL,Path,IMAGE_BITMAP,
    		0,0,LR_LOADFROMFILE);
    	if (hImage==NULL){
    		CHAR szBuf[80]; 
    		DWORD dw = GetLastError(); 
     
    		sprintf(szBuf, "%s failed: GetLastError returned %u\n", 
            "LoadImage", dw); 
     
    		//Don't be mistaken; This is MFC version of MessageBox
    		MessageBox(szBuf, "Error", MB_OK); 
    		OnOK();
    	}
    
    	//Structure to hold information about the Bitmap
    	BITMAP Bitmap;
    	//Holds the dimensions of the Bitmap
    	SIZE ImageSize;
    
    	//Get info about the bitmap
    	GetObject(hImage, sizeof(Bitmap), &Bitmap);
    	ImageSize.cx=Bitmap.bmWidth;
    	ImageSize.cy=Bitmap.bmHeight;
    
    	//MFC code starts here
    	//Create compatible memory device context
    	CDC memDC;
    	memDC.CreateCompatibleDC(&paintDC);
    
    	// ** Select it into our memory device context
    	memDC.SelectObject(CBitmap::FromHandle((HBITMAP) hImage));
    
    
    	//do blitting here.
        paintDC.BitBlt(0,0,ImageSize.cx,ImageSize.cy,&memDC,0,0,SRCCOPY);
    	//Delete DC for the Bitmap
    	DeleteObject(hImage);
    }
    Code in blue is what I had changed in order to call it in OnPaint function without crashing

  5. #5
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Steve, if u dun want to always load the bmp file for every WM_PAINT msg, u have to modify, so that it only load the bmp once and for all WM_PAINTs.

    It's simple.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    already modified Right now I only have 1 tiny problem...when I try more than 1 graphic, it only shows 1...

    hDC = BeginPaint( ghWnd_Main, &ps );

    clears the screen and draws on it...so the second image kind of clears the first...any way around this?

  7. #7
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    WM_PAINT will be fired when everything you drew is cleared and it needs repaint. BeingPaint(....) is used to get the handle to the device context when WM_PAINT is fired. It doesn't clear anything because it's already done. Make sure you aren't drawing your second bitmap on top of the other one (like the x any coordinates are same).
    Baaaaaaaaah

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    they're not the same

    and by switching function call order I can show different pictures...thats why i figured something was clearing.

  9. #9
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Steve:Are u doing a MFC or Win32 app?

    I am doing a MFC app. How come u can use my code?

    Code:
    ..........
    HANDLE hImage=LoadImage(NULL,Path,IMAGE_BITMAP,
    		0,0,LR_LOADFROMFILE);
    .........
    ........
    DeleteObject(hImage);
    .......
    U have to always delete hImage before u load a new file.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Win32...but yours was enough to get me to figure it out

  11. #11
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Ignore my previous post

    Maybe I didn't understand your problem.

    Can u rephrase?

  12. #12
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Are you doing exactly the same thing as what transcendental posted? It seems like he's only drawing one bitmap so I don't know if there's any other bitmap or anything else to erase. Can you post your code?
    Baaaaaaaaah

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Let me just explain everything so no ones confused

    I put all the code into a class...the class draws the image.

    But when I try making 2, only 1 will show

    they load different images at different spots to make sure they aren't just overlapping

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    heres the whole cpp file...sorry if its kind of sloppy...its a lot of code lumped into 1. (may not be the most efficient either.

    EDIT

    in WM_PAINT the two calls are commented...that was just part of testing, thats not my problem...
    Attached Files Attached Files
    Last edited by SteveCRM; Sep 1st, 2002 at 09:46 PM.

  15. #15
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    I haven't read your code yet. I'm not the one who requested it.

    I think for every WM_PAINT msg, u have to call these 2 functions to load 2 bitmaps.

  16. #16
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Hmm odd, it seems that if you call BeginPaint() twice, it effects the visibility of your contents. A quick solution would be to call BeginPaint(...) once when you get WM_PAINT message and then call graphics.paint() with the hdc you got your BeginPaint(...). Here's an example of working code:
    PHP Code:
    //Steve Mack

    #include <windows.h>

    HWND ghWnd_Main;

    HINSTANCE ghInst;
    HINSTANCE ghInst2;

    class 
    graphic
    {
    public:
        
    int x;
        
    int y;
        
    char filename[50];

        ~
    graphic(){}
        
    void Paint(HDC hDC);
        
    BOOL LoadBitmapFromBMPFileLPTSTR szFileNameHBITMAP *phBitmap,HPALETTE *phPalette );
        
    HBITMAP CreateBitmapMask(HBITMAP hbmColourCOLORREF crTransparent);
        
    void SetFileName(char file[50]);
    };

    void graphic::SetFileName(char file[50])
    {
        
    strcat(filenamefile);
    }
    BOOL graphic::LoadBitmapFromBMPFileLPTSTR szFileNameHBITMAP *phBitmap,HPALETTE *phPalette )
    {
       
    BITMAP  bm;

       *
    phBitmap NULL;
       *
    phPalette NULL;

       
    // Use LoadImage() to get the image loaded into a DIBSection
       
    *phBitmap = (HBITMAP)LoadImageNULLszFileNameIMAGE_BITMAP00,
                   
    LR_CREATEDIBSECTION LR_DEFAULTSIZE LR_LOADFROMFILE );
       if( *
    phBitmap == NULL )
         return 
    FALSE;

       
    // Get the color depth of the DIBSection
       
    GetObject(*phBitmapsizeof(BITMAP), &bm );
       
    // If the DIBSection is 256 color or less, it has a color table
       
    if( ( bm.bmBitsPixel bm.bmPlanes ) <= )
       {
       
    HDC           hMemDC;
       
    HBITMAP       hOldBitmap;
       
    RGBQUAD       rgb[256];
       
    LPLOGPALETTE  pLogPal;
       
    WORD          i;

       
    // Create a memory DC and select the DIBSection into it
       
    hMemDC CreateCompatibleDCNULL );
       
    hOldBitmap = (HBITMAP)SelectObjecthMemDC, *phBitmap );
       
    // Get the DIBSection's color table
       
    GetDIBColorTablehMemDC0256rgb );
       
    // Create a palette from the color tabl
       
    pLogPal = (LOGPALETTE *)mallocsizeof(LOGPALETTE) + (256*sizeof(PALETTEENTRY)) );
       
    pLogPal->palVersion 0x300;
       
    pLogPal->palNumEntries 256;
       for(
    i=0;i<256;i++)
       {
         
    pLogPal->palPalEntry[i].peRed rgb[i].rgbRed;
         
    pLogPal->palPalEntry[i].peGreen rgb[i].rgbGreen;
         
    pLogPal->palPalEntry[i].peBlue rgb[i].rgbBlue;
         
    pLogPal->palPalEntry[i].peFlags 0;
       }
       *
    phPalette CreatePalettepLogPal );
       
    // Clean up
       
    freepLogPal );
       
    SelectObjecthMemDChOldBitmap );
       
    DeleteDChMemDC );
       }
       else   
    // It has no color table, so use a halftone palette
       
    {
       
    HDC    hRefDC;

       
    hRefDC GetDCNULL );
       *
    phPalette CreateHalftonePalettehRefDC );
       
    ReleaseDCNULLhRefDC );
       }
       return 
    TRUE;
    }

    HBITMAP graphic::CreateBitmapMask(HBITMAP hbmColourCOLORREF crTransparent)
    {
        
    HDC hdcMemhdcMem2;
        
    HBITMAP hbmMask;
        
    BITMAP bm;

        
    GetObject(hbmColoursizeof(BITMAP), &bm);
        
    hbmMask CreateBitmap(bm.bmWidthbm.bmHeight11NULL);

        
    hdcMem CreateCompatibleDC(0);
        
    hdcMem2 CreateCompatibleDC(0);

        
    SelectObject(hdcMemhbmColour);
        
    SelectObject(hdcMem2hbmMask);

        
    SetBkColor(hdcMemcrTransparent);

        
    BitBlt(hdcMem200bm.bmWidthbm.bmHeighthdcMem00SRCCOPY);

        
    BitBlt(hdcMem00bm.bmWidthbm.bmHeighthdcMem200SRCINVERT);

        
    DeleteDC(hdcMem);
        
    DeleteDC(hdcMem2);

        return 
    hbmMask;
    }
    void graphic::Paint(HDC hDC)
    {     
        
    HBITMAP       hBitmaphOldBitmap;
        
    HPALETTE      hPalettehOldPalette;
        
    BITMAP        bm;
        
    HDC hdc;
        
    HBITMAP g_hbmMask;
            
    HDC           hMemDC;
                    if( 
    LoadBitmapFromBMPFilefilename, &hBitmap, &hPalette ) )
                    {
                        
    hMemDC CreateCompatibleDChDC );
                        
    GetObjecthBitmapsizeof(BITMAP), &bm );
                        
    g_hbmMask CreateBitmapMask(hBitmapRGB(000));
                        
    GetObjectg_hbmMasksizeof(BITMAP), &bm );
                        
    hOldBitmap = (HBITMAP)SelectObjecthMemDCg_hbmMask );
                        
    hOldPalette SelectPalettehDChPaletteFALSE );
    //                    RECT rcClient;

                        
    BitBlt(hDCxybm.bmWidthbm.bmHeighthMemDC00SRCAND);


                        
    hOldBitmap = (HBITMAP)SelectObjecthMemDChBitmap );
                        
    hOldPalette SelectPalettehDChPaletteFALSE );
                        
    RealizePalettehDC );


                        
    BitBlthDCxybm.bmWidthbm.bmHeight,    hMemDC00SRCPAINT );


                        
    SelectObjecthMemDChOldBitmap );
                        
    DeleteObjecthBitmap );
                        
    SelectPalettehDChOldPaletteFALSE );
                           
    DeleteObjecthPalette );
                    }
    }
    graphic PIC;
    graphic TEST;

    //function prototypes
    void SetFont(HWND hWndint iPointSize, const char *pcFontName);  //(obviously) sets fonts of controls

    LRESULT CALLBACK WndProc(HWND hWndUINT uMsgWPARAM wParamLPARAM lParam) {
    PAINTSTRUCT   ps;
    HDC hDC;

        switch(
    uMsg) {
            case 
    WM_CLOSE:
                
    DestroyWindow(ghWnd_Main); // Destroy the main window
                
    return 0;

            case 
    WM_DESTROY:
                
    PostQuitMessage(0); // Quit the application
                
    return 0;

            case 
    WM_CREATE:
                
                
                return 
    0;

            case 
    WM_PAINT:  //updates the pic everytime needed
                
    {
        
    hDC BeginPaintghWnd_Main, &ps );
                    
    TEST.Paint(hDC);
                    
    PIC.Paint(hDC);
        
    EndPaint(ghWnd_Main, &ps);
                }
                return 
    0;
            case 
    WM_COMMAND:
                
    //if(LOWORD(wParam) == BN_CLICKED && (HWND)lParam == OPENABOUT) {

                
    return 0;
        }
        return 
    DefWindowProc(hWnduMsgwParamlParam); // Not handled - let the system deal with it
    }

    //==================WINDOW CODE=========================================================
    int WINAPI WinMain(HINSTANCE hInstanceHINSTANCE hPrevInstanceLPSTR lpCmdLineint nCmdShow) {
        
    ghInst hInstance;
        
    ghInst2 hInstance;
        
        
    WNDCLASSEX wcxMyClass;

        
    wcxMyClass.cbSize sizeof(WNDCLASSEX); // Size of structure in bytes
        
    wcxMyClass.style 0// Extra style information
        
    wcxMyClass.lpfnWndProc WndProc// Pointer to window procedure
        
    wcxMyClass.cbClsExtra 0// Extra bytes in class definition
        
    wcxMyClass.cbWndExtra 0// Extra bytes for each window
        
    wcxMyClass.hInstance hInstance// Instance handle for the class
        
    wcxMyClass.hIcon NULL// Default icon - use system
        
    wcxMyClass.hCursor LoadCursor(NULLIDC_ARROW); // Load the system arrow
        
    wcxMyClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE 1); // Background brush
        
    wcxMyClass.lpszMenuName NULL// Menu name [MAKEINTRESOURCE(IDM_MAINMENU)] - none here
        
    wcxMyClass.lpszClassName "TESTCLASS"// Class name
        
    wcxMyClass.hIconSm NULL// Small icon

        
    RegisterClassEx(&wcxMyClass);  // Register the class

        
    ghWnd_Main CreateWindow("TESTCLASS""Graphics Working", \
                                  
    WS_SYSMENU WS_MINIMIZEBOX100100423347, \
                                  
    NULLNULLhInstanceNULL);
        if(
    ghWnd_Main) {
    //        ADDITEM = CreateWindowEx(WS_EX_CLIENTEDGE, "Button", "Add", WS_CHILD | /*WS_DLGFRAME |*/ WS_VISIBLE, 209, 1, 66, 40, ghWnd_Main, NULL, hInstance, NULL);
             

                    
    PIC.100;
                    
    PIC.100;
                    
    PIC.SetFileName("test.bmp");
                    
    TEST.0;
                    
    TEST.0;
                    
    TEST.SetFileName("car.bmp");

            
    ShowWindow(ghWnd_MainnCmdShow); // Show using the specific show style
        
    }
    //    for(int i=0; i<=200; i++)
    //    {
    //        TEST.Paint();
    //        TEST.x = i;
    //    }

        //=================== Handle message loop ==================================================
        
    MSG msg;
        while(
    GetMessage(&msgNULL00)) {
            
    TranslateMessage(&msg); // Translate keycode messages
            
    DispatchMessage(&msg); // Send to the window
        
    }

        
    UnregisterClass("TESTCLASS"hInstance); // Unregister our window class
        
    return msg.wParam// msg.wParam contains the code passed to PostQuitMessage()
    }

    //=========================FUNCTIONS===============================
    void SetFont(HWND hWndint iPointSize, const char *pcFontName) {  //(obviously) sets fonts of controls
        
        
    HFONT hTheFont;
        
    HDC hDC GetDC(hWnd);
        
    int nHeight = -MulDiv(iPointSizeGetDeviceCaps(hDCLOGPIXELSY), 72);

        
    hTheFont CreateFont(nHeight000FW_NORMALFALSEFALSEFALSEDEFAULT_CHARSETOUT_DEFAULT_PRECISCLIP_DEFAULT_PRECISDEFAULT_QUALITYDEFAULT_PITCHpcFontName);

        
    ReleaseDC(hWndhDC);

        
    SendMessage(hWndWM_SETFONT, (WPARAM)hTheFontTRUE);

    Baaaaaaaaah

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Abdul thats perfect! thanks so much for all your time! And everyone else too!

  18. #18
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Hi Steve, is the code above to load a DIB?

    It is strange to use a DDB bltbit function to blit a DIB.

    For DIB, there are StretchDIBits() and SetDIBitsToDevice() APIs which are faster than DDB's equivalents.

    Pls reply as I am looking for source code to load DIBs.

    The CBitmap class in MFC is for DDB and doesn't include a function to load bitmap from files, except from resources.

    That's why I have to use LoadImage() Win32 API and MFC CBitmap together.

    I wonder why MS doesn't create a class to encapsulate DIB as well.
    Last edited by transcendental; Sep 2nd, 2002 at 09:22 PM.

  19. #19
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    He uses DIB sections, which are strange hybrids between DDBs and DIBs. StretchDIBits and SetDIBitsToDevice are slow, especially on WinNT.

    In MFC7 there is the new CImage class which is capable of loading BMP, GIF, JPEG and PCX natively and can be extended by extending GDI+ images to other formats.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width