Thanks to anyone who knows
You can do the loading in a separate thread that is started in the
program DPR file before the main form is created. I don't know if that
would gain you much, though, since the time-consuming part is the
conversion of the JPEG to a TBitmap for display on screen. The
following example loads the JPG and converts it to a TBitmap in a
secondary thread. The bitmap is then handed to the main form for
display.
// programs DPR file
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.RES}
begin
Application.Initialize;
StartJpegLoad;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
// Unit1
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
ExtCtrls, JPEG;
const
UM_JPEGLOADED = WM_USER + 666;
type
TForm1 = class(TForm)
Image1: TImage;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FStartTicks: Integer;
Procedure UMJpegLoaded( Var Msg: TMessage );
message UM_JPEGLOADED;
public
{ Public declarations }
end;
Procedure StartJpegLoad;
var
Form1: TForm1;
implementation
{$R *.DFM}
Type
TJpegThread = Class( TThread )
private
FBMP: TBitmap;
public
Constructor Create;
Procedure Execute; override;
end;
Procedure StartJpegLoad;
Begin
TJpegThread.Create;
End;
{ TJpegThread }
constructor TJpegThread.Create;
begin
inherited Create( true );
FBMP:= TBitmap.Create;
FreeOnTerminate := True;
Resume;
end;
procedure TJpegThread.Execute;
var
FJPeg: TJPegImage;
begin
FJpeg := TJPegImage.Create;
try
FJpeg.LoadFromFile('D:\Daten\pix\fraktal_2.jpg');
FBmp.Assign( FJpeg );
finally
FJpeg.Free;
end;
While not Assigned( Application.Mainform ) Do
Sleep( 100 );
PostMessage( Application.Mainform.Handle,
UM_JPEGLOADED, Integer(FBMP), 0 );
// Note: We do NOT free the FBMP, that is left to the
// mainform!
end;
{ TForm1 }
procedure TForm1.UMJpegLoaded(var Msg: TMessage);
var
bmp: TBitmap;
time1, time2: Integer;
begin
time1 := GetTickCount;
bmp := TBitmap(msg.wparam);
image1.picture.bitmap.assign( bmp );
bmp.free;
time2:= GetTickCount;
ShowMessage( Format('Load delay: %d, show delay %d',
[time1-Fstartticks, time2-time1]));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FStartTicks:= GetTickcount;
Sleep( 1000 ); // fake slow form load
end;
end.
Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!