public IntPtr GetIconHandle(int iconIndex)
{
// Verify that the object is in a consistent state.
if (null == imageList)
{
throw new InvalidOperationException();
}
// Make sure that the list of handles is initialized.
if (null == iconHandles)
{
InitHandlesList();
}
// Verify that the index is inside the expected range.
if ((iconIndex < 0) || (iconIndex >= iconHandles.Count))
{
throw new ArgumentOutOfRangeException("iconIndex");
}
// Check if the icon is in the cache.
if (IntPtr.Zero == iconHandles[iconIndex])
{
var bitmap = imageList.Images[iconIndex] as Bitmap;
// If the image is not a bitmap, then we can not build the icon,
// so we have to return a null handle.
if (null == bitmap)
{
return IntPtr.Zero;
}
iconHandles[iconIndex] = bitmap.GetHicon();
}
return iconHandles[iconIndex];
}