function Farbzahl(bm: TBitmap): integer;
var
bits: TBits;
hlp: TBitmap;
scan: ^Integer;
x, y: Integer;
begin
hlp := TBitmap.Create;
hlp.pixelFormat := pf24bit;
hlp.height := bm.height;
hlp.width := bm.width;
hlp.canvas.draw(0, 0, bm);
Result := 0;
bits := TBits.Create;
bits.Size := 16777216;
hlp.pixelFormat := pf32bit;
for y := 0 to hlp.height - 1 do begin
scan := hlp.ScanLine[y];
for x := 0 to hlp.width - 1 do begin
if not bits[scan^] then begin
inc(Result);
bits[scan^] := true;
end;
inc(scan);
end;
end;
bits.Free;
hlp.Free;
end;
// Beispielaufruf
procedure TForm1.Button10Click(Sender: TObject);
var az: integer;
begin
az := Farbzahl(Image1.picture.bitmap);
if az = 0 then showmessage('Fehler')
else showmessage('Dieses Bild hat ' + inttostr(az) + ' Farben.');
end;