首页 > 解决方案 > 无法在 Microsoft Visual Studio 2017 C++ 项目中在编译时添加和包含资源

问题描述

我正在尝试将图像添加到 C++ 应用程序。在解决方案资源管理器中单击New Item -> Resource创建的Resource.rc文件。在Resource View添加的新资源中,即Bitmap图像。该resource.h文件是

#define IDB_BITMAP1                     101

#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        102
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1001
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif   

Resource.rc - 相关部分

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENN)LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_INDIA
// Bitmap
IDB_BITMAP1             JPG                     "Street.jpg"
#endif    // English (India) resources

该程序在 IDE 内运行时没有错误。但是当执行build生成的exe时,似乎找不到资源图像文件。我无法在编译时包含资源。

更新

加载图像并生成输出图像的 C++ 示例代码

#include <iostream>
#include "Open_eVision_2_11.h"
using namespace Euresys::Open_eVision_2_11;
using namespace std;
int main(){
    EImageBW8 EBW8Image1; // EImageBW8 instance
    // Display version - To make sure lib is included properly.
    std::cout << "Open eVison Version: " << Easy::GetVersion(); 
    try {
        EBW8Image1.Load("Street.jpg");
        EasyImage::Threshold(&EBW8Image1, &EBW8Image1, 140);
        EBW8Image1.SaveJpeg("output_image.jpg");
        std::cout << "\nImage generated!\n";
    }catch (const EException&) {
        std::cout << "Error: Possibly image not found!\n";
    }
}

当程序在 IDE 中执行时,程序会生成输出图像。但是,当使用此链接创建安装程序并使用Visual Studio Installer和执行它进行安装时,发现资源图像未正确加载。它打印出库版本和捕获部分。这个问题是否与使用的库有关Open_eVision_2_11.h

标签: c++visual-studiobuildvisual-studio-2017

解决方案


推荐阅读