Image resources?

2019-08-23 01:07发布

问题:

I've been struggling for hours trying to figure out how to use resources in C++/CLI. I've messed around with the resource.h/app.rc files along with the managed resx files to no avail.

I just have a couple PNG images that I would like to use with a PictureBox, but I can't seem to figure out how to setup the resources...

Thanks for your help,

Alex

回答1:

  • create a windows forms project
  • add new resource file (resx) to the project
  • open that file, the resource editor appears
  • in the top left corner switch to image mode
  • at the top click to "Add resources", and add your images
  • in your code use it this way:

    using namespace System::Resources;
    
    ResourceManager^ rm = gcnew ResourceManager("ImageResources.MyResources", GetType()->Assembly);
    
    pictureBox1->Image = safe_cast<Image^>(rm->GetObject(L"MyImage");
    

Where "ImageResources" is the name of the namespace, the "MyResources" is the name of the resx file and the "MyImage" is the name of the image inside the resource file.