为什么代码在调用 TImage.Picture.LoadFromFile 后继续?

Why does code continue after calling TImage.Picture.LoadFromFile?

我正在对标准 VCL TImage 控件和各种其他第三方替代品的性能进行一些比较。我正在使用 GetTickCount 来测量图像加载所需的时间。实际上,这大约需要 4 秒,但是 LoadFromFile() returns 几乎立即让我感到惊讶!

procedure TfrmMain.Button1Click(Sender: TObject);
begin
  FStart:= GetTickCount;
  imgStandard.Picture.LoadFromFile(txtFilename.Text);
  FEnd:= GetTickCount; //<-- Put a breakpoint here to observe immediate return in debug
  lblStandard.Caption:= IntToStr(FEnd-FStart)+' Msec';
end;

在两个图像控件中加载相同的 JPEG 文件在视觉上所花的时间与例如与 Graphics32 中的 TImage32 相比所花费的时间一样长。两者都按预期在这段时间内阻止了 UI,大约 4 秒。例如,GR32 报告 3734 毫秒,而标准的仅报告 16 毫秒。

为什么会这样?如何准确测量图像真正加载到 TImage 中所需的时间?

Why does this happen?

这两个控件的区别在于解压缩的处理方式。

  • 当您以这种方式使用 VCL 图像控件时,压缩数据是从文件中读取的,但实际解压缩仅在绘制控件时按需发生。
  • 当您使用 graphics32 图像控件时,加载文件时会发生解压缩。

你的图像几乎所有的时间都花在了解压缩步骤上,这解释了你的时间差异。

How can I accurately measure the time it takes for the image to really load into a TImage?

您可以在调用 LoadFromFile 之后立即插入对 imgStandard.Update 的调用,这将强制执行绘制循环,从而强制执行解压缩步骤。