我有一个控件库,我已经将.resx文件添加到(ImageResources.resx)。它包含两个.png图像,我随后添加了它。
在同一个库中我有一个控件可以加载几个图像来做一些自定义绘图,但我似乎无法加载资源:
void GTableLayoutPanel::SetBorderImagesFromManifest(String^ topLeftCornerImageName, String^ topImageName)
{
// Grab the assembly this is being called from
Assembly^ assembly = Assembly::GetExecutingAssembly();
// Grab the images from the assembly
Stream^ stream = assembly->GetManifestResourceStream(topLeftCornerImageName);
Image^ topLeftImage = System::Drawing::Image::FromStream(stream);
stream = assembly->GetManifestResourceStream(topImageName);
Image^ topImage = System::Drawing::Image::FromStream(stream);
// Update the internal store from the supplied images
SetBorderImages(topLeftImage, topImage);
}
...给我错误抱怨stream
为空,这表示我对GetManifestResourceStream
的调用失败。
图像被称为group_box_top.png
和group_box_top_left.png
,我按如下方式调用图像加载器:
SetBorderImagesFromManifest("group_box_top_left.png", "group_box_top.png");
我也试过了:
SetBorderImagesFromManifest("group_box_top_left", "group_box_top");
...因为文件出现在没有.png扩展名的.resx文件中,但这会产生同样的错误。
我错过了一步吗?
[编辑] 我在最后一个链接中尝试了这个建议,我得到了:
MyControlsLib.ImageResources.resources
所以现在我尝试引用:
Stream^ strm1 = assembly->GetManifestResourceStream("MyControlsLib.ImageResources.resources.group_box_top_left");
Stream^ strm2 = assembly->GetManifestResourceStream("MyControlsLib.ImageResources.resources.group_box_top_left.png");
Stream^ strm3 = assembly->GetManifestResourceStream("MyControlsLib.ImageResources.group_box_top_left");
Stream^ strm4 = assembly->GetManifestResourceStream("MyControlsLib.ImageResources.group_box_top_left.png");
...所有这些都返回nullptr: - /
答案 0 :(得分:3)
我终于获得了C ++ / CLI解决方案的神奇组合。所以,万一其他人有这个问题:
Resource Files
Add
> New Item..
Visual C++
> Resource
> Assembly Resource File (.resx)
。我将我的名字命名为“ImageResources.resx”Add Resource
按钮,然后选择Add Existing File...
group_box_top.png
和group_box_top_left.png
作为group_box_top
和group_box_top_left
出现在.resx文件中。然后,您可以使用以下方式从清单中抓取图像:
// Grab the assembly this is being called from
Assembly^ assembly = Assembly::GetExecutingAssembly();
AssemblyName^ assemblyName = assembly->GetName();
// Grab the images from the assembly
ResourceManager^ rm = gcnew ResourceManager(assemblyName->Name+".ImageResources", assembly);
Bitmap^ topLeftImage = (Bitmap^)rm->GetObject("group_box_top_left");
Bitmap^ topImage = (Bitmap^)rm->GetObject("group_box_top");
请注意,传递到ImageResources
构造函数的ResourceManager
字符串必须与.resx文件的名称匹配。
Linker
- > Input
Embed Managed Resource File
属性中。我在这里添加了PingSend.wav
。要访问文件,只需执行以下操作:
System::Reflection::Assembly^ assembly = Assembly::GetExecutingAssembly();
System::Media::Sonudplayer^ pingPlayer = gcnew System::Media::SoundPlayer(assembly->GetManifestResourceStream("PingSend.wav"));
..在这种情况下,加载准备播放的音频文件。
答案 1 :(得分:1)
我认为资源项的路径可能需要完全限定:
GetManifestResourceStream("MyNamespace.ImageResources.group_box_top_left")
此链接显示了一个C#示例(抱歉),注意创建流时它在参数中具有命名空间:
http://support.microsoft.com/kb/319292
这也涉及如何找到资源的完全限定路径:
答案 2 :(得分:0)
System::Reflection::Assembly^ assembly = System::Reflection::Assembly::GetExecutingAssembly();
Resources::ResourceManager^ rm = gcnew Resources::ResourceManager("namespaceName" + ".ResourceFileName", assembly);
this->button->Image = cli::safe_cast<Image^>(rm->GetObject("image1"));