How to load and display tiff images in TImage control?

15,349

Solution 1

As said in my comment, if the file extension is the standard tiff extension the code to open the file is trivial :

image1.Picture.LoadFromFile(MyTiffFile);

If not, follow the answer from dwrbudr.

Here is an example :

procedure LoadBitmapFromFile( aImage : TImage; tiffFilename : String);
var
  tiffIm : TWICImage;
  ext : String;
begin
  ext := SysUtils.ExtractFileExt(tiffFilename);
  if (ext = '.tif') or (ext = '.tiff')
    then aImage.Picture.LoadFromFile(tiffFilename)
    else begin
      tiffIm:= TWICImage.Create;
      try
        tiffIm.LoadFromFile(tiffFilename);
        aImage.Picture.Bitmap.Assign(tiffIm);
      finally
        tiffIm.Free;
      end;
    end;
end;

See also TWICImage, which works for XP SP3 and up.

Solution 2

     tiff := TWICImage.Create;
     tiff.LoadFromFile(Filename);
     ABitmap.Assign(tiff);

Solution 3

You can use GDI+:

uses ..., ActiveX, GDIPAPI, GDIPOBJ, GDIPUTIL;

function LoadImageFromFile(const FileName: string; Bmp: TBitmap): Boolean;
var
  GPImage: TGPImage;
  encoderClsid: TGUID;
  MemStream: TMemoryStream;
begin
  Result := False;
  GPImage := TGPImage.Create(FileName);
  try
    if GPImage.GetLastStatus = Ok then
    begin
      MemStream := TMemoryStream.Create;
      try
        GetEncoderClsid('image/bmp', encoderClsid);
        if GPImage.Save(TStreamAdapter.Create(MemStream), encoderClsid) = Ok then
        begin
          MemStream.Position := 0;
          Bmp.LoadFromStream(MemStream);
          Result := True;
        end;
      finally
        MemStream.Free;
      end;
    end;
  finally
    GPImage.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  LoadImageFromFile('D:\ML_10222.tif', Image1.Picture.Bitmap);
end;

I also want to mention Synopse TSynPicture (GDI+ wrapper): https://stackoverflow.com/a/6251810/937125


EDIT: GDI+ TGPImage also supports multiple tiff frames/pages:

To get the frames count use:

GPImage.GetFrameCount(GDIPAPI.FrameDimensionPage);

To set the active frame use:

GPImage.SelectActiveFrame(GDIPAPI.FrameDimensionPage, Index);

Note that TSynPicture also supports multiple frames.

Share:
15,349
Dev
Author by

Dev

Updated on June 06, 2022

Comments

  • Dev
    Dev almost 2 years

    I am currently working on Delphi XE2 trial version. I want to load and display TIFF images in TImage control without using any third party component/library.

    I tried below code but it is not woking for me.

    Procedure TForm1.Button1Click(Sender: TObject); 
    Var 
         OleGraphic               : TOleGraphic; 
         fs                       : TFileStream; 
         Source                   : TImage; 
         BMP                      : TBitmap; 
    Begin 
         Try 
              OleGraphic := TOleGraphic.Create; 
    
              fs := TFileStream.Create('c:\testtiff.dat', fmOpenRead Or fmSharedenyNone); 
              OleGraphic.LoadFromStream(fs); 
    
              Source := Timage.Create(Nil); 
              Source.Picture.Assign(OleGraphic); 
    
              BMP := TBitmap.Create; 
              bmp.Width := Source.Picture.Width; 
              bmp.Height := source.Picture.Height; 
              bmp.Canvas.Draw(0, 0, source.Picture.Graphic); 
    
              image1.Picture.Bitmap := bmp;
         Finally 
              fs.Free; 
              OleGraphic.Free; 
              Source.Free; 
              bmp.Free; 
         End; 
    End;
    

    Please advice.