How to load and display tiff images in TImage control?

一曲冷凌霜 提交于 2019-12-31 00:39:29

问题


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.


回答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.




回答2:


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



回答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.



来源:https://stackoverflow.com/questions/7845359/how-to-load-and-display-tiff-images-in-timage-control

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