Resources.ResourceManager.GetObject不提供对象 [英] Resources.ResourceManager.GetObject wouldn't provide object

查看:451
本文介绍了Resources.ResourceManager.GetObject不提供对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚,或谷歌回答,为什么从这样的方式从项目资源中提取图像不起作用:

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

obj什么都不是。它不适用于包含的文件扩展名。虽然这个(直接路径)有效(但当部署时却不行):

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

我错过了什么吗?我过去一定做过几次。
此测试代码在Form1_Load()事件中运行。显然,我可以在VS的资源中看到Item22.png。

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.

编辑

我使用Visual Studio添加了示例图像:项目资源上下文菜单 - >添加... - >现有项目

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

推荐答案

通过 My.Resources 访问图像的唯一方法是将其添加到项目的 .resx 文件中。仅存在资源文件夹,以便VS有一种在设计时引用资源的方法。添加文件将将它们包含在实际资源中。

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.

添加新资源:


  1. 解决方案资源管理器中右键单击您的项目,然后按属性 (图1)

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

转到资源标签(图2)

将资源拖放到VS (图3)




图1:

< img src =https://i.stack.imgur.com/Ie3VH.pngalt =项目属性>








图2:

< img src =https://i.stack.imgur.com/DEHFO.pngalt =资源>








图3:

< img src =https://i.stack.imgur.com/5lVtb.pngalt =拖放资源>

这篇关于Resources.ResourceManager.GetObject不提供对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆