What I Am Working On
My.Resources: I have a lot of icons in My.Resources that I want to display on a "preview button" (i.e. Button10 in the code snippet). So, I added the resource strings (See screenshot) as text to my ListView (i.e. Column1 = Program Name; Column2 = Path; Column3 = My.Resources) alongside their respective programs which is necessary to synch the images. The reason I'm attempting to achieve this is for programs which don't have file paths to extract the program icons from, i.e. WIN10 MS Apps and MS Store Apps which use Application User Model ID (AUMID). Unless anyone knows how to access/extract the WIN10 MS Apps and MS Store Apps icons from the system?
Problem: I'm trying to find a way to "cast" the resource from the resource string in the ListView column to a bitmap to use as the image on the preview button (i.e. SelectedIndexChanged). I'm not sure if casting is the right way to go?
What I have tried: The closest I have managed to get is the following code which throws an exception (i.e. System.ArgumentException: 'Parameter is not valid.')? The Text being passed appears to be correct.
Dim resourceName = ListView4.SelectedItems(0).SubItems(2).Text.ToString
Dim icon = DirectCast(My.Resources.ResourceManager.GetObject(resourceName), Icon)
Dim bmp = icon.ToBitmap()
Button10.BackgroundImage = bmp
Button10.BackgroundImageLayout = ImageLayout.Stretch
Related Questions
how-do-i-programmatically-get-the-icons-for-win10-ms-store-apps
Resources (URLs)
launching-windows-10-store-apps
find-the-application-user-model-id-of-an-installed-app
CodePudding user response:
Your ListView
appears to contain code snippets that you expect to execute at run time. That's not how it works. You can't just execute a string
as though it were compiled code. If you want to get a resource from My.Resources
based on its name as a string
then you can do this:
Dim resourceObject = DirectCast(My.Resources.ResourceManager.GetObject(resourceName), ResourceType)
In your case, you would need to store the actual names, e.g. "Bridge_0" and "Photoshop_1000", and use those values. You'll need to cast as type Icon
and then call ToBitmap
yourself, e.g.
Dim resourceName = ListView4.SelectedItems(0).SubItems(2)
Dim icon = DirectCast(My.Resources.ResourceManager.GetObject(resourceName), Icon)
Dim bmp = icon.ToBitmap()