Re:Canceling a JPEG image during loading
William,
Quote
> DUring loading of a JPEG image i use the on progress to provide feedback
> and application.processmessages to give the user some control over the
> application. However i want that control to include cancelling the
> loading of the current image, is that possible?
After some trial and error, I was able to get a handle on this one. Here
is an example:
===================
unit TestCalcelLoadu;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
ComCtrls, ExtCtrls, StdCtrls, JPEG;
type
ELoadCanceled = Class(Exception);
TForm1 = class(TForm)
Image1: TImage;
ProgressBar1: TProgressBar;
Button1: TButton;
Button2: TButton;
OpenDialog1: TOpenDialog;
procedure Image1Progress(Sender: TObject; Stage: TProgressStage;
PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
const Msg: String);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
bCancel : Boolean;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Image1Progress(Sender: TObject; Stage: TProgressStage;
PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
const Msg: String);
begin
Application.ProcessMessages;
if bCancel then begin
bCancel := False; // This is important! You get an AV without it.
Abort; // Raise silent exception
end
else
ProgressBar1.Position := Integer(PercentDone);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then begin
try
Image1.Picture.LoadFromFile(OpenDialog1.Filename);
Except //wrap up
on E: ELoadCanceled do begin
showmessage(E.Message);
bCancel := False;
end;
end; //try / finally
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
bCancel := True;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
bCancel := False;
end;
end.
===================