开发者

Visual Basic: Creating a bitmap from path?

Dim Image1 As New Bitmap(Application.StartupPath + "\Resource开发者_JS百科s\Pic1.png")

But I get an error.... It says invalid argument. I want to create a bitmap from an image called Pic1.png found in the Resources folder of my application.


if you have the file in your Resources folder you can call it like My.Resources.FileName

Dim Image1 As New Bitmap(My.Resources.Pic1)


Invalid argument in this situation usually means file not found. I think if you add a line before it with a msgbox( filename & " = " & io.file.exists(filename)) you will see that.

If you really want to access it through the file system you need to set the properties of the file to copy local.

If you want to access it as a resource you need to add it to your project through the resource screen in the project properties. Then you can access it using My.Resources.filename


You have to make sure your debug or release folder that the exe is running from contains a folder named "resources" and contains "pic1.png". You may want to verify the path that application.startuppath points to like so:

MessageBox.show(application.startuppath)

Now when your program starts, you will be able to manually verify that it's doing what you think it should and the file is in the right location. You can also use the following to check if the file exists first:

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

Let me know if you run into any problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜