在vb.net中的表单上显示图标

MaQ*_*eod 3 vb.net imagelist

如何在vb.net中的表单上以48x48分辨率显示图标?我查看了使用imagelist,但我不知道如何使用代码显示我添加到列表中的图像以及如何在表单上指定它的坐标.我做了一些谷歌搜索,但没有一个例子真的显示我需要知道的.

Fre*_*örk 7

当你有支持alpha透明度的图像格式时,ImageList并不理想(至少它曾经是这种情况;我最近没有使用它们),所以你可能最好从磁盘上的文件中加载图标或者来自资源.如果从磁盘加载它,您可以使用以下方法:

' Function for loading the icon from disk in 48x48 size '
Private Function LoadIconFromFile(ByVal fileName As String) As Icon
    Return New Icon(fileName, New Size(48, 48))
End Function

' code for loading the icon into a PictureBox '
Dim theIcon As Icon = LoadIconFromFile("C:\path\file.ico")
pbIcon.Image = theIcon.ToBitmap()
theIcon.Dispose()

' code for drawing the icon on the form, at x=20, y=20 '
Dim g As Graphics = Me.CreateGraphics()
Dim theIcon As Icon = LoadIconFromFile("C:\path\file.ico")
g.DrawIcon(theIcon, 20, 20)
g.Dispose()
theIcon.Dispose()
Run Code Online (Sandbox Code Playgroud)

更新:如果您希望将图标作为程序集中的嵌入资源,则可以更改LoadIconFromFile方法,使其看起来像这样:

Private Function LoadIconFromFile(ByVal fileName As String) As Icon
    Dim result As Icon
    Dim assembly As System.Reflection.Assembly = Me.GetType().Assembly
    Dim stream As System.IO.Stream = assembly.GetManifestResourceStream((assembly.GetName().Name & ".file.ico"))
    result = New Icon(stream, New Size(48, 48))
    stream.Dispose()
    Return result
End Function
Run Code Online (Sandbox Code Playgroud)