Hide Comments
Hide Comments

Load an SVG into a TBitmap/TGraphic

Comments (0)

This is one of the simplest ways to display an SVG in your programs with the RSCL.  The RSCL hooks itself into the TPicture/TGraphic architecture of VCL and the TBitmap codecs of FMX.  In practice, this means you just have to the include RSImaging.SVGImage.pas (VCL) or FMX.RS.SVGCodec.pas (FMX) unit.  Then, you need to read the SVG into the TPicture/TGraphic or TBitmap.

 

To load an SVG into a TImage control, the code is really easy:

 

var

  Image1: TImage;

[...]

// VCL

Image1.Picture.LoadFromFile('c:\mysvg.svg'); 

// FMX

Image1.Bitmap.LoadFromFile('c:\mysvg.svg');

 

To load an SVG into a bitmap is easy as well.  There are no changes to do this in FMX, though with VCL it is slightly more involved:

 

var

 Bitmap: TBitmap;

 Picture: TPicture;

begin

 Bitmap := TBitmap.Create;

 try

  Picture := TPicture.Create;

  try

   Picture.LoadFromFile('c:\mysvg.svg');

   Picture.Graphic.SetSize(1000,1000);

   Bitmap.Assign(Picture.Graphic);

   PaintBox1.Canvas.Draw(0, 0, Bitmap);

  finally

   Picture.Free;

  end;

 finally

  Bitmap.Free;

 end;

end;

 

Note that everything is rendered using defaults.  The size of the bitmap is determined from the SVG (SVGs may optionally specify their intended width and height or the RSCL will calculate the size).  Additionally, what you get is a bitmap, you cannot change colors of individually elements, detect elements being clicked, or anything else.  The result is a simple bitmap.

 

The VCL TGraphic architecture is more powerful in this case.  The TGraphic architecture allows you to change the size of the graphic after it is loaded.  The RSCL will redraw the SVG in the new size to keep everything looking sharp and perfect.

 

However, in FMX, this is not true.  If the specified or calculated size of the SVG is small, you can easily see jaggies as the bitmap is scaled up.  In addition, it should be noted that on mobile platforms, the FMX text output to bitmaps is poor.

Comments (0)

RiverSoftAVG SVG Component Library (RSCL) © 2013-2016, Thomas G. Grubb