Delphi.cz

Český portál Delphi

Firemonkey a konverze grafických formátů

Převáděl jsem aplikaci z VCL na FireMonkey a po (upravené) konverzi přes Mida Convertor z instalace XE3 (jedná se o light verzi, která neupravuje pas soubor, ale jen DFM) jsem po první kompilaci narazil na problém, že aplikace nešla přeložit, protože nenašel kompilátor unit jpeg.

Řešení je samozřejmě nasnadě, stačí použít FMX verzi TBitmap, která podporuje x formátů.

procedure TForm1.Button1Click(Sender: TObject);
var
  bmp: TBitmap;
begin
  bmp:= TBitmap.CreateFromFile('screen.png');
//  bmp:= TBitmap.CreateFromStream(stream);
  bmp.FlipHorizontal;
  bmp.FlipVertical;
  bmp.Rotate(5);
  bmp.SaveToFile('screen2.jpg');
  bmp.Free;
end;

Je to opravdu príma, hlavně protože při použití streamu to automaticky detekuje grafický formát a při ukládání stačí specifikovat jméno souboru a FMX aplikuje příslušný encoder pro uložení.

Samozřejmě můžete aplikovat libovolný filtr (jsou na paletě komponent - effects), např. TBlurEffect

procedure TForm1.Button1Click(Sender: TObject);
var
  bmp: TBitmap;
  efekt: TBlurEffect;
begin
  bmp:= TBitmap.CreateFromFile('screen.png');
  efekt := TBlurEffect.Create(nil);
  bmp.Canvas.BeginScene;
  efekt.Softness := 0.10;
  efekt.ProcessEffect(bmp.Canvas, bmp, 1);
  bmp.Canvas.EndScene;
  bmp.SaveToFile('screen2.jpg');
  efekt.Free;
  bmp.Free;
end;

Poslední věc: ohledně formátů jsou to na Windows tyto:

  TBitmapCodecManager.RegisterBitmapCodecClass('.bmp', SVBitmaps, True, TBitmapCodecWIC);
  TBitmapCodecManager.RegisterBitmapCodecClass('.jpg', SVJPGImages, True, TBitmapCodecWIC);
  TBitmapCodecManager.RegisterBitmapCodecClass('.jpeg', SVJPGImages, True, TBitmapCodecWIC);
  TBitmapCodecManager.RegisterBitmapCodecClass('.png', SVPNGImages, True, TBitmapCodecWIC);
  TBitmapCodecManager.RegisterBitmapCodecClass('.gif', SVGIFImages, True, TBitmapCodecWIC);
  TBitmapCodecManager.RegisterBitmapCodecClass('.tif', SVTIFFImages, True, TBitmapCodecWIC);
  TBitmapCodecManager.RegisterBitmapCodecClass('.ico', SVIcons, True, TBitmapCodecWIC);
  TBitmapCodecManager.RegisterBitmapCodecClass('.wmp', SWMPImages, True, TBitmapCodecWIC);

pro OS X pak

  TBitmapCodecManager.RegisterBitmapCodecClass('.bmp', SVBitmaps, True, TBitmapCodecQuartz);
  TBitmapCodecManager.RegisterBitmapCodecClass('.icns', SVIcons, True, TBitmapCodecQuartz);
  TBitmapCodecManager.RegisterBitmapCodecClass('.jpg', SVJPGImages, True, TBitmapCodecQuartz);
  TBitmapCodecManager.RegisterBitmapCodecClass('.jp2', SVJP2Images, True, TBitmapCodecQuartz);
  TBitmapCodecManager.RegisterBitmapCodecClass('.jpeg', SVJPGImages, True, TBitmapCodecQuartz);
  TBitmapCodecManager.RegisterBitmapCodecClass('.png', SVPNGImages, True, TBitmapCodecQuartz);
  TBitmapCodecManager.RegisterBitmapCodecClass('.gif', SVGIFImages, True, TBitmapCodecQuartz);
  TBitmapCodecManager.RegisterBitmapCodecClass('.tif', SVTIFFImages, True, TBitmapCodecQuartz);
  TBitmapCodecManager.RegisterBitmapCodecClass('.tga', SVTGAImages, True, TBitmapCodecQuartz);

Datum: 2012-11-01 00:39:00 Tagy: grafika, FireMonkey

Praxe