Vb.net 无法将关联菜单中的图像加载为图标

Vb.net 无法将关联菜单中的图像加载为图标,vb.net,contextmenu,toolstripmenu,toolstripdropdown,Vb.net,Contextmenu,Toolstripmenu,Toolstripdropdown,我正在尝试使用以下链接中的代码: 唯一的区别是我想要一个本地图像,而不是我的.resources 我所拥有的是: ''Tool 2 displays a string and image. Dim tool2 As New ToolStripMenuItem("Tool 2", (Image.FromFile("C:\test\icon.jpg"))) tool2.Name = "mnuToolsTool2" tool2.ShortcutKeys = (Keys.

我正在尝试使用以下链接中的代码:

唯一的区别是我想要一个本地图像,而不是我的.resources

我所拥有的是:

    ''Tool 2 displays a string and image.
    Dim tool2 As New ToolStripMenuItem("Tool 2", (Image.FromFile("C:\test\icon.jpg")))
    tool2.Name = "mnuToolsTool2"
    tool2.ShortcutKeys = (Keys.D2 Or Keys.Control) ' Ctrl+2
    AddHandler tool2.Click, AddressOf mnuTool2_Click
    ToolStripMenuItem1.DropDownItems.Add(tool2)

我无法重现这个错误。然而,从给定的文本、代码和链接来看,我的最佳猜测如下:

您正在使用64位计算机。 在Form.Load事件中运行代码。 此方法中的某个地方出错。 您可能不知道,在64位计算机上的Form.Load中抛出的错误会被系统吞噬

有关更多信息,请阅读本SO帖子:

您应该在构造函数中移动代码:

Public Sub New()
    Me.InitializeComponent()
    'Code goes here...
End Sub
或更改表单。显示的事件:


您使用的是ContextMenuStrip控件吗?它工作正常。您遇到了什么样的错误?它工作正常,可能需要检查您的文件位置?
Public Sub New()
    Me.InitializeComponent()
    'Code goes here...
End Sub
Private Sub _Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
    Try
        'Code goes here...
    Catch ex As Exception
        MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub