0
votes

I am writing a programme that shows a picture(map). when you click on a part of the picture it must Zoom in. There are 26 pictures in total(Including main picture). I Want to load those pictures into Delphi and replace Image1(Whole_map.jpg) with Amusement_park.jpg.

I want to use the good quality jpg not bitmaps :( *Is it possible to load those 26 images into TImageList and still use the images with its quality or *Can i save the images in some sort of Database and load it into Delphi

Loading images and converting to bitmap doesn't help because i don't want to use bitmaps. I also don't want to use any 3rd party components because this program must run on default Delphi 2010.

1
Regarding the database part Saving and loading jpeg images to database ....bummi
I'm not sure you should use TImageList in this case to store (hi-res?) images (not icons). create and use generic TObjectList<TImage> instead.teran
Why don't you siply create and array of TJpegImage and store individual images in each TJpegImage.SilverWarior
@SilverWarior I have created an array arrPics : array[1..26] of TJPEGImage; how do I add the images to it? I have tried loading the location names(string) into the array but it ain't working.GerhardAA
Without you providing some code of how you did it, its hard to say why it is not working.Ancaron

1 Answers

2
votes

As mentioned in my coment you can create an array of TJPEGImage objects to store the images.

You do this like so:

//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;

As you see in source coments the array only stores pointers to TJPEGImage objects and not the TJPEGImage objects themself. So don't forget to create them before trying to load any image data to them. Failing to do so will result in Access Violation.

Also becouse you have created these TJPEGImage objects by yourself you also need to free them by yourself to avoid posible memory leaks

procedure TForm1.FormDestroy(Sender: TObject);
var I: Integer;
begin
  for I := 1 to 26 do
  begin
    Images[I].Free;
  end;
end;

In order to show these stored images in your TImage component use this

//N is array index number telling us which array item stores the desired image
Image1.Picture.Assign(Images[N]); 

Second approach that you can use

Now since TJPEGImage are classed objects you could also use TObjectList to store pointers to them. In such case creation code would look like this

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;

You would now show these images like so

//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]));

Since we set TObjectList to own our TJPEGImage objects we can quickly destroy all of them like so

//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;