此代码:
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')而不出错?
答案 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关联)。
不要担心扩展名必须长度为三个字符。这只是一个文档错误。