Form display icon in vb.net

How do I display an icon with a resolution of 48x48 on a form in vb.net? I looked at using imagelist, but I don’t know how to display the image that I am adding to the list using code, and how to specify its coordinates in the form. I did some searches, but none of the examples showed what I need to know.

+3
source share
4 answers

ImageList is not ideal if you have image formats that support alpha transparency (at least that was the case, I have not used them recently), so you probably better not load the icon from a file on disk or from a resource. If you boot 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()

: , , 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
+7

, .

Image , , , .

, pct:

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
+2
  Me.Icon = Icon.FromHandle(DirectCast(ImgLs_ICONS.Images(0), Bitmap).GetHicon())
0

, . , . , 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
0

Source: https://habr.com/ru/post/1709163/


All Articles