Introduction
Learn how to retrieve a project resource by name for either an icon or a bitmap, size a PictureBox to properly display the image, and change a form's icon at runtime.
Project names reflect the original Microsoft Technet article written for NET7 and have been updated to NET10. The Microsoft Technet site has been retired.
By using the presented code, a developer
- Has common code so no need to copy code from project to project
- A good deal of code will be a learning opportunity.
Class project
ImageHelper class provides code to retrieve Icon and BitMap resources by passing a Windows Form ResourceManager.
- ResourceItemList extension method is responsible for remembering Icon and BitMap resources.
- ResourceImageNames extension method returns names of all resources
public static class ImageHelper
{
public static List<string> ResourceImageNames(this ResourceManager manager)
{
var names = new List<string>();
var resourceSet = manager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
names.AddRange(resourceSet!.Cast<DictionaryEntry>()
.Where(dictionaryEntry => dictionaryEntry.Value is Image or Icon)
.Select(dictionaryEntry => dictionaryEntry.Key.ToString()));
return names;
}
public static List<ResourceItem> ResourceItemList(this ResourceManager manager)
{
var items = new List<ResourceItem>();
foreach (var name in manager.ResourceImageNames())
{
var item = new ResourceItem()
{
Name = name,
IsIcon = false
};
if (manager!.GetObject(name) is Icon)
{
item.Image = ((Icon)manager.GetObject(name)!)?.ToBitmap();
item.IsIcon = true;
}
else
{
item.Image = (Bitmap)manager.GetObject(name)!;
}
items.Add(item);
}
return items;
}
}
// model for above
public class ResourceItem
{
/// <summary>
/// Resource name
/// </summary>
public string Name { get; set; }
/// <summary>
/// Image which is either an <see cref="Icon"/> or <see cref="Bitmap"/>
/// </summary>
public Bitmap Image { get; set; }
/// <summary>
/// Indicates if dealing with an icon so when displaying the
/// control used to display can adjust it's size or Size mode
/// </summary>
public bool IsIcon { get; set; }
public override string ToString() => Name;
}
Usage
Assign all resource images first to a BindingSource, which becomes the DataSource for a ListBox
_allBindingSource.DataSource = ResourceImages.Instance
.Images()
.OrderBy(x => x.Name)
.ToList();
AllImagesListBox.DataSource = _allBindingSource;
On position change
private void ChangeFromAllImage()
{
if (AllImagesListBox.SelectedIndex <= -1) return;
var item = (ResourceItem)_allBindingSource.Current;
AllImagesPictureBox.SizeMode = item!.IsIcon ? PictureBoxSizeMode.Normal : PictureBoxSizeMode.Zoom;
AllImagesPictureBox.Image = item.Image;
}
To get Icon and BitMap image to ListBoxes.
_iconBindingSource.DataSource = ResourceImages.Instance
.Images()
.Icons()
.OrderBy(x => x.Name)
.ToList();
IconListBox.DataSource = _iconBindingSource;
_iconBindingSource.PositionChanged += IconBindingSource_PositionChanged;
ChangeFromIconImage();
_bindingList = new BindingList<ResourceItem>(ResourceImages.Instance
.Images()
.BitMaps()
.OrderBy(x => x.Name)
.ToList());
_bitmapBindingSource.DataSource = _bindingList;
BitmapImagesListBox.DataSource = _bitmapBindingSource;
_bitmapBindingSource.PositionChanged += BitmapBindingSource_PositionChanged;
Get a single Icon
This code retrieves an icon by name (case-insensitive) and sets the form's icon.
Icon = ResourceImages.Instance.Images().GetIconByName("csharp");
Use GetBitmapByName, which returns a single BitMap.


Top comments (0)