Resources.ResourceManager.GetObject wouldn't p

2019-08-22 22:04发布

问题:

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: