在 Visual Basic 项目中,我添加了一个包含一堆图像的资源文件 (resx)。
现在我想查询图像的名称。
如果我在 Visual Studio IDE 的设计器视图中打开 resx 文件并选择一个图像,属性网格会显示一个名称属性(默认为“没有扩展名的文件名,但可以更改)。
背景是我有一个图像列表,是在运行时创建的,并填充了资源文件中的图像,为了能够通过密钥访问这些图像,我必须将其设置
为如下(所有内容都是硬编码的):
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)
我想实现这一点
Dim imagelist as new ImageList
For Each img in GetMeAllImagesWithNameFromMyResourceFile
imageList.Images.Add(img.Name, img.ImageFile)
Next
: Name 是一个字符串,ImageFile 是一个 System.Drawing.Bitmap
Within a Visual Basic Project I have added a resource file (resx) that contains a bunch of images.
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).
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
发布评论
评论(4)
看看这段代码是否有帮助。
我以 Icon 为例,如果您使用 Bitmap,则必须更改它。
编辑:您必须使用 dictEntry.Value & 的引用 看看如何使用它来将其添加到图像列表中。
See if this piece of code helps.
I have used Icon as an example, which you will have to change if you are using Bitmap.
EDIT: You will have to use reference of dictEntry.Value & see how it can be used for adding it to imagelist.
以下是用 C# 编写的,您应该能够轻松地将其转换为 VB。
现在您已经拥有了所有资源名称,您可以迭代列表并执行以下操作:
我还没有尝试过此操作,但它应该可以工作。
The following is written in C#, you should be able to translate this to VB easily.
Now that you have all the resource names, you can iterate over the list and do something like:
I have not tried this, but it should work.
尝试这样的操作:
您可以在 此链接。 这些示例是用 C# 编写的,但是针对 vb.net 修改它们并不难
Try something like this:
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
虽然上面的答案为我指明了正确的方向,但我添加了一个单独的答案来阐明 GetResourceSet 的使用以及随后的图像加载:
请注意 My.Resources.ResourceManager.GetResourceSet 中的参数的以下内容:
加载资源需要InvariantCulture
While above answers pointed me in right direction, I am adding a separate answer to clarify use of GetResourceSet and subsequent loading of images:
Note the following for arguments in My.Resources.ResourceManager.GetResourceSet:
InvariantCulture