未能加载图像错误代码2

Failed to load image error code 2

本文关键字:错误代码 图像 加载      更新时间:2023-10-16

我对编程还很陌生,而且我很难理解作为任务一部分提供给我的代码。

我正在尝试使用代码在程序中绘制位图

void Sprite::Draw(Canvas & c)
{
    draw_impl(c);
        c.DrawBitmap(50, 50, 50, 50, "H:UserDataDesktopArcadeGameArcadeGameImagescircle.bmp", 255, 0, 0);
}

我现在已经更改了它,所以我使用完整路径而不是相对路径。然而,错误仍然存在。

void WinCanvas::DrawBitmap(int x, int y, int w, int h, std::string filename, int r, int g, int b)
{
    std::wstring stemp = std::wstring(filename.begin(), filename.end());    // convert to wide string
    HBITMAP  hBmp = (HBITMAP)LoadImage(NULL, stemp.c_str(), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE | LR_LOADTRANSPARENT);
    if( hBmp == NULL )
    {
        DWORD err = GetLastError();
            std::wstring errorMsg = L"Failed to LoadImage - '" + stemp + L"', error code (" + std::to_wstring((long long)err) + L")";
        MessageBox(NULL, errorMsg.c_str(), L"WinCanvas::DrawBitmap()", MB_OK);
        PostMessage(m_hWnd, WM_DESTROY, NULL, NULL);    // Post a message to destroy (shutdown) the program
        return;
        .
    }

我不确定我的图像位置源是否有问题,或者我是否没有使用正确的整数正确绘制位图。

以下是使用的整数声明

virtual void DrawBitmap(int x, int y, int w, int h, std::string filename, int r=0, int g=0, int b=0);

谢谢你们的帮助。我把代码改成

c.DrawBitmap(50, 50, 50, 50, "H:\UserData\Desktop\ArcadeGame\ArcadeGame\Images\circle.bmp", 255, 0, 0);

然而,它现在出现的错误代码是0,而不是2。如果我读对了,错误消息是说无法加载字符串的字符。

错误代码2:ERROR_FILE_NOT_FOUND. 2 (0x2). The system cannot find the file specified.

  • 确保文件存在
  • 在路径字符串中使用双斜杠,否则将转义下一个字符,类似于:"H:\\UserData\\Desktop\\ArcadeGame\\ArcadeGame\\Images\\circle.bmp"