4

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.

Dev
  • 629
  • 2
  • 9
  • 23
  • 2
    If the file extension was a standard tiff extension, this would be a one-liner, image1.Picture.LoadFromFile(MyTiffFile). Otherwise, this SO question may help you [how-to-load-an-arbitrary-image-from-a-blob-stream-into-a-timage](http://stackoverflow.com/questions/6251504/how-to-load-an-arbitrary-image-from-a-blob-stream-into-a-timage) – LU RD Oct 21 '11 at 06:12
  • I wonder if this code came from the sample in http://www.swissdelphicenter.ch/en/showcode.php?id=2012 (dated 2004). I just found effectively the same code in a rarely explored part of the codebase I work on. – Gerry Coll Aug 31 '16 at 04:10

3 Answers3

6
     tiff := TWICImage.Create;
     tiff.LoadFromFile(Filename);
     ABitmap.Assign(tiff);
dwrbudr
  • 61
  • 1
5

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.

Community
  • 1
  • 1
kobik
  • 21,001
  • 4
  • 61
  • 121
3

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.

LU RD
  • 34,438
  • 5
  • 88
  • 296