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

发布于 2024-07-21 18:16:40 字数 128 浏览 10 评论 0原文

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

How would I display an Icon at 48x48 resolution on a form in vb.net?
I looked at using imagelist , but I don't know how to display the image that I add to the list using code and how to specify it's coordinates on the form.
I did some google searching but none of the examples really show what I need to know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

躲猫猫 2024-07-28 18:16:40

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

The ImageList is not ideal when you have image formats supporting alpha transparency (at least it this used to be the case; I didn't use them a lot recently), so you are probably better off loading the icon from a file on disk or from a resource. If you load it from disk you can use this approach:

' 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()

Update: if you instead want to have the icon as an embedded resource in your assembly, you can alter the LoadIconFromFile method so that it looks like this instead:

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
北座城市 2024-07-28 18:16:40

您需要一个图片框控件将图像放置在窗体上。

然后,您可以将 Image 属性设置为您希望显示的图像,可以是来自磁盘上的文件、图像列表或资源文件的图像。

假设你有一个名为 pct:

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

or

pct.Image = My.Resources.Image_Name 'project resources

or 的图片框

pct.Image = imagelist.image(0)  'imagelist

You want a picturebox control to place the image on the form.

You can then set the Image property to the image you wish to display, be that from a file on disk, an image list or a resource file.

Assuming you have a picturebox called pct:

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

or

pct.Image = My.Resources.Image_Name 'project resources

or

pct.Image = imagelist.image(0)  'imagelist
涫野音 2024-07-28 18:16:40
  Me.Icon = Icon.FromHandle(DirectCast(ImgLs_ICONS.Images(0), Bitmap).GetHicon())
  Me.Icon = Icon.FromHandle(DirectCast(ImgLs_ICONS.Images(0), Bitmap).GetHicon())
风流物 2024-07-28 18:16:40

您可以使用标签控件来执行相同的操作。 我用一个在图片框控件中的图像上画了一个点。 它可能比使用 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

You can use a label control to do the same thing. I used one to draw a dot over an image in a picturebox control. It might be less overhead than using a 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)

Using the OnPaint method might be the best way of doing this.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文