Glen Morse
Glen Morse

Reputation: 2593

Different size images loading into TImageList

The images iam loading are slightly different sizes. how can i get the smallest size and re-size them all to that size?

The code below is the loading of the images -> converting to bmp -> adding to imagelist

after about 3 images it gives error invalid image size. due to image size too big for the size i gave imagelist at start.

procedure TForm1.LoadImages(const Dir: string);
var
  z,i: Integer;
  CurFileName: string;
  JpgIn: TJPEGImage;
  BmpOut: TBitmap;
begin
  i := 0;
  z := 1;
  while True do
  begin
    CurFileName := Format('%s%d.jpg',
                          [IncludeTrailingPathDelimiter(Dir), i]);
    if not FileExists(CurFileName) then
      Break;
    JpgIn := TJPEGImage.Create;
    try
      JpgIn.LoadFromFile(CurFileName);
      if z = 1 then
       begin
        ImageList1.SetSize(jpgin.width, jpgin.Height);
        z := 0;
       end;
      BmpOut := TBitmap.Create;
      try
         BmpOut.Assign(JpgIn);
         ImageList1.Add(BmpOut, nil);
      finally
        BmpOut.Free;
      end;
    finally
      JpgIn.Free;
    end;
    Inc(i);
  end;
  if ImageList1.Count > 0 then
  begin
    BmpOut := TBitmap.Create;
    try
      ImageList1.GetBitmap(1, BmpOut);
      zimage1.Bitmap.Assign(bmpout);
      zimage1.Repaint;
    finally
      BmpOut.Free;
    end;
  end;
end;

Upvotes: 0

Views: 3707

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597941

Once you start putting images into the TImageList, you cannot resize it, and all images must be the same size. So you will have to pre-load all of the images ahead of time so you can then determine the smallest size available, THEN crop/stretch any larger images to the smaller size, THEN you can load the final images into the TImageList.

Have your loop store all of the TJPEGImage into a TList or TObjectList and not free them right away, then you can loop through that list calculating the smallest size, then loop through the list again resizing the images as needed, then loop through the list again adding the images to the TImageList, and then finally loop through the list freeing the images.

Upvotes: 2

Related Questions