procedure TfrmMain.actGenerateContactSheetExecute(Sender: TObject);
var
frm : TfrmDLGContactSheet;
lst : TStringList;
a : Integer;
pgWidth, pgHeight, thWidth, thHeight, cellWidth, cellHeight : Integer;
pgCount, pgTotal, pgCellTotal : Integer;
bgraThumb, bgraPage, bgraCurrent : TBgraBitmap;
x, y : Integer;
sze : TSize;
txt : String;
ps, os, ns : TPoint;
rct : TRect;
intMargin, intColCount, intRowCount : Integer;
begin
frm := TfrmDLGContactSheet.create(self);
try
lst := TStringList.create;
try
GetCurrentList(lst);
if lst.count-1=-1 then lst.add(GetCurrentImageFilename);
try
for a := 0 to lst.count - 1 do frm.AddToFiles(lst[a]);
except
//
end;
finally
lst.free;
end;
// Show and action dialog
if frm.ShowModal = mrOK then
begin
// Go for it
pgWidth := frm.seSheetWidth.value;
pgHeight := frm.seSheetHeight.value;
thWidth := frm.seThumbWidth.value;
thHeight := frm.seThumbHeight.value;
cellWidth := thWidth + (frm.seThumbMargin.value * 2);
cellHeight := thHeight + (frm.seThumbMargin.value * 2);
pgCellTotal := ((pgWidth - frm.seSheet_Margin.value) div cellWidth) + ((pgHeight - frm.seSheet_Margin.value) div cellHeight);
pgTotal := ((frm.lvFiles.items.count - 1) div pgCellTotal);
intColCount := pgWidth;
intColCount := intColCount - (frm.seSheet_Margin.value);
intColCount := round(intColCount / cellWidth);
// if intColCount mod CellWidth>0 then intColCount := intColCount+ 1;
intRowCount := pgHeight;
intRowCount := intRowCount - (frm.seSheet_Margin.value * 2);
intRowCount := round(intRowCount / cellHeight);
intMargin := pgWidth;
intMargin := intMargin - (frm.seSheet_Margin.value * 2);
intMargin := intMargin - (cellWidth * intColCount);
if intMargin<frm.seSheet_Margin.value then intMargin := frm.seSheet_Margin.value;
// Duh! I cannot get the centering to work so for now
// intMargin := frm.seSheet_Margin.value;
pbar.maxValue := frm.lvFiles.items.count - 1;
// Start new page here
bgraPage := TBgraBitmap.create(pgWidth, pgHeight);
try
x := intMargin;
y := frm.seSheet_Margin.value;
pgCount := 0;
// Prepare First Page
// Draw BG
bgraPage.FillRect(0, 0, pgWidth, pgHeight, frm.cbSheetBackground.ButtonColor);
// Page Header
bgraPage.FontName := frm.cboFont.Text;
bgraPage.FontHeight := 12;
txt := frm.txtHeader.text;
txt := StringReplaceAll(txt, '{page}', IntToStr(pgCount+1));
txt := StringReplaceAll(txt, '{pageleadzero}', LeadZero(pgCount+1, false));
txt := StringReplaceAll(txt, '{pageleadhundred}', LeadZero(pgCount+1, true));
txt := StringReplaceAll(txt, '{pagetotal}', IntToStr(pgTotal));
txt := StringReplaceAll(txt, '{pagetotalleadzero}', LeadZero(pgTotal, false));
txt := StringReplaceAll(txt, '{pagetotalleadhundred}', LeadZero(pgTotal, true));
// Get Text Size
sze := bgraPage.TextSize(txt);
// Output Page Header (Centered)
if frm.chkShowHeader.checked then
bgraPage.TextOut((pgWidth div 2) - (sze.width div 2), 0, txt, frm.cbTextColor.ButtonColor);
for a := 0 to frm.lvFiles.items.count - 1 do
begin
pbar.value := a;
application.ProcessMessages;
bgraCurrent := TBgraBitmap.create;
try
if fileExists(frm.lvFiles.items.item[a].caption) then
begin
LoadToBgra(frm.lvFiles.items.item[a].caption, bgraCurrent);
BgraCurrent.ResampleFilter := rfBestQuality;
os.x := BgraCurrent.width;
os.y := BgraCurrent.Height;
ns.x := thWidth;
ns.y := thHeight;
ps := ProportionalSize(os, ns);
bgraThumb := TBgraBitmap.create;
try
// Make Thumb
BgraThumb := BgraCurrent.Resample(ps.x, ps.y, rmFineResample);
// Draw BG
bgraPage.FillRect((x), y, (x) + (cellWidth-1), y + (cellHeight-1), frm.cbThumbBackground.ButtonColor);
// Draw Thumb
bgraThumb.Draw(bgraPage.canvas, ((x) + ((cellWidth div 2) - (ps.x div 2))), y, false);
// Cell Text
txt := frm.memInformation.Lines.text;
txt := renameMacro(frm.lvFiles.items.Item[a].caption, txt, a);
txt := StringReplaceAll(txt, '{page}', IntToStr(pgCount+1));
txt := StringReplaceAll(txt, '{pageleadzero}', LeadZero(pgCount+1, false));
txt := StringReplaceAll(txt, '{pageleadhundred}', LeadZero(pgCount+1, true));
txt := StringReplaceAll(txt, '{pagetotal}', IntToStr(pgTotal));
txt := StringReplaceAll(txt, '{pagetotalleadzero}', LeadZero(pgTotal, false));
txt := StringReplaceAll(txt, '{pagetotalleadhundred}', LeadZero(pgTotal, true));
bgraPage.FontName := frm.cboFont.Text;
bgraPage.FontHeight := frm.seFontSize.value;
// Get Size
sze := bgraPage.TextSize(txt);
// Output Cell Text
if frm.chkShowInformation.checked then
begin
if (sze.Width>=cellWidth) or (sze.height>=cellHeight) then
begin
rct.top := y + thHeight;
rct.left := x;
rct.Bottom := (y + cellHeight);
rct.Right := x + cellWidth;
bgraPage.TextRect(rct,txt,taCenter,tlCenter,ColorToBgra(frm.cbTextColor.ButtonColor));
end else bgraPage.TextOut(x + ((cellWidth div 2) - (sze.width div 2)), (y + thHeight), txt, frm.cbTextColor.ButtonColor);
end;
// Increment
x := succ(x) + cellWidth;
if x>=((pgWidth - frm.seSheet_Margin.value) - thWidth) then
begin
// Increment Y
y := succ(y) + cellHeight;
// Reset X
x := intMargin;
if y>=((pgHeight - frm.seSheet_Margin.value) - thHeight) then
begin
// Reset Y Co-Ord
y := frm.seSheet_Margin.value;
// Footer
bgraPage.FontName := frm.cboFont.Text;
bgraPage.FontHeight := 12;
txt := frm.txtFooter.text;
txt := StringReplaceAll(txt, '{page}', IntToStr(pgCount+1));
txt := StringReplaceAll(txt, '{pageleadzero}', LeadZero(pgCount+1, false));
txt := StringReplaceAll(txt, '{pageleadhundred}', LeadZero(pgCount+1, true));
txt := StringReplaceAll(txt, '{pagetotal}', IntToStr(pgTotal));
txt := StringReplaceAll(txt, '{pagetotalleadzero}', LeadZero(pgTotal, false));
txt := StringReplaceAll(txt, '{pagetotalleadhundred}', LeadZero(pgTotal, true));
sze := bgraPage.TextSize(txt);
if frm.chkShowFooter.checked then
bgraPage.TextOut((pgWidth div 2) - (sze.width div 2), (pgHeight - sze.height), txt, BgraBlack);
// Filename
txt := ChangeFileExt(frm.txtFilename.text, '');
txt := StringReplaceAll(txt, '{page}', IntToStr(pgCount+1));
txt := StringReplaceAll(txt, '{pageleadzero}', LeadZero(pgCount+1, false));
txt := StringReplaceAll(txt, '{pageleadhundred}', LeadZero(pgCount+1, true));
txt := StringReplaceAll(txt, '{pagetotal}', IntToStr(pgTotal));
txt := StringReplaceAll(txt, '{pagetotalleadzero}', LeadZero(pgTotal, false));
txt := StringReplaceAll(txt, '{pagetotalleadhundred}', LeadZero(pgTotal, true));
txt := BackSlash(frm.deOutput.directory) + txt;
if frm.cboType.itemIndex = 0 then txt := txt + '.tif'
else if frm.cboType.itemIndex = 1 then txt := txt + '.png'
else if frm.cboType.itemIndex = 2 then txt := txt + '.jpg'
else if frm.cboType.itemIndex = 3 then txt := txt + '.bmp'
else if frm.cboType.itemIndex = 4 then txt := txt + '.webp'
else if frm.cboType.itemIndex = 5 then txt := txt + '.avif'
else txt := txt + '.png';
// Save File or add a page if TIF?
if fileExists(txt) then RecycleItem(txt, true, false, true);
application.ProcessMessages;
sleep(15);
application.ProcessMessages;
SaveBGRAToFile(txt, BgraPage);
// Start new Page
BgraPage.SetSize(0,0);
BgraPage.SetSize(pgWidth, pgHeight);
// Set background options
bgraPage.FillRect(0, 0, pgWidth, pgHeight, frm.cbSheetBackground.ButtonColor);
// Header
bgraPage.FontName := frm.cboFont.Text;
txt := frm.txtHeader.text;
txt := StringReplaceAll(txt, '{page}', IntToStr(pgCount+1));
txt := StringReplaceAll(txt, '{pageleadzero}', LeadZero(pgCount+1, false));
txt := StringReplaceAll(txt, '{pageleadhundred}', LeadZero(pgCount+1, true));
txt := StringReplaceAll(txt, '{pagetotal}', IntToStr(pgTotal));
txt := StringReplaceAll(txt, '{pagetotalleadzero}', LeadZero(pgTotal, false));
txt := StringReplaceAll(txt, '{pagetotalleadhundred}', LeadZero(pgTotal, true));
sze := bgraPage.TextSize(txt);
if frm.chkShowHeader.checked then
bgraPage.TextOut((pgWidth div 2) - (sze.width div 2), 0, txt, frm.cbTextColor.ButtonColor);
// Increment page count
pgCount := succ(pgCount);
end;
end;
finally
bgraThumb.free;
end;
end;
finally
bgraCurrent.free;
end;
end;
// Footer
bgraPage.FontName := frm.cboFont.Text;
txt := frm.txtFooter.text;
txt := StringReplaceAll(txt, '{page}', IntToStr(pgCount+1));
txt := StringReplaceAll(txt, '{pageleadzero}', LeadZero(pgCount+1, false));
txt := StringReplaceAll(txt, '{pageleadhundred}', LeadZero(pgCount+1, true));
txt := StringReplaceAll(txt, '{pagetotal}', IntToStr(pgTotal));
txt := StringReplaceAll(txt, '{pagetotalleadzero}', LeadZero(pgTotal, false));
txt := StringReplaceAll(txt, '{pagetotalleadhundred}', LeadZero(pgTotal, true));
sze := bgraPage.TextSize(txt);
if frm.chkShowFooter.checked then
bgraPage.TextOut((pgWidth div 2) - (sze.width div 2), (pgHeight - sze.height), txt, BgraBlack);
// Save Last Page
txt := ChangeFileExt(frm.txtFilename.text, '');
txt := StringReplaceAll(txt, '{page}', IntToStr(pgCount+1));
txt := StringReplaceAll(txt, '{pageleadzero}', LeadZero(pgCount+1, false));
txt := StringReplaceAll(txt, '{pageleadhundred}', LeadZero(pgCount+1, true));
txt := StringReplaceAll(txt, '{pagetotal}', IntToStr(pgTotal));
txt := StringReplaceAll(txt, '{pagetotalleadzero}', LeadZero(pgTotal, false));
txt := StringReplaceAll(txt, '{pagetotalleadhundred}', LeadZero(pgTotal, true));
txt := BackSlash(frm.deOutput.directory) + txt;
if frm.cboType.itemIndex = 0 then txt := txt + '.tif'
else if frm.cboType.itemIndex = 1 then txt := txt + '.png'
else if frm.cboType.itemIndex = 2 then txt := txt + '.jpg'
else if frm.cboType.itemIndex = 3 then txt := txt + '.bmp'
else if frm.cboType.itemIndex = 4 then txt := txt + '.webp'
else if frm.cboType.itemIndex = 5 then txt := txt + '.avif'
else txt := txt + '.png';
SaveBGRAToFile(txt, BgraPage);
finally
bgraPage.free;
end;
try
if ask('Open output in directory browser?')=mrYes then
LoadAnyFile(BackSlash(frm.deOutput.directory));
except
//
end;
end;
finally
frm.free;
end;
end;