Load a CBitmap dynamically

前端 未结 8 1777
情深已故
情深已故 2021-02-06 02:59

I have a Bitmap image that i want to load dynamically. But I am unable to load it.

CBitmap bmp;

bmp.LoadBitmap(\"c:\\\\aeimg\");

it does not

相关标签:
8条回答
  • 2021-02-06 03:28

    It could be as simple as you forgetting to escape the backslash. Instead of

    bmp.LoadBitmap("c:\aeimg");
    

    use

    bmp.LoadBitmap("c:\\aeimg");
    

    Otherwise you're passing an invalid path to the LoadBitmap method.

    0 讨论(0)
  • 2021-02-06 03:31

    CBitmap doesn't support directly reading from a .bmp file. You can instead make use of CImage class as suggested in other answers. You'll need to include atlimage.h in your code to make CImage work:

    #include <atlimage.h>
    :
    CImage img;
    img.Load (_T("C:\\image.bmp"));
    CBitmap bitmap;
    bitmap.Attach(img.Detach());
    

    Another way is to load the image using LoadImage Win32 API and then attaching CBitmap to that:

    HBITMAP hBitmap = (HBITMAP)LoadImage(NULL,"c:\\image.bmp", 
                                          IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    if (hBitmap != NULL) 
       bitmap.Attach(hBitmap);
    
    0 讨论(0)
  • 2021-02-06 03:32

    You can also try something like this:

    CImage image;
    image.Load(_T("C:\\image.png"));
    CBitmap bitmap;
    bitmap.Attach(image.Detach());
    
    0 讨论(0)
  • 2021-02-06 03:32

    According to CBitmap documentation: LoadBitmap() function takes resource identifier of the bitmap or resource id of the bitmap.

    You can't specify the path of the bitmap file.

    E.g.

    MyProject.rc
    ------------
    MYBMP      BITMAP  "res\myimage.bmp"
    

    and make sure that resource.h does not have any entry of MYBMP otherwise during preprocessing its replaced by id and ultimately LoadBitmap() will fail since application can't locate the resource as FindResource() fails.

    Now do this :

    CBitmap bmp;
    bmp.LoadBitmap(L"MYBMP");
    

    It will definitely load the bitmap.

    0 讨论(0)
  • 2021-02-06 03:39
    CString filename;
    TCHAR szFilter[] = _T("Bitmap (*.bmp)|*.bmp|PNG (*.png)|*.png||");
    
    CFileDialog selDlg(TRUE, NULL, NULL, OFN_OVERWRITEPROMPT | OFN_EXTENSIONDIFFERENT, szFilter, this);
    
    
    if (selDlg.DoModal() == IDOK)
    {
        filename = selDlg.GetPathName();
    
        CImage image;
    
        HBITMAP hBitmap = (HBITMAP)LoadImage(NULL,filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    
    
        if (hBitmap)
        {
        // Delete the current bitmap
        if (m_bmpBitmap.DeleteObject())
            m_bmpBitmap.Detach();   // If there was a bitmap, detach it
    
        // Attach the currently loaded bitmap to the bitmap object
        m_bmpBitmap.Attach(hBitmap);
    
        Invalidate();
        }
    
    }
    
    0 讨论(0)
  • 2021-02-06 03:40

    To load a bitmap from a file, you want to use LoadImage with the LR_LOADFROMFILE flag.

    0 讨论(0)
提交回复
热议问题