我将一些图像加载到对象列表中,然后尝试调用它们。但它没有显示图像?
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;
如果你注意到我有这个部分在那里看看是否将iamge加载到zimage1是一个问题。
//zimage1.Bitmap.Width := bmpout.Width;
//zimage1.Bitmap.Height := bmpout.Height;
//ZImage1.Bitmap.Assign(bmpout);
当我这样做的时候,它将bmpout加载到zimage1中,这导致我用对象列表来思考它做错了什么?
答案 0 :(得分:5)
您有以下代码:
CurIdx:= mylist.Add(TBitmap(bmpout));
finally
//free bitmap and jpg
BmpOut.Free;
end;
您将一个项目添加到列表中,然后您立即释放该项目。 TList
及其TObjectList
等后代不会对其所持物体进行“深层复制”。
当您稍后阅读列表的内容时,您获得的是过时的引用。它们不再引用它们最初引用的对象,它们可能根本不引用任何对象。这些位置的内存可能仍然包含结构化的数据,看起来像那些以前的对象,但不能保证。无法保证您的程序也不会崩溃。崩溃是典型的,但下一个最常见的行为是程序显示细微错误,例如继续运行但不显示预期数据。
如果你想要一个对象列表,那么就不要释放它们;摆脱我上面引用的finally
块。为了例外安全,您需要小心地将所有权从当前代码块转移到列表中,如下所示:
BmpOut := TBitmap.Create;
try
BmpOut.Assign(JpgIn);
CurIdx := mylist.Add(bmpout);
except
BmpOut.Free;
raise;
end;
如果在Assign
或Add
调用期间发生异常,则本地过程应释放位图。否则,列表将获取对象的所有权,释放列表将隐式释放其所有对象(假设列表的OwnsObjects
属性为True
)。
一旦你加载了所有的位图并将它们存储在列表中,就没有必要创建另一个位图,只是要为你的图像控件分配一些东西。您可以使用列表中存储的那个:
if mylist.Count > 0 then
begin
ZImage1.Bitmap.Assign(mylist[mylist.Count - 1]);
end;
从该代码中,您可以看到您甚至不需要跟踪CurIdx
。它的值始终是最后添加的对象的索引,并且该索引总是比列表中的对象总数少一个。
在为其他图形对象赋值之前,您也不必设置位图的高度和宽度。它将自动获取源对象的尺寸。