如何显示TIFF?

时间:2016-05-10 02:09:15

标签: delphi delphi-7

我想使用pascal将.tif显示到delphi中,我已经使用了LibTiff

var
  OpenTiff: PTIFF;
  FirstPageWidth,FirstPageHeight: Cardinal;
  FirstPageBitmap: TBitmap;
begin
  OpenTiff:=TIFFOpen('C:\World.tif','r');
  TIFFGetField(OpenTiff,TIFFTAG_IMAGEWIDTH,@FirstPageWidth);
  TIFFGetField(OpenTiff,TIFFTAG_IMAGELENGTH,@FirstPageHeight);
  FirstPageBitmap:=TBitmap.Create;
  FirstPageBitmap.PixelFormat:=pf32bit;
  FirstPageBitmap.Width:=FirstPageWidth;
  FirstPageBitmap.Height:=FirstPageHeight;
  TIFFReadRGBAImage(OpenTiff,FirstPageWidth,FirstPageHeight,
               FirstPageBitmap.Scanline[FirstPageHeight-1],0);
  TIFFClose(OpenTiff);
  TIFFReadRGBAImageSwapRB(FirstPageWidth,FirstPageheight,
               FirstPageBitmap.Scanline[FirstPageHeight-1]);

end;

但为什么图像不显示?谁有解决方案?抱歉我的英语不好。

1 个答案:

答案 0 :(得分:1)

使用下面的代码将tiff转换为位图 然后像往常一样显示位图 这是您正在使用的完整功能(有一些改动)。

function ReadTiffIntoBitmap(const Filename: string): TBitmap;
var
  OpenTiff: PTIFF;
  FirstPageWidth, FirstPageHeight: Cardinal;
begin
  Result:= nil;  //in case you want to tweak code to not raise exceptions.
  OpenTiff:= TIFFOpen(Filename,'r');
  if OpenTiff = nil then raise Exception.Create(
           'Unable to open file '''+Filename+'''');
  try 
    TIFFGetField(OpenTiff, TIFFTAG_IMAGEWIDTH, @FirstPageWidth);
    TIFFGetField(OpenTiff, TIFFTAG_IMAGELENGTH, @FirstPageHeight);
    Result:= TBitmap.Create;
    try 
      Result.PixelFormat:= pf32bit;
      Result.Width:= FirstPageWidth;
      Result.Height:= FirstPageHeight;
    except
      FreeAndNil(Result);
      raise Exception.Create('Unable to create TBitmap buffer');
    end;
    TIFFReadRGBAImage(OpenTiff, FirstPageWidth, FirstPageHeight,
                 Result.Scanline[FirstPageHeight-1],0);
    TIFFReadRGBAImageSwapRB(FirstPageWidth, FirstPageheight,
                 Result.Scanline[FirstPageHeight-1]);
  finally
    TIFFClose(OpenTiff);
  end;
end;

现在在以下背景下使用它:
在表格上放一个按钮和一个图像 双击按钮并填充按钮的OnClick处理程序,如下所示:

Form1.Button1Click(sender: TObject);
var
  MyBitmap: TBitmap;
begin
  MyBitmap:= ReadTiffIntoBitmap('c:\test.tiff');
  try
    //uncomment if..then if ReadTiffIntoBitmap does not raise exceptions
    //but returns nil on error instead.
    {if Assigned(MyBitmap) then} Image1.Picture.Assign(MyBitmap);
  finally
    MyBitmap.Free;
  end;
end;

我找到了完整版的代码段:http://www.asmail.be/msg0055571626.html