繁体   English   中英

图片数据库,TDBImages,TImageList,Delphi

[英]Picture Database, TDBImages, TImageList, Delphi

我正在编写一个显示图片(地图)的程序。 当您单击图片的一部分时,必须将其放大。共有26张图片(包括主图片)。 我想将这些图片加载到Delphi中,并用Amusement_park.jpg替换Image1(Whole_map.jpg)。

我想使用高质量的jpg而不是位图:( *是否可以将这26张图像加载到TImageList中,并且仍使用其质量的图像,或者*我可以将图像保存在某种数据库中并加载到Delphi中吗

加载图像并转换为位图无济于事,因为我不想使用位图。 我也不想使用任何第三方组件,因为该程序必须在默认的Delphi 2010上运行。

如我的评论所述,您可以创建一个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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM