No, it is working. Just a typo: use "Extra" instead of "Extras" - sorry. And it's even simpler. Since TBGRABitmap inherits from TFPCustomImage it already has the "Extra" property. So, all you have to do is to set the resolution in the BGRABitmap before writing, and the ORIGINAL tiff writer will take care of the rest - no need for a special adapted writer.
procedure TForm1.Button3Click(Sender: TObject);
var
bm: TBGRABitmap;
begin
bm := TBGRABitmap.Create(Image1.Picture.Bitmap);
try
bm.Extra['TiffXResolution'] := '300/1';
bm.Extra['TiffYResolution'] := '300/1';
bm.Extra['TiffResolutionUnit'] := '2';
bm.SaveToFile('TiffPicture.tif');
finally
bm.Free;
end;
end;
If the source bitmap is a standard TBitmap instead of a TBGRABitmap convert it to a LazIntfImage which inherits from TFPCustomBitmap again, and you can do the same:
uses
IntfGraphics;
...
var
intImg: TLazIntfImage;
...
intfimg := image1.Picture.Bitmap.CreateIntfImage;
try
intfimg.Extra['TiffXResolution'] := '600/1';
intfimg.Extra['TiffYResolution'] := '600/1';
intfimg.Extra['TiffResolutionUnit'] := '2';
intfimg.SaveToFile('TiffPicture1.tif');
finally
intfimg.Free;
end;
Unfortunately, it seems to me that TIFF seems to be the only file format which supports the built-in metadata "Extra". So, you still have to handle the jpeg and png cases separately. The jpeg writer has an internal field FInfo in which fields for x and y resolution are available, but FInfo is private and cannot be reached by a derived writer. And the original png writer does not write the PHYS chunk at all (which contains the dpi values) - but your solution correctly implements it. An idea for improvement: extract the dpi from the "Extra" of the FPCustomImage to avoid hard-coding the dpi in the writer.