2016-05-10 4 views
0

私はパスカル使用してデルファイへの.tif表示したいと私はすでにlibtiffのTIFFを表示するには?

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; 

を使用しています。しかし、なぜ画像が表示されませんか?誰にでも解決策がありますか?そして、私の悪い英語のために申し訳ありません。

+2

私がしようとし、ここで任意のコードが表示されません何かを表示する。 –

+2

ビットマップは単なる記憶域です。どこかにペイントしたり、イメージに割り当てたりしない限り、それ自体は表示されません。 – Johan

答えて

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

+0

関数は 'try/finally'に' TIFFClose() 'を入れ、例外が発生したために関数がnilを返すことができないので、関数終了時にnilのチェックを外す必要があります。 –

関連する問題