Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi problem mit dynamischem array (https://www.delphipraxis.net/108888-problem-mit-dynamischem-array.html)

marabu 20. Feb 2008 20:44

Re: problem mit dynamischem array
 
Zitat:

Zitat von chamop87
ich ziehe dort jeweils einen ab, weil das array ja zwangsweise bei null losgeht.
beispiel
anzahl der bilder=10

wenn ich dann schreibe setlength(array,10) habe ich elf speicherplatze, ich brauche aber nur zehn.

Das ist dein Irrtum.

SetLength(randoms, 10) führt zu einem Array mit den Indewerten 0 bis 9 und insgesamt 10 Items. Lies nochmal genau in der Hilfedatei nach.

Gute Nacht.

chamop87 20. Feb 2008 20:49

Re: problem mit dynamischem array
 
genau das habe ich doch auch gesagt

mkinzler 20. Feb 2008 20:50

Re: problem mit dynamischem array
 
Nein du gingst von 11 aus

busybyte 20. Feb 2008 20:51

Re: problem mit dynamischem array
 
Dein Code ist Horror,ich habe ihn in soweit korrigiert ,das er für mich halbwegs einen Sinn
ergeben könnte.

Delphi-Quellcode:
procedure TForm1.FormCreate(Sender: TObject);
var fa:integer;
begin
randomize;
zaehl:=0;
filelistbox1.Directory:='C:\';//Eigene Dateien\Eigene Bilder\';
filelistbox1.Mask:='*.bmp;*.jpg;*.jpeg';

if filelistbox1.Items.Count>1 then
begin
setlength(randoms,filelistbox1.Items.Count);  //Edit
bildanzahl:=filelistbox1.items.count;  //Edit
end;
ca:=0;
memo1.lines.add(inttostr(filelistbox1.Items.count));
for fa:=low(randoms) to high(randoms) do
randoms[fa]:=fa;
//randoms[11]:=11;
end;//procedure

procedure TForm1.Button1Click(Sender: TObject);
var dateiname,endung:string;
    fi,fa,fb,zufall,bildnr:integer;
begin
//for fa:=0 to filelistbox1.items.count-1 do begin



//Edit
  zufall:=bildanzahl;
  while zufall>high(randoms) do Zufall:=random(bildanzahl); //Edit

//while (zufall>Length(randoms)-1) do exit;
  bildnr:=randoms[zufall];

  //Edit
  for fa:=0 to filelistbox1.Items.count-1 do
  begin
  test:=inttostr(randoms[fa]);
  memo1.lines.add(inttostr(randoms[fa])+' '+inttostr(bildnr));
  end;


 //Letztes Edit memo1.lines.add(test+' '+inttostr(bildnr));
test:='';
  randoms[zufall]:=randoms[bildanzahl-1];    //Edit
  inc(bildanzahl,-1);
  if bildanzahl<0 then begin
    for fb:=0 to filelistbox1.items.count-1 do randoms[fb]:=fb;
    bildanzahl:=filelistbox1.items.count;
  end;

////EDIT
while (bildnr<0) or (bildnr>filelistbox1.items.count-1) do exit;

dateiname:=filelistbox1.items[bildnr];

for fi:=length(dateiname)-3 to length(dateiname) do endung:=endung+dateiname[fi];
endung:=lowercase(endung);
jpeganzeigen(dateiname,endung);
//inc(zaehl);
end;//procedure

procedure TForm1.jpeganzeigen(dateiname:TFilename;endung:string);
var jpeg: TJPEGImage;
begin
bmp := TBitmap.Create;
//Bitmaps anzeigen
if endung='.bmp' then bmp.loadfromfile(dateiname);
//Jpegs anzeigen
if (endung='.jpg') or (endung='jpeg') then begin
  jpeg := TJPEGImage.Create;
  try
    jpeg.CompressionQuality := 100; {Default Value}
    jpeg.LoadFromFile(dateiname);
    bmp.Assign(jpeg);
  finally
    jpeg.Free
  end;
end;//if
image1.picture.bitmap:=bmp;
image1.AutoSize:=true;
bmp.free;
end;//procedure;

busybyte 20. Feb 2008 20:58

Re: problem mit dynamischem array
 
Tipp:
lagere das ganze in eine eigene function oder precdure aus und übergebe die Parameter
=mehr Übersicht (wer macht was und was soll dabei herauskommen)

busybyte 20. Feb 2008 21:56

Re: problem mit dynamischem array
 
Delphi-Quellcode:
unit jpganzeigen02;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, FileCtrl,jpeg, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    FileListBox1: TFileListBox;
    Image1: TImage;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  public
   Function getZufall(bildanzahl_:integer):integer;
   Function debugrandoms(L1_,BildNr_:integer):TStrings;
   procedure jpeganzeigen(dateiname:TFilename; endung:string);
   Procedure iniRandoms(L1_:integer);
 { Public-Deklarationen }
  end;

var Form1: TForm1;

implementation {$R *.dfm}

var bmp: TBitmap;
    bildanzahl:integer;
    randoms:array of integer;
 TestS:string;
 Testi:integer;


Procedure TForm1.iniRandoms(L1_:integer);
Var cnt1:integer;
begin
for cnt1:=0 to L1_-1 do randoms[cnt1]:=cnt1;
bildanzahl:=L1_;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
randomize;

with Memo1 do
begin
Align:=alBottom;
Scrollbars:=ssBoth;
end;

with filelistbox1 do
begin
Directory:='C:\Windows';//Eigene Dateien\Eigene Bilder\';
Mask:='*.bmp;*.jpg;*.jpeg';

 if Items.Count>1 then
 begin
 setlength(randoms,Items.Count);  //Edit
 iniRandoms(items.count);
 end;

end;


//randoms[11]:=11;
end;//procedure



Function TForm1.debugrandoms(L1_,BildNr_:integer):TStrings;
Var cnt1:integer;
begin
Result:=TStringlist.Create;
Result.add('BildNr: '+inttostr(BildNr_));
//Länge überschritten ? wenn ja dann reduzieren
while (L1_>Length(randoms)-1) do L1_:=High(randoms);

  for cnt1:=0 to L1_-1 do
  Result.add('Randoms' +inttostr(cnt1)+': '+inttostr(randoms[cnt1]));
  Result.add('*** end of debug ***');
end;


Function TForm1.getZufall(bildanzahl_:integer):integer;
begin
 
  Result:=Random(bildanzahl_); //Edit;
  while Result>high(randoms) do Result:=random(bildanzahl_); //Edit
end;


procedure TForm1.Button1Click(Sender: TObject);
var dateiname,endung:string;
    fi,fa,fb,zufall,bildnr:integer;
begin

  zufall:=GetZufall(bildanzahl);
  bildnr:=randoms[zufall];
  //debug anzeigen
  memo1.lines.Assign(DebugRandoms(filelistbox1.Items.count-1,bildnr));

  dateiname:=filelistbox1.items[bildnr];
  endung:=ExtractFileExt(dateiname);
  jpeganzeigen(dateiname,endung);


  randoms[zufall]:=randoms[bildanzahl-1];
  dec(bildanzahl);
   if bildanzahl<0 then iniRandoms(filelistbox1.items.count);
//inc(zaehl);
end;//procedure



procedure TForm1.jpeganzeigen(dateiname:TFilename;endung:string);
var jpeg: TJPEGImage;
begin
bmp := TBitmap.Create;
//Bitmaps anzeigen
if endung='.bmp' then bmp.loadfromfile(dateiname);
//Jpegs anzeigen
if (endung='.jpg') or (endung='jpeg') then begin
  jpeg := TJPEGImage.Create;
  try
    jpeg.CompressionQuality := 100; {Default Value}
    jpeg.LoadFromFile(dateiname);
    bmp.Assign(jpeg);
  finally
    jpeg.Free
  end;
end;//if
image1.picture.bitmap:=bmp;
image1.AutoSize:=true;
bmp.free;
end;//procedure;

end.


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:18 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz