【问题标题】:How to load JPE image file?如何加载 JPE 图像文件?
【发布时间】:2014-05-09 20:16:05
【问题描述】:

这段代码:

uses
  Vcl.Imaging.jpeg...
...
ThisPicture := TPicture.Create;
try
  ThisPicture.LoadFromFile('MyImage.JPE'); // error
  ...
finally
  ThisPicture.Free;
end;

产生这个错误:

EInvalidGraphic: Unknown picture file extension <.jpe>

尽管使用了 Vcl.Imaging.jpeg。 JPG和JPEG可以毫无问题地加载。

Wikipedia 解释说 .jpg、.jpeg、.jpe .jif、.jfif、.jfi 是 JPEG 格式的扩展。

那么我怎样才能正确使用 LoadFromFile('MyImage.JPE') 呢?

【问题讨论】:

  • 在程序启动时调用TPicture.RegisterFileFormat('jpe', 'JPE Image File', TJPEGImage);
  • @TLama TLama 和 David Heffernan 的两个解决方案都可以工作,这要感谢他们!但是由于通常我更喜欢最简单的解决方案,因此我想使用您的解决方案,因此请将其添加为答案。

标签: delphi jpeg delphi-xe2


【解决方案1】:

Delphi JPEG 代码未注册 JPE 扩展。因此错误。由于您知道图像类型,因此您可以将其直接加载到 TJPEGImage 对象中:

Image := TJPEGImage.Create;
Image.LoadFromFile(...);

并分配给图片控件。

 ThisPicture.Assign(Image);

或者注册 JPE 扩展的更简单的解决方案,以便 TPicture 将其与 TJPEGImage 关联。这可以使用TPicture.RegisterFileFormat 来完成:

uses
  Vcl.Imaging.JConsts, Vcl.Imaging.jpeg;
....
TPicture.RegisterFileFormat('jpe', sJPEGImageFile, TJPEGImage);
TPicture.RegisterFileFormat('jif', sJPEGImageFile, TJPEGImage);
TPicture.RegisterFileFormat('jfif', sJPEGImageFile, TJPEGImage);
TPicture.RegisterFileFormat('jfi', sJPEGImageFile, TJPEGImage);

值得一提的是,RegisterFileFormat 的文档中包含以下相当古怪的一行:

AExtension 参数指定与图形类关联的三字符系统文件扩展名(例如,“bmp”与 TBitmap 关联)。

不必担心扩展名的长度必须恰好是三个字符的建议。这只是一个文档错误。

【讨论】:

  • 您不能将Assign()TJPEGImage 转换为TPicture.Graphic,除非它已经指向现有的TJPEGImage。最好将 Assign() 改为 TPicture 本身,以便它更新 Graphic 而不管其当前类型如何:ThisPicture.Assign(Image);
猜你喜欢
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
  • 2020-09-02
  • 2021-08-06
  • 1970-01-01
相关资源
最近更新 更多