使用项目资源的 VB.NET 图像数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/762954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:10:57  来源:igfitidea点击:

VB.NET Image array using project resources

vb.netarraysimage

提问by burntsugar

I need to make an array of images using references to the resources folder.

我需要使用对资源文件夹的引用来制作一组图像。

The following is my code which does not work:

以下是我不起作用的代码:

Dim imgPictures(8) As Image
imgPictures(0) = Image.FromFile(My.Resources.cat_1.ToString)

How do I reference the images sitting in the resouces folder for this task?

我如何引用位于资源文件夹中的图像来完成此任务?

Cheers

干杯

采纳答案by JaredPar

Dim imgPictures(8) As Image
imgPictures(0) = My.Resources.ResourceManager.GetString("myResourceName")

...

...

' For assigning a resource to a image control based on your array 
' of strings use something  like this:

Me.picture1.Image = My.Resources.ResourceManager.GetObject(arr(i))

回答by Kirtan

You can use the My.Resource.GetString("ResourceKey{0}")method for it.

您可以使用该My.Resource.GetString("ResourceKey{0}")方法。

回答by JaredPar

The easiest way to reference a Resource in code is to add it to the project resources.

在代码中引用资源的最简单方法是将其添加到项目资源中。

  • Right click on the project and choose Properties
  • Select the Resources Tab
  • Change the Combo Box to Images
  • Select the "Add Existing" Image from the Add Resource Combo Button
  • Choose your Image
  • 右键单击项目并选择属性
  • 选择资源选项卡
  • 将组合框更改为图像
  • 从添加资源组合按钮中选择“添加现有”图像
  • 选择您的图片

You can then reference the image directly in code using the following

然后,您可以使用以下代码直接在代码中引用图像

Dim img = My.Resources.NameOfTheImage

回答by JaredPar

I think JaredPar was on the right track, but a little more info is needed.

我认为 JaredPar 走在正确的轨道上,但需要更多信息。

In essence, it kind of depends on how the resources are stored. (If you are looking to get the path of a resource, you will most likely have to look into reflection.)

从本质上讲,这取决于资源的存储方式。(如果您想获得资源的路径,您很可能需要考虑反射。)



If your images are embedded or content you can reference them directly:

如果您的图像是嵌入的或内容,您可以直接引用它们:

-(Right Click Image in Visual Studio > Properties > Build Action = "Embedded Resource"

-(在 Visual Studio 中右键单击图像 > 属性 > 构建操作 =“嵌入式资源”

-(Right Click Image in Visual Studio > Properties > Build Action = "Content"; also ensure Copy To Output Directory = "Copy Always")

-(在 Visual Studio 中右键单击图像 > 属性 > 构建操作 =“内容”;还要确保复制到输出目录 =“始终复制”)

Dim imgPictures(8) As Image
imgPictures(0) = My.Resources.NameOfImage1
imgPictures(1) = My.Resources.NameOfImage2
...

If your images are just in a folder:

如果您的图像只是在一个文件夹中:

Dim imgPictures(8) As Image
imgPictures(0) = Bitmap.FromFile( <filename1> )
imgPictures(1) = Bitmap.FromFile( <filename2> )
...

Scott

斯科特