IceCold
IceCold

Reputation: 21231

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.

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. 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:

E2599 Field definition not allowed in helper type

How to achieve this?

Upvotes: 1

Views: 542

Answers (1)

Yuriy Afanasenkov
Yuriy Afanasenkov

Reputation: 1440

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. TPicture is container for TGraphic which allows you to write just one line of code:

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. 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).

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. I'm going to do similar thing to add ICC profiles capability (supported in BitmapHeaderV5).

Upvotes: 1

Related Questions