Jerry Dodge
Jerry Dodge

Reputation: 27296

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

I'm doing some comparisons between the performance of the standard VCL TImage control and various other third-party alternatives. I'm using GetTickCount to measure how long it takes for the image to load. In reality it takes about 4 seconds, however LoadFromFile() returns almost immediately to my surprise!

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;

Loading the same JPEG file in both the image controls visually takes just as long as, for example, comparing with the TImage32 from Graphics32. Both block the UI during this time as expected, at about 4 seconds. The GR32 reports, for example, 3734 Msec, while the standard one only reports 16 Msec.

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

Upvotes: 3

Views: 385

Answers (1)

David Heffernan
David Heffernan

Reputation: 613432

Why does this happen?

The difference between the two controls is to do with how the decompression is handled.

  • When you use the VCL image control in this way, the compressed data is read from the file, but the actual decompression only happens on demand, when the control is painted.
  • When you use the graphics32 image control, the decompression happens when you load the file.

Almost all of the time taken for your image is for the decompression step, which explains the the discrepancy in your timings.

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

You could insert a call to imgStandard.Update immediately following the call to LoadFromFile, which would force a paint cycle, and hence force the decompression step.

Upvotes: 6

Related Questions