简体   繁体   English

如何将 Exif 功能添加到 TBitmap?

[英]How to add Exif functionality to TBitmap?

I want to create a general purpose function that loads 'any' type of image (gif, jpg, bmp, png, etc) from disk and returns a bitmap.我想创建一个通用函数,从磁盘加载“任何”类型的图像(gif、jpg、bmp、png 等)并返回位图。

function LoadGraph(FileName: string): TBitmap;     {pseudocode}
begin
   if FileIsJpeg then 
     jpeg.LoadFromFile; 
     Bitmap.Exif.Assign(Jpeg.Exif);
end; 

The thing is that I need to have access to the Exif data when the input type is Jpeg.问题是当输入类型为 Jpeg 时,我需要访问 Exif 数据。 So, I wanted to create a class helper like this:所以,我想创建一个这样的类助手:

TYPE
  TBitmapHelper = class helper for TBitmap
    public
      FExifData: TExif;
  end;

However, it seems that the Delphi compiler does not have this capability (yet?) as I get this compile error:但是,Delphi 编译器似乎没有此功能(还没有?),因为我收到此编译错误:

E2599 Field definition not allowed in helper type E2599 辅助类型中不允许字段定义

How to achieve this?如何实现这一目标?

There is quite complex hierarchy of graphic objects in Delphi intended to work with various image formats: TGraphic is abstract class for some image you can load from file, save to file and draw on canvas. Delphi 中有相当复杂的图形对象层次结构,旨在处理各种图像格式: TGraphic是某些图像的抽象类,您可以从文件加载、保存到文件并在画布上绘制。 TPicture is container for TGraphic which allows you to write just one line of code: TPictureTGraphic容器,它允许您只编写一行代码:

Picture.LoadFromFile(Filename);

and it will look up correct graphic class, create it and load your image.它将查找正确的图形类,创建它并加载您的图像。

So one of solutions for you would be to use TPicture instead of TBitmap.因此,您的解决方案之一是使用 TPicture 而不是 TBitmap。 For TBitmap and its descendants TPicture will contain Bitmap property, while for the others you can draw on Canvas or assign Picture.Graphic to your TBitmap (works for TJPEGImage, TPNGImage, but still fails with TIcon).对于 TBitmap 及其后代,TPicture 将包含 Bitmap 属性,而对于其他属性,您可以在 Canvas 上绘制或将 Picture.Graphic 分配给您的 TBitmap(适用于 TJPEGImage、TPNGImage,但仍然无法使用 TIcon)。

If it's too bulky, for example, you need not just show image on screen but modify it in some way and not think each time how it's actually represented, I'd recommend to create descendant from TBitmap, not just helper, it's quite possible, you can pass such an object everywhere in program where TBitmap is expected, but now you can extend it with new fields.如果它太笨重,例如,您不仅需要在屏幕上显示图像,还需要以某种方式对其进行修改,而不是每次都考虑它的实际表示方式,我建议从 TBitmap 创建后代,而不仅仅是助手,这是很有可能的,您可以在程序中需要 TBitmap 的任何地方传递这样的对象,但现在您可以使用新字段扩展它。 I'm going to do similar thing to add ICC profiles capability (supported in BitmapHeaderV5).我将做类似的事情来添加 ICC 配置文件功能(在 BitmapHeaderV5 中支持)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM