2014-09-25 85 views
0

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

我要使用质量好的JPG不是位图:( *是否有可能对那些26倍的图像加载到的TImageList并仍然使用图像以其质量 或 *我可以将图像保存在某种数据库并加载到德尔福

Loading images and converting to bitmap 没有帮助,因为我不希望使用位图。 我也不想使用任何第三方组件,因为这个程序必须在默认运行德尔福2010年

+0

关于数据库部分[保存和加载JPEG图像数据库....(http://stackoverflow.com/questions/18214686/saving-and-loading-jpeg-images-to-database-不工作的delphi) – bummi 2014-09-26 06:42:55

+1

我不确定你应该在这种情况下使用'TImageList'来存储(高分辨率?)图像(不是图标)。改为创建并使用通用的'TObjectList '。 – teran 2014-09-26 09:28:44

+0

为什么你不要在每个TJpegImage中创建和存储TJpegImage数组并存储单个图像。 – SilverWarior 2014-09-26 10:43:48

回答

2

正如我在coment中所提到的,您可以创建一个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对象自理。因此,在尝试将任何图像数据加载到它们之前,请不要忘记创建它们。如果不这样做将导致访问冲突。

而且becouse您自行创建这些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; 
+0

非常感谢兄弟!我使用了第一种方法,它完美地工作。 有点话题...你说我需要释放它 'procedure TForm1.FormDestroy(Sender:TObject); var I:Integer; 开始 对于I:= 1至26做 开始 图像[I] .Free; 结束; 结束;' 我不明白我的影响? – GerhardAA 2014-09-27 09:21:33

+0

由于尺寸限制,使用coment/s不容易解释,但我会尝试。首先你需要了解组件和常规类之间的区别(组件是特殊类)。创建组件时,您将在创建过程中为其分配所有者。所以现在,当拥有者正在使用destroeyd时,它会通知它拥有的所有组件,以便它们也销毁它们(释放自己的内存)。因此,如果组件不需要特别注意,您可以让所有者在其创建过程中让lill(没有所有者)。它继续... – SilverWarior 2014-09-27 11:24:26

+0

但是,当你在你的情况下创建其他类像TJPEGImage时,你不能指定一个所有者,这意味着当他们需要销毁它们时不会通知他们,你可以通过免费调用他们每个人。这意味着他们永远不会释放他们正在使用的内存。这种情况被称为“内存泄漏”。现在确实,在你的特定情况下,不释放TJPEGImage对象不会引起太多问题,因为在应用程序终止时它们的内存将被释放。它会继续...... – SilverWarior 2014-09-27 11:28:08