1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
unit URedimThread;
interface
uses
SysUtils, Classes, Graphics, ImageUtils;
type
TRedimMethod = (rmMaxSize, rmFixedSize);
TRedimProgressEvent = procedure (ActualFile, NumFiles: Integer) of object;
TRedimThread = class(TThread)
private
FWidth: Integer;
FHeight: Integer;
FQuality: Integer;
FMethod: TRedimMethod;
FBackColor: TColor;
FSourceFiles: TStringList;
FActualFile: Integer;
FDestDir: string;
FOnProgress: TRedimProgressEvent;
protected
procedure Execute; override;
procedure DoProgress;
public
constructor Create;
destructor Destroy; override;
property SourceFiles: TStringList read FSourceFiles;
property DestDir: string read FDestDir write FDestDir;
property Width: Integer read FWidth write FWidth;
property Height: Integer read FHeight write FHeight;
property Quality: Integer read FQuality write FQuality;
property Method: TRedimMethod read FMethod write FMethod;
property BackgroundColor: TColor read FBackColor write FBackColor;
property OnProgress: TRedimProgressEvent read FOnProgress write FOnProgress;
end;
implementation
constructor TRedimThread.Create;
begin
inherited Create(True);
FWidth := 100;
FHeight := 100;
FQuality := 90;
FMethod := rmFixedSize;
FBackColor := clWhite;
FSourceFiles := TStringList.Create;
end;
destructor TRedimThread.Destroy;
begin
FSourceFiles.Free;
inherited Destroy;
end;
procedure TRedimThread.Execute;
var
SourceBmp, DestBmp: TBitmap;
W, H: Integer;
DestFileName: string;
begin
FActualFile := 0;
FDestDir := IncludeTrailingPathDelimiter(FDestDir);
while not Terminated and (FActualFile < FSourceFiles.Count) do
begin
{>> Trouve le nom de fichier de destination }
DestFileName := FDestDir + ExtractFileName(FSourceFiles[FActualFile]);
DestFileName := ChangeFileExt(DestFileName, '.jpg');
{>> Traitement suivant la mthode }
case FMethod of
rmMaxSize:
begin
SourceBmp := TBitmap.Create;
try
LoadImage(FSourceFiles[FActualFile], SourceBmp);
W := FWidth;
H := FHeight;
CalcSize(SourceBmp.Width, SourceBmp.Height, W, H);
DestBmp := CreateBmp(W, H);
try
StretchGraphic(SourceBmp, SourceBmp.Canvas.ClipRect,
DestBmp, DestBmp.Canvas.ClipRect);
SaveBitmapAsJpeg(DestFileName, DestBmp, FQuality);
finally
DestBmp.Free;
end;
finally
SourceBmp.Free;
end;
end;
rmFixedSize:
begin
DestBmp := CreateBmp(FWidth, FHeight);
try
LoadImage(FSourceFiles[FActualFile], DestBmp, FBackColor);
SaveBitmapAsJpeg(DestFileName, DestBmp, FQuality);
finally
DestBmp.Free;
end;
end;
end;
{>> Evenement }
DoProgress;
Inc(FActualFile);
end;
end;
procedure TRedimThread.DoProgress;
begin
if Assigned(FOnProgress) then
FOnProgress(FActualFile, FSourceFiles.Count);
end;
end. |
Partager