TSVGIconImageCollection
Unit: SVGIconImageCollection
Inherits from: TCustomImageCollection (Delphi 10.3+) or TComponent
Minimum Delphi Version: 10.3 Rio (for full VirtualImageList integration)
Description
TSVGIconImageCollection is a modern VCL component for storing and managing collections of SVG icons. It follows Delphi's ImageCollection/VirtualImageList architecture introduced in Delphi 10.3, allowing multiple TSVGIconVirtualImageList components to share the same icon source at different sizes.
Unlike TSVGIconImageList which contains both storage and rendering, TSVGIconImageCollection only stores SVG icons. Rendering is performed by linked TSVGIconVirtualImageList components, enabling efficient memory usage when the same icons are needed at multiple sizes.
Published Properties
Icon Collection
SVGIconItems
property SVGIconItems: TSVGIconItems;Collection of SVG icon items. Each item contains an SVG image and associated properties.
Usage:
// Access items
var Item := SVGIconImageCollection1.SVGIconItems[0];
// Count
Label1.Caption := Format('%d icons', [SVGIconImageCollection1.SVGIconItems.Count]);Default Rendering Attributes
These properties set default rendering attributes for all icons. Individual icons can override these settings.
FixedColor
property FixedColor: TColor;Default fixed color for all icons. Default: SVG_INHERIT_COLOR
Usage:
SVGIconImageCollection1.FixedColor := clBlue;ApplyFixedColorToRootOnly
property ApplyFixedColorToRootOnly: Boolean;Apply FixedColor only to root SVG elements. Default: False
Usage:
SVGIconImageCollection1.FixedColor := clRed;
SVGIconImageCollection1.ApplyFixedColorToRootOnly := True;GrayScale
property GrayScale: Boolean;Render all icons in grayscale. Default: False
Usage:
SVGIconImageCollection1.GrayScale := True;Opacity
property Opacity: Byte;Default opacity for all icons (0-255). Default: 255
Usage:
SVGIconImageCollection1.Opacity := 200;AntiAliasColor
property AntiAliasColor: TColor;Background color for anti-aliasing. Default: clBtnFace
Usage:
SVGIconImageCollection1.AntiAliasColor := clWhite;Public Methods
Create / Destroy
constructor Create(AOwner: TComponent);
destructor Destroy; override;Icon Management
Add
function Add(const ASVG: ISVG; const AIconName: string;
const AGrayScale: Boolean = False;
const AFixedColor: TColor = SVG_INHERIT_COLOR;
const AApplyToRootOnly: Boolean = False): Integer;Adds a new SVG icon to the collection and returns its index.
Parameters:
ASVG: SVG interface to addAIconName: Name for the iconAGrayScale: Render this icon in grayscaleAFixedColor: Fixed color for this iconAApplyToRootOnly: Apply fixed color only to root element
Returns: Index of the newly added icon
Usage:
var
SVG: ISVG;
Index: Integer;
begin
SVG := GlobalSVGFactory.NewSvg;
SVG.LoadFromFile('home.svg');
Index := SVGIconImageCollection1.Add(SVG, 'home');
end;Delete
procedure Delete(const Index: Integer);Deletes the icon at the specified index.
Usage:
SVGIconImageCollection1.Delete(0);Delete (range with optional category filter)
procedure Delete(const ACategory: String; AStartIndex, AEndIndex: Integer);Deletes a range of icons, optionally filtered by category.
Parameters:
ACategory: Category to filter (empty string = all categories)AStartIndex: Starting index (inclusive)AEndIndex: Ending index (inclusive)
Usage:
// Delete icons 5-10
SVGIconImageCollection1.Delete('', 5, 10);
// Delete icons 0-4 in 'Navigation' category
SVGIconImageCollection1.Delete('Navigation', 0, 4);Remove
procedure Remove(const Name: string);Removes the icon with the specified name.
Usage:
SVGIconImageCollection1.Remove('home');
SVGIconImageCollection1.Remove('Navigation\home');IndexOf
function IndexOf(const Name: string): Integer;Returns the index of the named icon, or -1 if not found.
Usage:
var
Index: Integer;
begin
Index := SVGIconImageCollection1.IndexOf('settings');
if Index >= 0 then
ShowMessage('Found at index: ' + IntToStr(Index));
end;ClearIcons
procedure ClearIcons;Removes all icons from the collection.
Usage:
SVGIconImageCollection1.ClearIcons;File Operations
LoadFromFile
function LoadFromFile(const AFileName: string; out AImageName: string): TSVGIconItem;Loads an SVG from a file and returns the created item.
Parameters:
AFileName: Path to SVG fileAImageName: Receives the name of the loaded icon (filename without extension)
Returns: The newly created TSVGIconItem
Usage:
var
Item: TSVGIconItem;
ImageName: string;
begin
Item := SVGIconImageCollection1.LoadFromFile('C:\Icons\home.svg', ImageName);
ShowMessage('Loaded as: ' + ImageName);
end;LoadFromFiles
function LoadFromFiles(const AFileNames: TStrings; const AAppend: Boolean = True): Integer;Loads multiple SVG files and returns the count loaded.
Parameters:
AFileNames: List of file pathsAAppend: If True, append to existing icons; if False, clear first
Returns: Number of icons successfully loaded
Usage:
var
Files: TStringList;
Count: Integer;
begin
Files := TStringList.Create;
try
Files.LoadFromFile('iconlist.txt');
Count := SVGIconImageCollection1.LoadFromFiles(Files, True);
ShowMessage(Format('Loaded %d icons', [Count]));
finally
Files.Free;
end;
end;SaveToFile
function SaveToFile(const AFileName: string; const AImageName: string): Boolean;Saves a named icon to a file.
Parameters:
AFileName: Target file pathAImageName: Name of the icon to save
Returns: True if successful
Usage:
if SVGIconImageCollection1.SaveToFile('C:\Export\home.svg', 'home') then
ShowMessage('Icon saved successfully');Resource Operations
LoadFromResource
function LoadFromResource(const hInstance: THandle;
const ResourceName: string; const IconName: string): Integer;Loads an SVG from a compiled resource.
Parameters:
hInstance: Module handle (use HInstance for current executable)ResourceName: Name of the resourceIconName: Name to assign to the loaded icon
Returns: Index of the loaded icon
Usage:
// Add SVG to resources first (.rc file):
// HOME_ICON SVG "home.svg"
Index := SVGIconImageCollection1.LoadFromResource(HInstance, 'HOME_ICON', 'home');LoadFromString
function LoadFromString(const Source: string; const IconName: string): Integer;Loads an SVG from an XML string.
Parameters:
Source: SVG XML contentIconName: Name to assign to the icon
Returns: Index of the loaded icon
Usage:
const
HomeSVG = '<svg width="24" height="24"><path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>';
begin
Index := SVGIconImageCollection1.LoadFromString(HomeSVG, 'home');
end;Rendering Attributes
SetColors
procedure SetColors(const AFixedColor: TColor = SVG_INHERIT_COLOR;
const AApplyToRootOnly: Boolean = False;
const AAntiAliasColor: TColor = clBtnFace);Sets color attributes for the entire collection.
Usage:
SVGIconImageCollection1.SetColors(clBlue, False, clWhite);UpdateAttributes (Delphi 10.3+)
procedure UpdateAttributes(AFixedColor: TColor;
AApplyFixedColorToRootOnly: Boolean; AGrayScale: Boolean;
AAntiAliasColor: TColor; AOpacity: Byte);Updates all rendering attributes at once.
Usage:
SVGIconImageCollection1.UpdateAttributes(
clBlue, // FixedColor
False, // ApplyToRootOnly
False, // GrayScale
clWhite, // AntiAliasColor
255 // Opacity
);Access Methods (Delphi 10.3+)
IsIndexAvailable
function IsIndexAvailable(AIndex: Integer): Boolean;Checks if an index is valid.
Usage:
if SVGIconImageCollection1.IsIndexAvailable(5) then
ShowMessage('Index 5 exists');GetIndexByName
function GetIndexByName(const AName: String): Integer;Gets the index of a named icon.
Usage:
Index := SVGIconImageCollection1.GetIndexByName('home');GetNameByIndex
function GetNameByIndex(AIndex: Integer): String;Gets the name of an icon by index.
Usage:
Name := SVGIconImageCollection1.GetNameByIndex(0);Drawing
Draw
procedure Draw(ACanvas: TCanvas; ARect: TRect; AIndex: Integer;
AProportional: Boolean = False);Draws an icon directly to a canvas.
Parameters:
ACanvas: Target canvasARect: Rectangle to draw inAIndex: Icon indexAProportional: Maintain aspect ratio
Usage:
SVGIconImageCollection1.Draw(
Canvas,
Rect(10, 10, 74, 74),
0,
True
);GetBitmap (Delphi 10.3+)
function GetBitmap(AIndex: Integer; AWidth, AHeight: Integer): TBitmap;Returns a bitmap representation of an icon.
Parameters:
AIndex: Icon indexAWidth,AHeight: Desired bitmap size
Returns: New TBitmap instance (caller must free)
Usage:
var
Bitmap: TBitmap;
begin
Bitmap := SVGIconImageCollection1.GetBitmap(0, 48, 48);
try
Image1.Picture.Assign(Bitmap);
finally
Bitmap.Free;
end;
end;Miscellaneous
Assign
procedure Assign(Source: TPersistent); override;Assigns content from another collection.
Usage:
SVGIconImageCollection2.Assign(SVGIconImageCollection1);Examples
Basic Setup with Virtual ImageLists
procedure TForm1.FormCreate(Sender: TObject);
begin
// Load icons into collection (once)
var Files := TStringList.Create;
try
Files.Add('icons\home.svg');
Files.Add('icons\settings.svg');
Files.Add('icons\exit.svg');
SVGIconImageCollection1.LoadFromFiles(Files);
finally
Files.Free;
end;
// Configure small icons (16x16)
SVGIconVirtualImageListSmall.ImageCollection := SVGIconImageCollection1;
SVGIconVirtualImageListSmall.Size := 16;
// Configure large icons (48x48)
SVGIconVirtualImageListLarge.ImageCollection := SVGIconImageCollection1;
SVGIconVirtualImageListLarge.Size := 48;
// Use with controls
ToolBar1.Images := SVGIconVirtualImageListSmall;
ListView1.LargeImages := SVGIconVirtualImageListLarge;
// Same icons, different sizes, shared storage!
end;Dynamic Icon Loading
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
var ImageName: string;
SVGIconImageCollection1.LoadFromFile(OpenDialog1.FileName, ImageName);
ListBox1.Items.Add(ImageName);
end;
end;Icon Organization with Categories
procedure TForm1.LoadCategorizedIcons;
var
SVG: ISVG;
begin
SVG := GlobalSVGFactory.NewSvg;
// Navigation category
SVG.LoadFromFile('home.svg');
SVGIconImageCollection1.Add(SVG, 'home', 'Navigation');
SVG.LoadFromFile('back.svg');
SVGIconImageCollection1.Add(SVG, 'back', 'Navigation');
// Actions category
SVG.LoadFromFile('save.svg');
SVGIconImageCollection1.Add(SVG, 'save', 'Actions');
SVG.LoadFromFile('delete.svg');
SVGIconImageCollection1.Add(SVG, 'delete', 'Actions');
// Icons are named: Navigation\home, Navigation\back, Actions\save, Actions\delete
end;Theme Support
procedure TForm1.ApplyTheme(ADarkMode: Boolean);
begin
if ADarkMode then
begin
SVGIconImageCollection1.FixedColor := clWhite;
SVGIconImageCollection1.AntiAliasColor := $202020;
end
else
begin
SVGIconImageCollection1.FixedColor := clBlack;
SVGIconImageCollection1.AntiAliasColor := clWhite;
end;
// All linked virtual image lists automatically update
end;Batch Icon Import
procedure TForm1.ImportIconsFromDirectory(const APath: string);
var
Files: TStringList;
SR: TSearchRec;
begin
Files := TStringList.Create;
try
if FindFirst(TPath.Combine(APath, '*.svg'), faAnyFile, SR) = 0 then
begin
repeat
Files.Add(TPath.Combine(APath, SR.Name));
until FindNext(SR) <> 0;
FindClose(SR);
end;
var Count := SVGIconImageCollection1.LoadFromFiles(Files, True);
ShowMessage(Format('Imported %d icons', [Count]));
finally
Files.Free;
end;
end;Custom Drawing
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
R: TRect;
I: Integer;
begin
// Draw icon grid
for I := 0 to SVGIconImageCollection1.SVGIconItems.Count - 1 do
begin
R := Rect(
(I mod 4) * 64,
(I div 4) * 64,
(I mod 4) * 64 + 64,
(I div 4) * 64 + 64
);
SVGIconImageCollection1.Draw(PaintBox1.Canvas, R, I, True);
end;
end;Export Collection
procedure TForm1.ExportAllIcons(const AOutputPath: string);
var
I: Integer;
IconName, FileName: string;
begin
ForceDirectories(AOutputPath);
for I := 0 to SVGIconImageCollection1.SVGIconItems.Count - 1 do
begin
IconName := SVGIconImageCollection1.GetNameByIndex(I);
FileName := TPath.Combine(AOutputPath, IconName + '.svg');
// Create subdirectories for categories
ForceDirectories(ExtractFilePath(FileName));
SVGIconImageCollection1.SaveToFile(FileName, IconName);
end;
ShowMessage(Format('Exported %d icons', [SVGIconImageCollection1.SVGIconItems.Count]));
end;Notes
Benefits of ImageCollection Architecture
- Memory Efficiency: Store icons once, render at multiple sizes
- Centralized Management: Update all icon lists by changing the collection
- Design-Time Preview: Visual editing in the IDE
- Standard Integration: Works with VCL's Virtual ImageList system
Comparison with TSVGIconImageList
| Feature | TSVGIconImageCollection | TSVGIconImageList |
|---|---|---|
| Storage | Yes | Yes |
| Rendering | No (via VirtualImageList) | Yes |
| Delphi Version | 10.3+ | XE6+ |
| Multiple Sizes | Yes (efficient) | No (need separate lists) |
| Memory Usage | Lower (shared storage) | Higher (per list) |
| Complexity | Moderate (2 components) | Simple (1 component) |
When to Use
Use TSVGIconImageCollection when:
- Using Delphi 10.3 or later
- Need same icons at multiple sizes
- Want centralized icon management
- Building modern applications
Use TSVGIconImageList when:
- Supporting older Delphi versions (XE6-10.2)
- Simple single-size icon lists
- Quick prototyping
- Legacy code migration