如何在TImage控件中加载和显示tiff图像? [英] How to load and display tiff images in TImage control?

查看:397
本文介绍了如何在TImage控件中加载和显示tiff图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Delphi XE2试用版。我想在TImage控件中加载和显示TIFF图像,而不使用任何第三方组件/库。



我尝试下面的代码,但不是为我而起作用。 >

 过程TForm1.Button1Click(Sender:TObject); 
Var
OleGraphic:TOleGraphic;
fs:TFileStream;
资料来源:TImage;
BMP:TBitmap;
开始
尝试
OleGraphic:= TOleGraphic.Create;

fs:= TFileStream.Create('c:\testtiff.dat',fmOpenRead或fmSharedenyNone);
OleGraphic.LoadFromStream(fs);

资料来源:= 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;
最后
fs.Free;
OleGraphic.Free;
Source.Free;
bmp.Free;
结束;
结束;

请咨询。

解决方案正如我在评论中所说,如果文件扩展名是标准的tiff扩展名,则打开文件的代码是微不足道的:

  image1.Picture.LoadFromFile(MyTiffFile); 

如果没有,请按照dwrbudr的答案。



这是一个例子:

  procedure LoadBitmapFromFile(aImage:TImage; tiffFilename:String); 
var
tiffIm:TWICImage;
ext:String;
begin
ext:= SysUtils.ExtractFileExt(tiffFilename);
if(ext ='.tif')或(ext ='.tiff')
then aImage.Picture.LoadFromFile(tiffFilename)
else begin
tiffIm = = TWICImage。创建;
try
tiffIm.LoadFromFile(tiffFilename);
aImage.Picture.Bitmap.Assign(tiffIm);
finally
tiffIm.Free;
结束
结束
结束

另请参见 TWICImage ,适用于XP SP3及以上。


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.

解决方案

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.

这篇关于如何在TImage控件中加载和显示tiff图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆