I think the βdumbβ conversion would probably be so simple:
type TGraphicTypeEnum = (gteBMP, gteJPG, gteTIF, gtePNG); procedure SaveGraphicAs(AGraphic : TGraphic; AGraphicType : TGraphicTypeEnum; AFileName : String); var vGraphicClass : TGraphicClass; vTargetGraphic : TGraphic; vBmp : TBitmap; begin case AGraphicType of gteBMP : vGraphicClass := TBitmap; gtejpg : vGraphicClass := TJPEGImage; gtetif : vGraphicClass := TWICImage; gtepng : vGraphicClass := TPngImage; else // EXIT; or raise... end; if aGraphic is vGraphicClass then //As suggested by Rob Kennedy AGraphic.SaveToFile(AFileName) else begin vBmp := nil; vTargetGraphic := vGraphicClass.Create; try vBmp := TBitmap.Create; vBmp.Assign(AGraphic); vTargetGraphic.Assign(vBmp); vTargetGraphic.SaveToFile(aFileName); finally vTargetGraphic.Free; vBmp.Free; end; end; end;
Assigning TPicture did not work, because when assigning TPicture, TPicture converts the graphics to the class you are assigning.
Please note that in my example there are 2 conversion levels since the original image is converted to a bitmap before being converted to the final format. There can be quite a lot of information loss in the process. Most (all?) Of the graphic types know how to convert to and from TBitmap, but TJPEGImage has no idea how to convert to TPngImage and vice versa.
A more efficient conversion method may be developed that provides transparency and other effects specific to the file format, but this is known to me. But depending on your needs, this may be enough.
source share