Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net Visual Basic:从路径创建位图?_Vb.net_Path - Fatal编程技术网

Vb.net Visual Basic:从路径创建位图?

Vb.net Visual Basic:从路径创建位图?,vb.net,path,Vb.net,Path,但我有一个错误。。。。它说的是无效的论点。 我想从我的应用程序的Resources文件夹中找到的名为Pic1.png的图像创建位图。如果您的Resources文件夹中有该文件,您可以像my.Resources.FileName Dim Image1 As New Bitmap(Application.StartupPath + "\Resources\Pic1.png") 在这种情况下,无效参数通常意味着找不到文件。我认为如果在它前面添加一行msgbox(filename&“=”&io.fil

但我有一个错误。。。。它说的是无效的论点。
我想从我的应用程序的Resources文件夹中找到的名为Pic1.png的图像创建位图。

如果您的
Resources
文件夹中有该文件,您可以像
my.Resources.FileName

Dim Image1 As New Bitmap(Application.StartupPath + "\Resources\Pic1.png")

在这种情况下,无效参数通常意味着找不到文件。我认为如果在它前面添加一行msgbox(filename&“=”&io.file.exists(filename)),您将看到这一点

如果确实希望通过文件系统访问它,则需要将文件的属性设置为“复制本地”


如果要将其作为资源访问,则需要通过“项目属性”中的“资源”屏幕将其添加到项目中。然后,您可以使用My.Resources.filename访问它,您必须确保运行exe的调试或发布文件夹包含名为“Resources”的文件夹,并且包含“pic1.png”。您可能希望验证application.startuppath指向的路径,如下所示:

Dim Image1 As New Bitmap(My.Resources.Pic1)
现在,当您的程序启动时,您将能够手动验证它正在执行您认为应该执行的操作,并且文件位于正确的位置。您还可以使用以下命令首先检查文件是否存在:

MessageBox.show(application.startuppath)

如果您遇到任何问题,请告诉我。

在编译时,资源通常内置在EXE中,而不是实际存储在文件夹中,因此请遵循binil的建议查看MessageBox向我抛出了一条路径,我尝试遵循它,但是我在c:\Users\Administrator\AppData\Local\Temporary Projects\Test\bin\Debug…中找不到AppData文件夹……在我的解决方案资源管理器窗口中,我有一个Resources文件夹,其中有Pic1.pngOk。MessageBox为我提供了项目调试文件夹的路径。但这里不是资源文件夹所在的位置!这就是我出错的原因。。。。但是,如何访问resources文件夹?Application.StartupPath指向.exe文件的起始文件夹,在本例中是调试文件夹。如果要使用Application.StartupPath,您必须给出文件的完整路径并将文件放在那里,或者如果要将文件用作内置资源,则必须将该文件拖到项目->属性->资源窗口中。将其拖动到该窗口后,将该资源的属性更改为“embedded in.resx”。现在,要访问资源,您必须使用My.Resources.Pic1,这应该可以为您完成。
Dim Image1 as Bitmap
If (System.IO.File.Exists(Application.StartupPath + "\Resources\Pic1.png")) Then
    Image1 = New Bitmap(Application.StartupPath + "\Resources\Pic1.png")
Else
    MessageBox.show("File: " & Application.StartupPath + "\Resources\Pic1.png" & " doesn't exist. Check file and try again.")
    End
End If