如何在非窗体文件中使用项目资源修改PictureBox图片?
Let's fix that file path headache once and for all. The problem with Image::FromFile is that it relies on the current working directory, which breaks the second you move your program outside the project folder. Embedded resources are baked directly into your executable, so they work no matter where you run the app. Here's how to make this work in your Figure class:
Step 1: Double-Check Your Resource Setup
First, confirm your images are properly configured as embedded resources:
- Right-click your project in Solution Explorer → Properties → Resources.
- Add your
EMPTY.png,WhiteKing.png, andBlackKing.pngfiles using "Add Existing Item". - For each image, open its Properties window and set:
- Build Action: Embedded Resource
- Access Modifier:
public(orinternalif yourFigureclass lives in the same project as your form)
Step 2: Access Resources Directly (Simplest Approach)
If your Figure class is in the same project as your main form, you can directly reference your project's auto-generated Resources class. Here's the updated code:
First, add a using directive for your project's Properties namespace (replace YourProjectName with your actual project name):
using namespace YourProjectName::Properties;
Then update your Figure class methods to use embedded resources instead of FromFile:
ref class Figure { public: // Fixed the duplicate color assignment in the constructor! Figure() { color = EMPTY; active = 0; king = 0; } Figure(int x, int y, TYPE color) { this->color = color; active = 0; king = 0; } void MoveTo(pair<int,int> newcord, System::Windows::Forms::PictureBox^ current, TYPE turn) { current->Location = System::Drawing::Point(37 + newcord.second * 90, 37 + newcord.first * 90); if (color == EMPTY) color = turn; else color = EMPTY; // Uncomment if you need to set the empty image here: // current->Image = Resources::EMPTY; } void make_king(System::Windows::Forms::PictureBox^% current, int i) { if (i == 0 && color == WHITE) { current->Image = Resources::WhiteKing; king = true; } else if (i == 7 && color == BLACK) { current->Image = Resources::BlackKing; king = true; } } // ... rest of your existing methods ... private: Graphics^ picture; TYPE color; bool active; bool king; };
And wherever you were using Image::FromFile("EMPTY.png"), replace it with:
mass[oldcord.first - 1, oldcord.second + 1]->Image = Resources::EMPTY;
Step 3: Alternative: Pass ResourceManager via Constructor
If your Figure class is in a separate project, or you prefer not to hardcode the project's Resources class, you can pass a ResourceManager to the Figure constructor (here's the correct implementation):
- Update the
Figureclass to hold a reference to theResourceManager:
ref class Figure { public: Figure(System::Resources::ResourceManager^ resManager) : resources(resManager), color(EMPTY), active(false), king(false) {} Figure(int x, int y, TYPE color, System::Resources::ResourceManager^ resManager) : color(color), resources(resManager), active(false), king(false) {} void make_king(System::Windows::Forms::PictureBox^% current, int i) { if (i == 0 && color == WHITE) { current->Image = cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"WhiteKing")); king = true; } else if (i == 7 && color == BLACK) { current->Image = cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"BlackKing")); king = true; } } // ... rest of your existing methods ... private: System::Resources::ResourceManager^ resources; Graphics^ picture; TYPE color; bool active; bool king; };
- When creating
Figureinstances in your main form, pass the project's ResourceManager:
// In your MainForm code Figure^ newFigure = gcnew Figure(Properties::Resources::ResourceManager); // Or for the parameterized constructor: Figure^ newFigure = gcnew Figure(x, y, BLACK, Properties::Resources::ResourceManager);
Key Fixes & Notes
- I fixed a redundant duplicate
this->color = colorline in your parameterized constructor – that was a tiny bug that could cause unexpected behavior. - Embedded resources are part of your executable now, so you don't need to distribute the image files separately.
- Make sure the resource names you use (like
"WhiteKing") match exactly what's listed in your Resources designer (case-sensitive in some contexts).
内容的提问来源于stack exchange,提问作者NoActualName

