2014-09-25 55 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