imagedelphijpegdelphi-2010timagelist

Picture Database, TDBImages, TImageList, Delphi


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.


Solution

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