Hackbrew
Hackbrew

Reputation: 513

Convert multiple single page TIFF files into one multi-page TIFF using Delphi VCL

In a Delphi (11.3) VCL program, I'm trying to create a multi-page TIFF file from individual TIFF pages. At first, I was able to successfully combine all the individual TIFF's into one TIFF, but the pages just overlayed each other, and it was only 1 page. I want to output 1 TIFF with more than one page. So the output file path is valid and writable and the file is not locked by another user. Here's the code:

procedure TiffsToMultiPage(const fileNames: array of string; const outputFileName: string);
var
  multiPageBitmap: TBitmap;
  wicImage: TWICImage;
  i: Integer;
begin
  // Initialize the bitmap
  multiPageBitmap := TBitmap.Create;
  try
    // Load and combine the images
    for i := 0 to High(fileNames) do
    begin
      wicImage := TWICImage.Create;
      try
        wicImage.LoadFromFile(fileNames[i]);
        multiPageBitmap.Canvas.Draw(0, i * wicImage.Height, wicImage);
      finally
        wicImage.Free;
      end;
    end;
    // Output the multi-page TIFF
    multiPageBitmap.SaveToFile(outputFileName);
  finally
    multiPageBitmap.Free;
  end;
end;

When I click the Enter button one TIFF file gets output, but the file is empty (zero bytes).

procedure Tfrm_Main.btn_EnterClick(Sender: TObject);
begin
  // Combine the TIFF's
  TiffsToMultiPage(['file1.tif', 'file2.tif', 'file3.tif'], 'C:\Documents\Test\combined_multi_page.tif');
end;

I have verified file paths, inspected variables, debugged to make sure that each image gets loaded, and added a breakpoint to step through the code to identify any issues. Why is this code outputting a zero-byte file?

Upvotes: 0

Views: 178

Answers (0)

Related Questions