Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi objectlist未检索数据_Delphi_Tobjectlist - Fatal编程技术网

Delphi objectlist未检索数据

Delphi objectlist未检索数据,delphi,tobjectlist,Delphi,Tobjectlist,我将一些图像加载到对象列表中,然后尝试调用它们。但它没有显示图像 procedure TForm1.LoadImages(const Dir: string); var i: Integer; CurFileName: string; JpgIn: TJPEGImage; BmpOut: TBitmap; begin //sets index for object list CurIdx := -1; i := 0; while True do begin //g

我将一些图像加载到对象列表中,然后尝试调用它们。但它没有显示图像

procedure TForm1.LoadImages(const Dir: string);
var
  i: Integer;
  CurFileName: string;
  JpgIn: TJPEGImage;
  BmpOut: TBitmap;
begin
//sets index for object list
  CurIdx := -1;
  i := 0;
  while True do
  begin
//gets current folder 
    CurFileName := Format('%s%d.jpg',
                          [IncludeTrailingPathDelimiter(Dir), i]);
    if not FileExists(CurFileName) then
      Break;
//creates jpgin
    JpgIn := TJPEGImage.Create;
    try
//loads jpgin 
      JpgIn.LoadFromFile(CurFileName);
//creates TBitmap and sets width to same as jpgs
      BmpOut := TBitmap.Create;
      bmpout.Width := jpgin.Width;
      bmpout.Height := jpgin.Height;
     try
         BmpOut.Assign(JpgIn);
//if i assign it here it works, showing last image of course
         //zimage1.Bitmap.Width := bmpout.Width;
        //zimage1.Bitmap.Height := bmpout.Height;
         //ZImage1.Bitmap.Assign(bmpout);
//adds 1 to index for object list. thus starting at 0
         curIdx := curIdx+1;
//add bitmap to objectlist
         CurIdx:= mylist.Add(TBitmap(bmpout));
      finally
//free bitmap and jpg
        BmpOut.Free;
      end;
    finally
      JpgIn.Free;
    end;
    Inc(i);
  end;
//makes sure cout is above 0  
  if mylist.Count > 0 then
  begin
//create bitmap
    BmpOut := TBitmap.Create;
      try
//sets width and heigh of bitmap before getting image
      bmpout.Height := TBitmap(mylist[curidx]).Height;
      bmpout.Width := TBitmap(mylist[curidx]).Width;
      bmpout.Assign(TBitmap(mylist[CurIdx]));
//sets zimage width height before getting image.
      zimage1.Bitmap.Width := bmpout.Width;
      zimage1.Bitmap.Height := bmpout.Height;
      ZImage1.Bitmap.Assign(bmpout);
    finally
      BmpOut.Free;
    end;
  end;
  page:= '0';
end;
如果你注意到我有这个部分在那里,看看是否加载到zimage1的iamge是一个问题

     //zimage1.Bitmap.Width := bmpout.Width;
    //zimage1.Bitmap.Height := bmpout.Height;
     //ZImage1.Bitmap.Assign(bmpout);
当我这样做时,它将bmpout加载到zimage1中,这让我想到我做错了对象列表的某些方面?

您有以下代码:

  CurIdx:= mylist.Add(TBitmap(bmpout));
finally
  //free bitmap and jpg
  BmpOut.Free;
end;
您将一个项目添加到列表中,然后立即释放该项目。TList及其子体(如TObjectList)不会对它们所持有的对象进行深度复制

当您稍后阅读列表的内容时,您得到的是过时的引用。它们不再引用最初引用的对象,并且可能根本不引用任何对象。这些位置的内存可能仍然包含结构类似于以前的对象的数据,但这不能保证。也不能保证你的程序不会崩溃。崩溃是典型的,但下一个最常见的行为是程序出现细微错误,例如继续运行但不显示预期数据

如果想要对象列表,则不要释放它们;摆脱我上面引用的最后一块。为了例外安全,您需要小心地将所有权从当前代码块转移到列表中,如下所示:

BmpOut := TBitmap.Create;
try
  BmpOut.Assign(JpgIn);
  CurIdx := mylist.Add(bmpout);
except
  BmpOut.Free;
  raise;
end;
如果在分配或添加调用期间发生异常,则本地过程应释放位图。否则,列表将获得对象的所有权,如果列表的OwnsObjects属性为True,则释放列表将隐式释放其所有对象

一旦加载了所有位图并将其存储在列表中,就不需要创建另一个位图来分配给图像控件。您可以使用存储在列表中的:

if mylist.Count > 0 then
begin
  ZImage1.Bitmap.Assign(mylist[mylist.Count - 1]);
end;
从这些代码中,您可以看到,您甚至不需要跟踪CurIdx。它的值将始终是最后添加的对象的索引,并且该索引将始终比列表中的对象总数少一个

在从另一个图形对象为位图赋值之前,也不必设置位图的高度和宽度。它将自动获取源对象的尺寸。

您有以下代码:

  CurIdx:= mylist.Add(TBitmap(bmpout));
finally
  //free bitmap and jpg
  BmpOut.Free;
end;
您将一个项目添加到列表中,然后立即释放该项目。TList及其子体(如TObjectList)不会对它们所持有的对象进行深度复制

当您稍后阅读列表的内容时,您得到的是过时的引用。它们不再引用最初引用的对象,并且可能根本不引用任何对象。这些位置的内存可能仍然包含结构类似于以前的对象的数据,但这不能保证。也不能保证你的程序不会崩溃。崩溃是典型的,但下一个最常见的行为是程序出现细微错误,例如继续运行但不显示预期数据

如果想要对象列表,则不要释放它们;摆脱我上面引用的最后一块。为了例外安全,您需要小心地将所有权从当前代码块转移到列表中,如下所示:

BmpOut := TBitmap.Create;
try
  BmpOut.Assign(JpgIn);
  CurIdx := mylist.Add(bmpout);
except
  BmpOut.Free;
  raise;
end;
如果在分配或添加调用期间发生异常,则本地过程应释放位图。否则,列表将获得对象的所有权,如果列表的OwnsObjects属性为True,则释放列表将隐式释放其所有对象

一旦加载了所有位图并将其存储在列表中,就不需要创建另一个位图来分配给图像控件。您可以使用存储在列表中的:

if mylist.Count > 0 then
begin
  ZImage1.Bitmap.Assign(mylist[mylist.Count - 1]);
end;
从这些代码中,您可以看到,您甚至不需要跟踪CurIdx。它的值将始终是最后添加的对象的索引,并且该索引将始终比列表中的对象总数少一个


在从另一个图形对象为位图赋值之前,也不必设置位图的高度和宽度。它将自动获取源对象的尺寸。

您正在将bmpout添加到列表mylist.addtbitmappbmpout,然后将其释放为bmpout.Free。添加不会创建对象的副本。因此,列表中的指针无效。当然,我不知道mylist变量所说的Add行为是什么。但我想这是一份清单。请注意,行curIdx:=curIdx+1;这是不必要的;第二:在将bmpout添加到列表中时,您不必将bmpout强制转换为TBitmap。在调用它之前,我已在底部附近再次创建了它?不。您是在第一次创建它bmpout:=TBitmap.Create,然后从文件加载图像,然后将其添加到列表中。现在bmpout和mylist[curidx]都指向同一个对象。在mylist[curidx]的bmpout.free值无效后。啊,它起作用了,在回复中发布,我会接受。但我也不想在一个星期前把它放出来
图像的ew文件夹已正确加载。因此释放mylist中的所有IAMGE?您将bmpout添加到列表mylist.addTBitmapbmpout,然后释放它bmpout.Free。添加不会创建对象的副本。因此,列表中的指针无效。当然,我不知道mylist变量所说的Add行为是什么。但我想这是一份清单。请注意,行curIdx:=curIdx+1;这是不必要的;第二:在将bmpout添加到列表中时,您不必将bmpout强制转换为TBitmap。在调用它之前,我已在底部附近再次创建了它?不。您是在第一次创建它bmpout:=TBitmap.Create,然后从文件加载图像,然后将其添加到列表中。现在bmpout和mylist[curidx]都指向同一个对象。在mylist[curidx]的bmpout.free值无效后。啊,它起作用了,在回复中发布,我会接受。但是我也不想在新的图像文件夹正确加载之前释放bmpout。这样释放了我列表中的所有符号?