如何加载JPEG图像文件?

时间:2014-03-30 15:00:53

标签: delphi jpeg delphi-xe2

此代码:

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')而不出错?

1 个答案:

答案 0 :(得分:4)

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关联)。

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