简体   繁体   English

使用 MFC 从文件加载 BMP

[英]Load BMP from file by using MFC

I try to load a bmp to my MFC Picture Control.我尝试将 bmp 加载到我的 MFC 优化校准。

void CMFCAppDlg::OnBnClickedButtonload()
{
    CFileDialog dlg(TRUE);
    int result=dlg.DoModal();
    if(result==IDOK)
    {
        MyBmpFile::Instance() -> setPath (dlg.GetPathName());
        UpdateData(FALSE);
    }
    HANDLE hBitmap = LoadImage(0, MyBmpFile::Instance() -> getPath(), IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    CBitmap m_bitmap;
    m_bitmap.Attach((HBITMAP)hBitmap);

    CDC dc, *pDC;
    BITMAP bmp;

    m_bitmap.LoadBitmapW(IDB_BITMAP);
    m_bitmap.GetBitmap(&bmp);

    pDC = this->GetDC();
    dc.CreateCompatibleDC(pDC);
    dc.SelectObject(m_bitmap);
    pDC->BitBlt(200, 200, bmp.bmWidth, bmp.bmHeight, &dc,0 , 0, SRCCOPY);

    m_bitmap.DeleteObject();
    m_bitmap.Detach();
}

This code returns me an error after I select an item in dialog box.此代码在我 select 对话框中的项目后返回错误。 Problem is with LoadImage() it returns NULL .问题在于LoadImage()它返回NULL But actually I dont know what im doing wrong with that.但实际上我不知道我做错了什么。

Ok, I used CImage to draw this bmp, anyway i did not solve the problem with LoadImage() .好的,我用 CImage 来绘制这个 bmp,反正我没有解决LoadImage()的问题。 I try to make it in static way like: L"D:\\e.bmp" or _T("D:\\e.bmp") but even there problem is the same as before.我尝试以 static 的方式制作它,例如: L"D:\\e.bmp"_T("D:\\e.bmp")但即使存在问题也与以前相同。

void CMFCAppDlg::OnBnClickedButtonload()
{
    CFileDialog dlg(TRUE);
    int result=dlg.DoModal();
    if(result==IDOK)
    {
        MyBmpFile::Instance() -> setPath (dlg.GetPathName());
        UpdateData(FALSE);
    }

    CImage image;
    image.Load( MyBmpFile::Instance() ->getPath() );

    CDC dc, *pDC;

    pDC = this->GetDC();
    dc.CreateCompatibleDC(pDC);
    image.Draw(pDC -> GetSafeHdc(),0,0);
}

Following may be of help:以下可能会有所帮助:

INSTANCE hInst = AfxGetInstanceHandle();
HBITMAP hBmp = (HBITMAP)LoadImage(hInst, L"path\to\file.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

Couple of things to check:几件事要检查:

  • Are you using 1-byte or 2-byte characters.您使用的是 1 字节还是 2 字节字符。 Use the L macro for the latter.后者使用L宏。
  • Is the path to your file correctly specified (eg could be relative to where the program happens to run from)是否正确指定了文件的路径(例如,可能与程序恰好运行的位置相关)
  • Can you load the file manually as a bitmap as in the example below?您可以像下面的示例中那样手动将文件加载为 bitmap 吗?

Code to load a file manually as a bitmap:将文件手动加载为 bitmap 的代码:

CFile file;
if (file.Open(L"C:\\Tmp\\Example.bmp", CFile::modeRead))
{
    // Read file header
    BITMAPFILEHEADER bmfHeader;
    if (file.Read((LPSTR) &bmfHeader, sizeof(bmfHeader)) == sizeof(bmfHeader))
    {
        // File type should be 'BM'
        if (bmfHeader.bfType == ((WORD)('M' << 8)| 'B'))
        {
            BITMAPINFOHEADER bmiHeader;
            if (file.Read((LPSTR) &bmiHeader, sizeof(bmiHeader)) == sizeof(bmiHeader))
            {
                int width = bmiHeader.biWidth;
                int height = bmiHeader.biHeight;
            }
        }
    }

    file.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM