Reputation: 639
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.
Upvotes: 4
Views: 17355
Reputation: 88
Also, if TIF contains multiple images, you can specify the image you want to load using the FrameIndex
property before loading the image in TWicImage
With FrameCount
you get the number of available images in the TIF file
FrameIndex
range is 0 to FrameCount
-1
Upvotes: 0
Reputation: 21252
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.
Upvotes: 5
Reputation: 34949
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.
Upvotes: 3
Reputation: 61
tiff := TWICImage.Create;
tiff.LoadFromFile(Filename);
ABitmap.Assign(tiff);
Upvotes: 6