Resources.ResourceManager.GetObject wouldn't provide object

蓝咒 提交于 2020-01-06 07:03:32

问题


I cannot figure out, or google an answer, why pulling an image from project resources this way doesn't work:

    Dim MIIcon As Image
    Dim obj As Object = My.Resources.ResourceManager.GetObject("Item22")  'file: "Item22.png"
    MIIcon = DirectCast(obj, Image) 
    Me.PictureBox2.Image = MIIcon

The obj is nothing. Neither it works with file extension included. While this (direct path) works (but it wouldn't when deployed, of course):

    Dim MIIcon2 As Image
    MIIcon2 = Image.FromFile("C:\Users\****\documents\visual studio \Projects\******\Resources\Item22.png")
    Me.PictureBox3.Image = MIIcon2

Did I missed something? I must have done that couple of times in past. This test code is run at Form1_Load() event. Obviously, I can see the Item22.png in my resources in VS.

EDIT

I added the sample image using Visual Studio: Project Resources context menu -> Add... -> Existing item


回答1:


The only way to make an image accessible through My.Resources is to add it to your project's .resx file. The Resources folder exists only so that VS has a way of referencing the resources at design time. Adding files to it will not include them in the actual resources.

To add a new resource:

  1. Right-click your project in the Solution Explorer and press Properties (fig. 1).

  2. Go to the Resources tab (fig. 2).

  3. Drag and drop your resources into VS (fig. 3).

Fig. 1:


Fig. 2:


Fig. 3:



来源:https://stackoverflow.com/questions/47513248/resources-resourcemanager-getobject-wouldnt-provide-object

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