Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.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中在窗体上显示图标_Vb.net_Imagelist - Fatal编程技术网

在vb.net中在窗体上显示图标

在vb.net中在窗体上显示图标,vb.net,imagelist,Vb.net,Imagelist,如何在vb.net中的窗体上以48x48分辨率显示图标? 我研究了使用imagelist,但我不知道如何使用代码显示添加到列表中的图像,以及如何在表单上指定其坐标。 我做了一些谷歌搜索,但没有一个例子真正显示了我需要知道的内容。您需要一个picturebox控件来将图像放置在表单上 然后,可以将图像属性设置为要显示的图像,可以是磁盘上的文件、图像列表或资源文件中的图像 假设您有一个名为pct的picturebox: pct.Image = Image.FromFile("c:\Image_Nam

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

您需要一个picturebox控件来将图像放置在表单上

然后,可以将图像属性设置为要显示的图像,可以是磁盘上的文件、图像列表或资源文件中的图像

假设您有一个名为pct的picturebox:

pct.Image = Image.FromFile("c:\Image_Name.jpg")  'file on disk


当图像格式支持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

可以使用label控件执行相同的操作。我用它在picturebox控件的图像上画了一个点。它的开销可能比使用PictureBox要小

        Dim label As Label = New Label()
        label.Size = My.Resources.DefectDot.Size
        label.Image = My.Resources.DefectDot ' Already an image so don't need ToBitmap 
        label.Location = New Point(40, 40)
        DefectPictureBox.Controls.Add(label)
使用OnPaint方法可能是最好的方法

Private Sub DefectPictureBox_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DefectPictureBox.Paint
    e.Graphics.DrawIcon(My.Resources.MyDot, 20, 20)
End Sub

欢迎来到堆栈溢出。请详细说明你的答案这真的没有用。一行没有解释,代码在VisualBasic中出现语法错误。我们应该读懂你的心思来利用这个答案吗?(-1)这工作非常好。我刚刚将…图像(0)更改为My.Resources.mypngfile,效果非常好。
  Me.Icon = Icon.FromHandle(DirectCast(ImgLs_ICONS.Images(0), Bitmap).GetHicon())
        Dim label As Label = New Label()
        label.Size = My.Resources.DefectDot.Size
        label.Image = My.Resources.DefectDot ' Already an image so don't need ToBitmap 
        label.Location = New Point(40, 40)
        DefectPictureBox.Controls.Add(label)
Private Sub DefectPictureBox_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DefectPictureBox.Paint
    e.Graphics.DrawIcon(My.Resources.MyDot, 20, 20)
End Sub