Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
Delphi 10.4 Sydney
|
AW: Access violation bei dynamischen Arrays
14. Jun 2011, 18:30
Destroyer,
so läuft es wenigstens mal durch.
Delphi-Quellcode:
type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
end;
TIntegerList = class(TObject)
procedure Add(const Value: integer);
function IndexOf(const Value: integer): integer;
procedure IncItem(const Index: integer);
private
FItems: array of integer;
FCount: integer;
protected
procedure SetCount(const NewCount: integer);
function GetItem(Index: integer): integer;
procedure SetItem(Index: integer; const Value: integer);
public
constructor Create;
destructor Destroy; override;
property Count: integer Read FCount;
property Item[Index: integer]: integer Read GetItem Write SetItem;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TIntegerList.SetCount(const NewCount: integer);
begin
FCount:= NewCount;
SetLength(FItems, FCount);
end;
function TIntegerList.GetItem(Index: integer): integer;
begin
Result:= FItems[Index];
end;
procedure TIntegerList.SetItem(Index: integer; const Value: integer);
begin
FItems[Index]:= Value;
end;
procedure TIntegerList.Add(const Value: integer);
begin
SetCount(FCount+1);
SetItem(FCount-1, Value);
end;
function TIntegerList.IndexOf(const Value: integer): integer;
var
I: integer;
begin
Result:= -1;
for I:= 0 to FCount-1 do
if FItems[I] = Value then
begin
Result:= I;
Break;
end;
end;
procedure TIntegerList.IncItem(const Index: integer);
begin
FItems[Index]:= FItems[Index]+1;
end;
constructor TIntegerList.Create;
begin
inherited Create;
SetCount(0);
end;
destructor TIntegerList.Destroy;
begin
SetCount(0);
inherited Destroy;
end;
procedure GetBmpColors (const Bmp: TBitmap; const SL: TStringList);
var
Farben, Anzahl: TIntegerList;
I, J, Index, idx: integer;
begin
Farben:= TIntegerList.Create;
Anzahl:= TIntegerList.Create;
for I:= 0 to Bmp.Height-1 do
begin
for J:= 0 to Bmp.Width-1 do
begin
idx:= Bmp.Canvas.Pixels[I, J];
Index:= Farben.IndexOf(idx);
if Index = -1 then
begin
Farben.Add(idx);
Anzahl.Add(1);
end
else
Anzahl.IncItem(Index);
end;
end;
for I:= 0 to Farben.Count-1 do
SL.Add('Farbwert: ' + IntToStr(Farben.GetItem(I))
+ '; Vorhanden: ' + IntToStr(Anzahl.GetItem(I)) + 'x');
Farben.Free;
Anzahl.Free;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
SL: TStringlist;
Bmp: TBitmap;
begin
SL:= TStringlist.Create;
Bmp:= TBitmap.Create;
Bmp.LoadFromFile('..');
GetBmpColors (Bmp, SL);
SL.SaveToFile('..');
Bmp.Free;
SL.Free;
end;
Geändert von Bjoerk (14. Jun 2011 um 18:41 Uhr)
Grund: Anzahl.IncItem(Index);
|
|
Zitat
|