0

我加载的图像大小略有不同。我怎样才能得到最小的尺寸并将它们全部重新调整到那个尺寸?

下面的代码是图像的加载->转换为bmp->添加到imagelist

大约 3 张图像后,它给出了错误无效的图像大小。由于图像大小对于我在开始时给出的图像列表的大小来说太大了。

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

1 回答 1

2

一旦开始将图像放入 中TImageList,就无法调整它的大小,并且所有图像的大小必须相同。因此,您必须提前预加载所有图像,以便确定可用的最小尺寸,然后将任何较大的图像裁剪/拉伸到较小的尺寸,然后您可以将最终图像加载到TImageList.

让您的循环将所有内容存储TJPEGImage到 aTListTObjectList不立即释放它们,然后您可以循环遍历该列表以计算最小尺寸,然后根据需要再次循环遍历列表调整图像大小,然后再次循环遍历列表添加图像到TImageList, 然后最后循环释放图像的列表。

于 2012-06-12T01:45:11.710 回答