unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ExtDlgs, ExtCtrls, BGRABitmap, BGRABitmapTypes;
type
{ TForm1 }
TForm1 = class(TForm)
btnImage2: TButton;
btnImage1: TButton;
OpenPictureDialog1: TOpenPictureDialog;
Timer1: TTimer;
procedure btnImage1Click(Sender: TObject);
procedure btnImage2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
Image1: TBGRABitmap;
Image2: TBGRABitmap;
Combined: TBGRABitmap;
ImageLoaded: Boolean;
end;
const
TransitionDelayTime = 100; // should be 5 .. 500
StillDelayTime = 200; // should be 0 .. 1000
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.btnImage1Click(Sender: TObject);
begin
if not(OpenPictureDialog1.Execute) then Exit;
Image1 := TBGRABitmap.Create(OpenPictureDialog1.FileName);
btnImage1.Enabled := False;
btnImage2.Enabled := True;
end;
procedure TForm1.btnImage2Click(Sender: TObject);
begin
if not(OpenPictureDialog1.Execute) then Exit;
Image2 := TBGRABitmap.Create(OpenPictureDialog1.FileName);
Combined := TBGRABitmap.Create(Width, Height);
ImageLoaded := True;
Timer1.Enabled := True;
btnImage1.Visible := False;
btnImage2.Visible := False;
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
Timer1.Enabled := False;
Image1.Free;
Image2.Free;
Combined.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
btnImage2.Enabled := False;
ImageLoaded := False;
Timer1.Enabled := False;
Timer1.Interval := 20;
end;
procedure TForm1.FormPaint(Sender: TObject);
const
StillDelay : Integer = 0;
Transition : Integer = 0;
Direction : (From1To2, From2To1) = From1To2;
var
Transparency : Integer;
AspectForm : Single;
AspectImage1 : Single;
AspectImage2 : Single;
Image1NewWidth : Integer;
Image1NewHeight : Integer;
Image2NewWidth : Integer;
Image2NewHeight : Integer;
RectImage1 : TRect;
RectImage2 : TRect;
begin
// Don't start before all image are loaded
if not(ImageLoaded) then Exit;
// Process direction changing
if Transition >= TransitionDelayTime then
if StillDelay <= 0 then
begin
StillDelay := StillDelayTime;
Transition := 0;
Inc(Direction);
if Direction > High(Direction) then
Direction := Low(Direction);
end;
// Calculations for proportional scaling
AspectForm := Width / Height;
AspectImage1 := Image1.Width / Image1.Height;
AspectImage2 := Image2.Width / Image2.Height;
if AspectForm > AspectImage1 then // use same height
begin
Image1NewHeight := Height;
Image1NewWidth := Round(Image1NewHeight * AspectImage1);
end
else begin // use same width
Image1NewWidth := Width;
Image1NewHeight := Round(Image1NewWidth / AspectImage1);
end;
if AspectForm > AspectImage2 then // use same height
begin
Image2NewHeight := Height;
Image2NewWidth := Round(Image2NewHeight * AspectImage2);
end
else begin // use same width
Image2NewWidth := Width;
Image2NewHeight := Round(Image2NewWidth / AspectImage2);
end;
RectImage1.Left := (Width-Image1NewWidth) div 2; // Make the image center
RectImage1.Top := (Height-Image1NewHeight) div 2; // Make the image center
RectImage1.Width := Image1NewWidth;
RectImage1.Height := Image1NewHeight;
RectImage2.Left := (Width-Image2NewWidth) div 2; // Make the image center
RectImage2.Top := (Height-Image2NewHeight) div 2; // Make the image center
RectImage2.Width := Image2NewWidth;
RectImage2.Height := Image2NewHeight;
// Process fading and show the result
Transparency := Round(Transition / TransitionDelayTime * 255);
case Direction of
From1To2:
begin
Combined.StretchPutImage(RectImage1, Image1, dmDrawWithTransparency, 255);
Combined.StretchPutImage(RectImage2, Image2, dmDrawWithTransparency, Transparency);
end;
From2To1:
begin
Combined.StretchPutImage(RectImage2, Image2, dmDrawWithTransparency, 255);
Combined.StretchPutImage(RectImage1, Image1, dmDrawWithTransparency, Transparency);
end;
end;
Combined.Draw(Canvas, 0,0);
// Process delays
if StillDelay <= 0 then
Inc(Transition)
else
Dec(StillDelay);
end;
procedure TForm1.FormResize(Sender: TObject);
begin
if not(ImageLoaded) then Exit;
Combined.Free;
Combined := TBGRABitmap.Create(Width, Height);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
FormPaint(Self);
end;
end.