如何加载 JPE 图像文件?

use*_*348 3 delphi jpeg delphi-xe2

这段代码:

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

生成此错误:

EInvalidGraphic: Unknown picture file extension <.jpe>
Run Code Online (Sandbox Code Playgroud)

虽然Vcl.Imaging.jpeg使用。JPG 和 JPEG 可以毫无问题地加载。

维基百科解释说,.jpg、.jpeg、.jpe、.jif、.jfif、.jfi是 JPEG 格式的扩展。

那么如何使用LoadFromFile('MyImage.JPE')而不会出错呢?

Dav*_*nan 6

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

Image := TJPEGImage.Create;
Image.LoadFromFile(...);
Run Code Online (Sandbox Code Playgroud)

并分配给图片控件。

 ThisPicture.Assign(Image);
Run Code Online (Sandbox Code Playgroud)

或者注册 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);
Run Code Online (Sandbox Code Playgroud)

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

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

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