-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageListEx.pas
119 lines (101 loc) · 2.96 KB
/
ImageListEx.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
unit ImageListEx;
interface
uses
Classes, Controls, Graphics;
type
TImageListEx = class helper for TImageList
public
procedure LoadAndSplitImages(filename: string);
end;
implementation
(*
If Images < 1 then entire AGraphic will be split up into
individual images, otherwise only NumberOfImages will
be added to the ImageList.
Images are processed from left to right and top to bottom.
Source: http://www.delphipages.com/forum/showpost.php?p=154601&postcount=8
*)
procedure SetImageList(AGraphic: TGraphic; ImageList: TImageList; ClearList: boolean; NumberOfImages: integer = 0);
var
mask, image, bmp: TBitmap;
AColor: TColor;
modX, modY, x, y, xx, yy, imgWid, imgHgt: integer;
cnt: integer;
begin
Assert((AGraphic <> nil) and Assigned(ImageList));
if ClearList then ImageList.Clear;
mask := TBitmap.Create;
image := TBitmap.Create;
bmp := TBitmap.Create;
try
image.PixelFormat := pf24bit;
image.Width := AGraphic.Width;
image.Height := AGraphic.Height;
image.Canvas.Draw(0, 0, AGraphic);
//Get the lower left pixel color (the transparent color).
AColor := image.Canvas.Pixels[0, image.Height -1];
x := ImageList.Width;
y := ImageList.Height;
modX := x -(image.Width mod x);
modY := y -(image.Height mod y);
if (modX <> x) or (modY <> y) then
begin
//Resize image bitmap so that it's width and height
//are a multiple of the imagelist's width and height.
image.Width := AGraphic.Width +modX;
image.Height := AGraphic.Height +modY;
image.Canvas.Brush.Color := AColor;
image.Canvas.FillRect(Rect(0, 0, image.Width, image.Height));
//Draw the graphic centered on the image bitmap.
image.Canvas.Draw(modX div 2, modY div 2, AGraphic);
end;
imgWid := image.Width;
imgHgt := image.Height;
//Size bmp to the imagelist's sizes.
bmp.Width := x;
bmp.Height := y;
if NumberOfImages > 0 then
cnt := NumberOfImages
else
cnt := (imgWid div x) *(imgHgt div y);
xx := 0;
yy := 0;
while (cnt > 0) and (yy < imgHgt) do
begin
//Draw at negative xx/yy (only the portion that overlaps
//the canvas will be drawn.
bmp.Canvas.Draw(-xx, -yy, image);
//Create the mask for transparency.
mask.Assign(bmp);
mask.Canvas.Brush.Color := AColor;
mask.Monochrome := true;
//Add the bmp to the imagelist.
ImageList.Add(bmp, mask);
Dec(cnt);
Inc(xx, x);
if xx +x > imgWid then
begin
//Get the next row.
xx := 0;
Inc(yy, y);
end;
end;
finally
bmp.Free;
mask.Free;
image.Free;
end;
end;
procedure TImageListEx.LoadAndSplitImages(filename: string);
var
bmp : TBitmap;
begin
bmp := TBitmap.Create;
try
bmp.LoadFromFile(filename);
SetImageList(bmp, Self, true);
finally
bmp.Free;
end;
end;
end.