代码之家  ›  专栏  ›  技术社区  ›  MaQleod

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

  •  3
  • MaQleod  · 技术社区  · 17 年前

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

    4 回复  |  直到 17 年前
        1
  •  7
  •   Fredrik Mörk    17 年前

    当你有支持阿尔法透明度的图像格式时,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()
    

    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
    
        2
  •  1
  •   Pondidum    17 年前

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

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

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

    pct.Image = imagelist.image(0)  'imagelist
    
        3
  •  0
  •   user707407    15 年前
      Me.Icon = Icon.FromHandle(DirectCast(ImgLs_ICONS.Images(0), Bitmap).GetHicon())
    
        4
  •  0
  •   Ron Carter    13 年前

    您可以使用标签控件来执行相同的操作。我用一个在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)
    

    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
    
    推荐文章