Within a Visual Basic Project I have added a resource file (resx) that contains a bunch of images.
在Visual Basic项目中,我添加了一个包含一堆图像的资源文件(resx)。
Now I want to query the names of the images. If I open the resx file in the designer view in the Visual Studio IDE and select an image, the property grid shows me a name property (defaults to "filename without extension but can be changed).
现在我想查询图像的名称。如果我在Visual Studio IDE的设计器视图中打开resx文件并选择一个图像,属性网格会显示一个名称属性(默认为“没有扩展名但可以更改的文件名”)。
The background is that I have a imagelist that is created at runtime and populated with the images from the resource file. To be able to access these images by the key, I have to set it.
背景是我有一个在运行时创建的图像列表,并填充了资源文件中的图像。为了能够通过密钥访问这些图像,我必须设置它。
My code looks like this (everything hard coded):
我的代码看起来像这样(所有硬编码):
Dim imagelist as new Imagelist
imageList.Images.Add("A", My.Resources.MyImages.A)
imageList.Images.Add("B", My.Resources.MyImages.B)
imageList.Images.Add("C", My.Resources.MyImages.C)
imageList.Images.Add("D", My.Resources.MyImages.D)
imageList.Images.Add("E", My.Resources.MyImages.E)
....
imageList.Images.Add("XYZ", My.Resources.MyImages.XYZ)
And I want to achive this:
我希望得到这个:
Dim imagelist as new ImageList
For Each img in GetMeAllImagesWithNameFromMyResourceFile
imageList.Images.Add(img.Name, img.ImageFile)
Next
where Name is a string and ImageFile a System.Drawing.Bitmap
其中Name是一个字符串,ImageFile是一个System.Drawing.Bitmap
4 个解决方案
#1
See if this piece of code helps.
看看这段代码是否有帮助。
Dim runTimeResourceSet As Object
Dim dictEntry As DictionaryEntry
runTimeResourceSet = My.Resources.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, False, True)
For Each dictEntry In runTimeResourceSet
If (dictEntry.Value.GetType() Is GetType(Icon)) Then
Console.WriteLine(dictEntry.Key)
End If
Next
I have used Icon as an example, which you will have to change if you are using Bitmap.
我使用Icon作为示例,如果您使用Bitmap,则必须更改。
EDIT: You will have to use reference of dictEntry.Value & see how it can be used for adding it to imagelist.
编辑:你将不得不使用dictEntry.Value的参考,并看看它如何用于将其添加到imagelist。
#2
The following is written in C#, you should be able to translate this to VB easily.
以下是用C#编写的,您应该可以轻松地将其转换为VB。
Assembly executingAssembly = GetExecutingAssembly();
foreach (string resourceName in executingAssembly.GetManifestResourceNames())
{
Console.WriteLine( resourceName );
}
Now that you have all the resource names, you can iterate over the list and do something like:
现在您拥有了所有资源名称,您可以迭代列表并执行以下操作:
foreach(string s in executingAssembly.GetManifestResourceNames())
{
if (s.EndsWith(".bmp"))
{
imgStream = a.GetManifestResourceStream(s);
if (imgStream != null)
{
bmp = Bitmap.FromStream(imgStream) as Bitmap;
imgStream.Close();
}
}
}
I have not tried this, but it should work.
我没试过这个,但它应该有效。
#3
Try something like this:
尝试这样的事情:
Dim reader As New ResXResourceReader(resxFilePath)
Dim en As IDictionaryEnumerator
en = reader.GetEnumerator()
While en.MoveNext()
Console.WriteLine("Resource Name: [{0}] = {1}", en.Key, en.Value)
End While
reader.Close()
You can find more examples that could help you out at this link. The examples are written in C#, but it's not very hard to modify them for vb.net
您可以在此链接中找到更多可以帮助您的示例。这些示例是用C#编写的,但是为vb.net修改它们并不是很难
#4
While above answers pointed me in right direction, I am adding a separate answer to clarify use of GetResourceSet and subsequent loading of images:
虽然上面的答案指出了我正确的方向,但我正在添加一个单独的答案来澄清GetResourceSet的使用和后续的图像加载:
Dim resSet As Resources.ResourceSet = My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.InvariantCulture, True, False)
For Each de As DictionaryEntry In resSet
If (de.Value.GetType() Is GetType(Bitmap)) Then
m_Icons.Add(de.Key, My.Resources.ResourceManager.GetObject(de.Key))
End If
Next
Note the following for arguments in My.Resources.ResourceManager.GetResourceSet:
请注意以下My.Resources.ResourceManager.GetResourceSet中的参数:
- use of InvariantCulture
- True is required to load the resources, as at this point in my class library I have not yet accessed the resource set and this forces it to be loaded. This seems to be what @bcrgen-steinblock meant in his comment, but it was misunderstood in the ensuing edit
- false is ok for me because I don't have a fallback / default resource set
使用InvariantCulture
加载资源需要True,因为在我的类库中此时我还没有访问资源集,这迫使它被加载。这似乎是@ bcrgen-steinblock在评论中的意思,但在随后的编辑中却被误解了
false对我来说没问题,因为我没有回退/默认资源集
#1
See if this piece of code helps.
看看这段代码是否有帮助。
Dim runTimeResourceSet As Object
Dim dictEntry As DictionaryEntry
runTimeResourceSet = My.Resources.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, False, True)
For Each dictEntry In runTimeResourceSet
If (dictEntry.Value.GetType() Is GetType(Icon)) Then
Console.WriteLine(dictEntry.Key)
End If
Next
I have used Icon as an example, which you will have to change if you are using Bitmap.
我使用Icon作为示例,如果您使用Bitmap,则必须更改。
EDIT: You will have to use reference of dictEntry.Value & see how it can be used for adding it to imagelist.
编辑:你将不得不使用dictEntry.Value的参考,并看看它如何用于将其添加到imagelist。
#2
The following is written in C#, you should be able to translate this to VB easily.
以下是用C#编写的,您应该可以轻松地将其转换为VB。
Assembly executingAssembly = GetExecutingAssembly();
foreach (string resourceName in executingAssembly.GetManifestResourceNames())
{
Console.WriteLine( resourceName );
}
Now that you have all the resource names, you can iterate over the list and do something like:
现在您拥有了所有资源名称,您可以迭代列表并执行以下操作:
foreach(string s in executingAssembly.GetManifestResourceNames())
{
if (s.EndsWith(".bmp"))
{
imgStream = a.GetManifestResourceStream(s);
if (imgStream != null)
{
bmp = Bitmap.FromStream(imgStream) as Bitmap;
imgStream.Close();
}
}
}
I have not tried this, but it should work.
我没试过这个,但它应该有效。
#3
Try something like this:
尝试这样的事情:
Dim reader As New ResXResourceReader(resxFilePath)
Dim en As IDictionaryEnumerator
en = reader.GetEnumerator()
While en.MoveNext()
Console.WriteLine("Resource Name: [{0}] = {1}", en.Key, en.Value)
End While
reader.Close()
You can find more examples that could help you out at this link. The examples are written in C#, but it's not very hard to modify them for vb.net
您可以在此链接中找到更多可以帮助您的示例。这些示例是用C#编写的,但是为vb.net修改它们并不是很难
#4
While above answers pointed me in right direction, I am adding a separate answer to clarify use of GetResourceSet and subsequent loading of images:
虽然上面的答案指出了我正确的方向,但我正在添加一个单独的答案来澄清GetResourceSet的使用和后续的图像加载:
Dim resSet As Resources.ResourceSet = My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.InvariantCulture, True, False)
For Each de As DictionaryEntry In resSet
If (de.Value.GetType() Is GetType(Bitmap)) Then
m_Icons.Add(de.Key, My.Resources.ResourceManager.GetObject(de.Key))
End If
Next
Note the following for arguments in My.Resources.ResourceManager.GetResourceSet:
请注意以下My.Resources.ResourceManager.GetResourceSet中的参数:
- use of InvariantCulture
- True is required to load the resources, as at this point in my class library I have not yet accessed the resource set and this forces it to be loaded. This seems to be what @bcrgen-steinblock meant in his comment, but it was misunderstood in the ensuing edit
- false is ok for me because I don't have a fallback / default resource set
使用InvariantCulture
加载资源需要True,因为在我的类库中此时我还没有访问资源集,这迫使它被加载。这似乎是@ bcrgen-steinblock在评论中的意思,但在随后的编辑中却被误解了
false对我来说没问题,因为我没有回退/默认资源集