正如我在评论中提到的,您可以创建一个 TJPEGImage 对象数组来存储图像。
你这样做:
//Global array for storing images
var Images: Array [1..26] of TJPEGImage;
implemenetation
...
procedure TForm1.FormCreate(Sender: TObject);
var I: Integer;
begin
for I := 1 to 26 do
begin
//Since TJPEGIMage is a class we first need to create each one as array only
//stores pointer to TJPEGImage object and not the object itself
Images[I] := TJPEGImage.Create;
//Then we load Image data from file into each TJPEGImage object
//If file names are not numerically ordered you would probably load images
//later and not inside this loop. This depends on your design
Images[I].LoadFromFile('D:\Image'+IntToStr(I)+'.jpg');
end;
end;
正如您在源评论中看到的,该数组仅存储指向 TJPEGImage 对象的指针,而不是 TJPEGImage 对象本身。所以不要忘记在尝试将任何图像数据加载到它们之前创建它们。否则将导致访问冲突。
另外,因为您自己创建了这些 TJPEGImage 对象,您还需要自己释放它们以避免可能的内存泄漏
procedure TForm1.FormDestroy(Sender: TObject);
var I: Integer;
begin
for I := 1 to 26 do
begin
Images[I].Free;
end;
end;
为了在您的 TImage 组件中显示这些存储的图像,请使用此
//N is array index number telling us which array item stores the desired image
Image1.Picture.Assign(Images[N]);
您可以使用的第二种方法
现在,由于 TJPEGImage 是分类对象,您还可以使用 TObjectList 来存储指向它们的指针。在这种情况下,创建代码看起来像这样
procedure TForm1.FormCreate(Sender: TObject);
var I: Integer;
Image: TJPEGImage;
for I := 1 to NumberOfImages do
begin
//Create TObject list with AOwnsObjects set to True means that destroying
//the object list will also destroy all of the objects it contains
//NOTE: On ARC compiler destroying TObjectList will only remove the reference
//to the objects and they will be destroyed only if thir reference count
//drops to 0
Images := TObjectList.Create(True);
//Create a new TJPEGImage object
Image := TJPEGImage.Create;
//Load image data into it from file
Image.LoadFromFile('Image'+IntToStr(I)+'.jpg');
//Add image object to our TObject list to store reference to it for further use
Images.Add(Image);
end;
end;
您现在将像这样显示这些图像
//Note becouse first item in TObject list has index of 0 you need to substract 1
//from your ImageNumber
Image1.Picture.Assign(TJPEGImage(Images[ImageNumber-1]));
由于我们将 TObjectList 设置为拥有我们的 TJPEGImage 对象,因此我们可以像这样快速销毁所有对象
//NOTE: On ARC compiler destroying TObjectList will only remove the reference
//to the objects and they will be destroyed only if thir reference count
//drops to 0
Images.Free;