Icon corruption when loading from a resource

  • Thread starter Thread starter Carl Scarlett
  • Start date Start date
C

Carl Scarlett

When I display icons in controls from an imagelist, the icons are
"corrupted" if they are added from a resource (or a file) but are fine if I
add them using the designer at design time. I need to add the icons
dynamically at runtime.

How can you add the icons at runtime without corrupting the image?
 
Here's some code from my test project. I have an icon "help2.ico" that
contains all possible image sizes and formats. At design time, I add it to
ImageList1. I also create ListView1 with 3 items in it, and add the icon to
the project as an embedded resource.

When you run the project, the last two items have corrupted icons. It looks
like the icons are being resized from a larger icon size. How do I tell it
to use the correct icon size from the icon?

Code follows:
--------------

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Set image list
ListView1.SmallImageList = ImageList1

'Set item 0 image
ListView1.Items(0).ImageIndex = 0

'Load from resource
ImageList1.Images.Add(New
System.Drawing.Icon(System.Reflection.Assembly.GetExecutingAssembly.GetManif
estResourceStream(Me.GetType, "help2.ico")))

'Set item 1 image
ListView1.Items(1).ImageIndex = ImageList1.Images.Count - 1

'Load from file
Dim s As String = Application.StartupPath
Dim FileName As String = s.Substring(0, s.LastIndexOf("\"c) + 1) &
"help2.ico"
ImageList1.Images.Add(New System.Drawing.Icon(FileName))

'Set item 2 image
ListView1.Items(2).ImageIndex = ImageList1.Images.Count - 1

End Sub

"Mick Doherty"
 
Excellent. I've tested and it does resolve the problem.

Thanks Mick (and Matthew Hazlett).

"Mick Doherty"
 
Back
Top