Replacing Absolute file path by Resource file path

此生再无相见时 提交于 2021-01-28 13:48:44

问题


I created an add-in to an existing software (Revit) and I want to embed an image (my company's logo) in the add-in's button.

Here is my current code for it:

'Set the large image shown on button
        Dim uriImage As New Uri("\\10.8.60.3\Shared\REVIT\FSElogo.png")
        Dim largeImage As New BitmapImage(uriImage)
        MainButton.LargeImage = largeImage

It works pretty well and the logo is correctly displayed, however it requires the computer to have access to the server located at \\10.8.60.3. When working from home, we do not have access to this server and Revit throws an error when starting because of it.

So I tried adding the FSElogo.png file to my VB.Net project as a Resource and then tried to use My.Resources to access the image, effectively removing the need for an external image to be used.

Well, I can't get it to work. I tried replacing the code above by

MainButton.LargeImage = CType(My.Resources.ResourceManager.GetObject("FSElogo.png"), Windows.Media.ImageSource)

But it doesn't work. It doesn't throw an error, but no image is displayed on the button.

If I don't cast my Object to an ImageSource I get an implicit conversion from Object to Image error, and I'm not even sure my ResourceManager is even really returning the object FSElogo.png.

What am I doing wrong here?


回答1:


I am using the VS provided button with the .BackgroundImage property. Notice that the extension of the file is not included in the resource identifier. If this doesn't work, you will have to explain exactly how you added the resource to your project.

    MainButton.BackgroundImage = My.Resources.FSElogo


来源:https://stackoverflow.com/questions/62397657/replacing-absolute-file-path-by-resource-file-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!