-
Notifications
You must be signed in to change notification settings - Fork 4
MultiPageImage Helper Methods to Add Multi Page support to TImage Component
This unit, MultiPageImage.pas, supports multipage TIF in the standard VCL TImage component. You simply add the unit to the Form in the same way you would for JPEG (Jpeg), GIF (GIFImg) and PNG (PngImage) support.
The Unit has helper methods for TImage and TPicture that provides TIF multi page support including:
- PageCount (read only)
- PageNumber (read or set)
- LoadFromFile(Filename, PageNo)
- LoadFromStream(Stream, PageNo)
Only VCL is supported (Cross platform support is not available).
WARNING! SaveToFile still only has single file support as of September 2018.
Download the latest MultipageImage.pas from the repo or cut and paste it into a new unit called "MultiPageImage.pas" from here.
Once you have added the unit to your form unit, use it like this!

To support multipage tifs in your program, simply:
- Add a TImage to the form
- Add MultiPageImage to the implementation
usesclause.
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
procedure Image1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses MultiPageImage;
{$R *.dfm}
procedure TForm1.Image1Click(Sender: TObject);
var lPageNo: integer;
begin
lPageNo := 2;
Image1.Picture.LoadFromMultiPageFile('C:\temp\someMPTif.tif', lPageNo);
end;
end.
To see the page you are on, OR to change pages, use the TImage PageNumber property. To see the number of pages available use the TImage PageCount. These two properties have been added to TImage via a helper method. In TImage and TPicture, Page numbers are 1-based. Inside the TMPWICImage class however this images are 0-based as per the Interface specification.
The nature of how TImage internally deals with the image data means that by default, the whole file will be loaded into memory. Changing the page number on a loaded file however does not cause the stream or file to be re-read.
Calling LoadFromMultipageFile on a non-multipage image like BMP or PNG does not cause an exception because the page number is essentially ignored and 1 is always used. However, Requesting a non-existent page number from a multipage Tif image will raise an Invalid Image Exception.
Currently the SAVE function simply writes the internal data to file, so calling SaveToFile will simply duplicate the image file and ignore the currently displayed image.
A very simple method of saving the current page is to simply copy the canvas to another image and save the contents. This however uses the default TIF settings which produces rather large colour images without compression.
procedure TForm1.SaveSinglePage;
var
lSinglePage: TImage;
begin
lSinglePage := TImage.Create(nil);
try
lSinglePage.Width := Image1.Picture.Width;
lSinglePage.Height := Image1.Picture.Height;
lSinglePage.Canvas.Draw(0, 0, Image1.Picture.Graphic);
lSinglePage.Picture.SaveToFile(ChangeFileExt(OpenPictureDialog1.Filename,
'.' +IntTostr(Image1.PageNumber) +
ExtractFileExt(OpenPictureDialog1.Filename)));
finally
freeandnil(lSinglePage);
end;
end;
There is an example project called TestMultiPage in the Delphi Tips Demo Folder.
MultipageImage supports all versions of Delphi back to Delphi 2010. Delphi 2009 also works, but there are a number of minor issues and is dependent on a copy of Delphi 2010 Wincodec unit with some modification. So 2009 is not recommended. Delphi 2007 and prior are NOT supported.
This unit has has been tested on:
| Version | Version | Version | Version | Version | |
|---|---|---|---|---|---|
| 4 ☐ | 2005 ☐ | XE ☑ | XE6 ☐ | 10.0 Seattle | ☐ |
| 5 ☐ | 2006 ☐ | XE2 ☑ | XE7 ☐ | 10.1 Berlin | ☑ |
| 6 ☐ | 2007 ☒ | XE3 ☐ | XE8 ☐ | 10.2 Tokyo | ☑ |
| 7 ☐ | 2009 ☑ | XE4 ☐ | Delpi 2010 ☑ | ||
| 8 ☐ | 2010 ☑ | XE5 ☑ |
Sorry, there was an error rendering this page.