How to load and display tiff images in TImage control?

半城伤御伤魂 提交于 2019-12-01 19:13:58

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!