gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:11:08 24 4
gpt4 key购买 nike

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

最佳答案

当您拥有支持 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()

更新:如果您希望将图标作为程序集中的嵌入资源,您可以更改 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

关于vb.net - 在 vb.net 中的表单上显示图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/914323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com