Home > database >  Unable to load image during WinApi c programming
Unable to load image during WinApi c programming

Time:12-14

I am doing WinApi C programming with my colleague. We are using the visual studio. While I was loading the image, there was an error like the one in the picture below.

Error Image

While debugging, I found out that this is an error caused by the lack of files on the path. You can simply add a file to the path to resolve the error, but you want to import the image from another specified path. The reason why you don't choose a simple method is that even though your colleagues use the same code, they succeed in importing files from different paths, so they don't get errors.

The path where I import the file is 'D:\BreakTogether\OutPut\bin\Res', and the path where my colleague imports the file is 'D:\BreakTogether\bin\Res'. Below is the code used to get the file path.

#include "pch.h"
#include "ResMgr.h"
#include "PathMgr.h"
#include "Image.h"

ResMgr::ResMgr()
{
}

ResMgr::~ResMgr()
{
    /*map<wstring, Image*>::iterator iter;
    for (iter = m_mapImg.begin(); iter != m_mapImg.end();   iter)
    {
        delete iter->second;
    }*/
    Safe_Delete_Map(m_mapImg);
}

Image* ResMgr::ImgLoad(const wstring& _strKey, const wstring& _strRelativePath)
{
    Image* pImg = ImgFind(_strKey);
    if (nullptr != pImg)
    {
        return pImg;
    }
    wstring strFilePath = PathMgr::GetInst()->GetRsrcPath();
    strFilePath  = _strRelativePath;
    pImg = new Image;
    pImg->Load(strFilePath);
    pImg->SetKey(_strKey);
    pImg->SetRelativePath(_strRelativePath);
    m_mapImg.insert(make_pair(_strKey, pImg));
    //  m_mapImg.insert({ _strKey , pImg });
    return pImg;
}

Image* ResMgr::ImgFind(const wstring& _strKey)
{
    auto iter = m_mapImg.find(_strKey);
    if (iter == m_mapImg.end())
    {
        return nullptr;
    }
    return static_cast<Image*>(iter->second);
}

#include "pch.h"
#include "PathMgr.h"
#include "Core.h"

PathMgr::PathMgr()
    : m_szRsrcPath{}
{
}

PathMgr::~PathMgr()
{
}

void PathMgr::Init()
{
    GetCurrentDirectory(255, m_szRsrcPath);

    int Length = wcslen(m_szRsrcPath);

    for (int i = Length - 1; i >= 0; i--)
    {
        if (m_szRsrcPath[i] == '\\')
        {
            m_szRsrcPath[i] = '\0';
            break;
        }
    }

    wcscat_s(m_szRsrcPath, 255, L"\\bin\\Res\\");

    SetWindowText(Core::GetInst()->GetWndHandle(), m_szRsrcPath);
}

I tried adding an image to the path where I load the file. That will solve the problem. However, it is inefficient because my colleague and I have to add the same file twice because we have different paths to load the image.

CodePudding user response:

If you can load the file normally when the image file is in place, there is no problem with your code. Go to your Project Property Pages -> Debugging -> Working Directory and align your path with your colleagues. If the problem is not solved, plz tell me more about your problem. Have a nice day!

  • Related