0

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?

8
  • 4
    Because you don't set the size of the multiPageBitmap before you start to load the images into it. E.g. multiPageBitmap.SetSize(w, n * h) Commented Aug 2 at 21:33
  • Note that multipage TIFF is just each single page file's data appended to each other. There is no additional file/page structure. Even a binary copy on the command line can do it, copy/b file1.tif+file2.tif+file3.tif combined_multi_page.tif
    – Brian
    Commented Aug 3 at 14:35
  • 2
    VCL's TBitmap does not support TIFF images. You are actually creating a BMP image with a .tif file extension. BMP and TIFF are very different formats Commented Aug 3 at 20:06
  • And do you want to create a real multipage tiff (with multiple pages) or a tiff with all your pages/images on one very long page (all above eachother)? Because your code would do the latter (if tbitmap would have supported tiff). It would not create a real true multipage tiff.
    – Rik
    Commented Aug 4 at 7:28
  • @Remy Lebeau - Is there another VCL component that does support TIFF images?
    – Hackbrew
    Commented Aug 4 at 20:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.