TImage 可以放入 .bmp  .jpg .jpeg .emf 等文件类型, 
现在想把TImage里面的图片保存出来,因为保存的时候不知道它里面存的是什么格式的图片,
直接用 cxImage2.Picture.SaveToFile('F:\Temp\aa.jpg'); 保存的时候是可以保存出来的,也能在windows 里面正常显示,
可是再把文件放入到Timage里面的时候会出错  cxImage2.Picture.LoadFromFile('F:\Temp\aa.jpg')。
主要应该是扩展名不对,所以LoadFromFile的时候,要是是.bmp或.emf的文件保存为 .jpg的就会出错。我想在保存的时候判断文件的类型再加上相应的扩展名。问题:怎么判段里面的文件类型并保存。我是这样做的可是保存出来是空的文件。procedure TForm8.Button2Click(Sender: TObject);
begin
  if not cxImage1.Picture.Bitmap.Empty then
        cxImage1.Picture.SaveToFile('F:\Temp\aa.bmp');  if not cxImage1.Picture.Graphic.Empty then
        cxImage1.Picture.SaveToFile('F:\Temp\aa.jpg');  if not cxImage1.Picture.Metafile.Empty then
        cxImage1.Picture.SaveToFile('F:\Temp\aa.emf');
end;
好像是因为 cxImage1.Picture.Bitmap.Empty 这个代码有问题, 执这个代码后Timage里面的图片就清空了。我用如下有判段语句也是一样的问题(Timage里面有图片被清空了,保存出来是空的文件) if cxImage1.Picture.Bitmap <> nil then
        cxImage1.Picture.SaveToFile('F:\Temp\aa.bmp');
请问能不能这样判断?  或者有没有其他好的办法?(有没有可能把所有的图片都转换成jpg保存出来)

解决方案 »

  1.   

    是不是我的 Delphi 出了问题啊? 执行上面有代码不应该把图片清空掉啊? 哪位高手知不知道怎么弄?
      

  2.   

    可以这样:
      if Image1.Picture.Graphic is TBitMap then
        showmessage('is bmp')
      else if Image1.Picture.Graphic is TJPEGImage then
        showmessage('is jpg');
      

  3.   

    如果delphi把graphics.pas里面这段放到interface里面,就好办了,很多人提,但一直到delphi2010也没有。type
      PFileFormat = ^TFileFormat;
      TFileFormat = record
        GraphicClass: TGraphicClass;
        Extension: string;
        Description: string;
        DescResID: Integer;
      end;  TFileFormatsList = class(TList)
      public
        constructor Create;
        destructor Destroy; override;
        procedure Add(const Ext, Desc: String; DescID: Integer; AClass: TGraphicClass);
        function FindExt(Ext: string): TGraphicClass;
        function FindClassName(const Classname: string): TGraphicClass;
        procedure Remove(AClass: TGraphicClass);
        procedure BuildFilterStrings(GraphicClass: TGraphicClass;
          var Descriptions, Filters: string);
      end;
      

  4.   

    http://qc.embarcadero.com/wc/qcmain.aspx?d=71373
    去顶顶
      

  5.   

    http://qc.embarcadero.com/wc/qcmain.aspx?d=71373
    经过一通抱怨,codegear的gc终于接受了这个建议,把状态修改为了Opended.