Jump to content
gwideman

Copy bitmap from TSkSVG to TImage

Recommended Posts

I would like to use VCL.Skia.TSkSvg to render an SVG, and then copy the resulting bitmap to another control, for example a TImage.

 

I though that might be possible using TSkSvg's Canvas, but that's protected, so presumably not the way to do it.

 

I noticed that FMX.Skia.TSkSvg has a (inherited) PaintTo method, but the VCL version does not.

 

So what is the correct approach?  Thanks!

 

For bonus points -- is it possible to perform the above with the TSkSvg control not visible on the Form?

Share this post


Link to post

I appreciate your answers, but I'm still not completely understanding how to make this work...   A couple of followup questions below:

 

9 hours ago, Vincent Gsell said:

Doc on Skia Github page contains usefull code to convert svg to bmp 

https://github.com/skia4delphi/skia4delphi/blob/main/Documents/SVG.md

Are you referring to the Rendering SVG example?  If so, I'm afraid that example is too incomplete to show how it relates to my task, which is essentially "input an SVG file, render it to a bitmap or canvas." 

In that example, it's not clear to me:

  • where "ACanvas" comes
  • what is the role of the procedure parameter in the SkiaDraw procedure. I infer that it takes part in rendering the SVG. But under what circumstances does SkiaDraw invoke it? Why so convoluted?  I didn't find any doc on SkiaDoc,,  so if there is doc for that procedure it might shed some light.

 

9 hours ago, Vincent Gsell said:

 

From an skia's image (do not remomber if available from skSVG.

https://docwiki.embarcadero.com/Libraries/Athens/en/FMX.Skia.SkImageToBitmap

 

For VCL: 

function SkImageToBitmap(const AImage: ISkImage): TBitmap;

Where do I get the ISkImage from?

 

Thanks all!

 

Share this post


Link to post

Hi

 

In fact, to draw it properly on a Bmp, there are some init and code to add : The technic to pass the proc allow you (not only, but it is uses as is) to concentrate yourself to your drawing process.

I invite you to go to the implementation in FMX.Skia or VCL.Skia, to see what is done. (If you do not have source code, it is available github)

-> In the code, IksCanvas comes directly for a Bitmap helper class, wich introduce the canvas from a dedicated surface. 

 

And again, the skia integration is very well done, because "high level" code between 2 framework is very same.

 

-> So, here is the code you need to transfert your svg to your "vcl" control  (I do it on VCL framework, it's been a long time that I do not open a new projet on it :))

 

uses
  System.IOUtils, //files.
  System.Types, //RectF
  System.Skia, VCL.Skia;

Procedure Transfert;
var
  LBitmap: TBitmap;
begin
  LBitmap := TBitmap.Create(100, 100);
  try
    LBitmap.SkiaDraw(
      procedure (const ACanvas: ISKCanvas)
      var
        LSvgBrush: TSkSvgBrush;
      begin
        LSvgBrush := TSkSvgBrush.Create;
        try
          LSvgBrush.Source := TFile.ReadAllText('..\..\..\samples\panda.svg');
          LSvgBrush.Render(ACanvas, RectF(0, 0, LBitmap.Width, LBitmap.Height), 1);
        finally
          LSvgBrush.Free;
        end;
      end);
  finally

    //Transfert into an VCL Control
    Form59.Image1.Picture.Assign(LBitmap);
    FreeAndNil(LBitmap);
  end;
end;

procedure TForm59.FormCreate(Sender: TObject);
begin
  Transfert;
end;

 

Edited by Vincent Gsell

Share this post


Link to post
7 hours ago, gwideman said:

Where do I get the ISkImage from?

When you use skia directly, this interface come very quickly, and it will more easy to call. As I said in my first message, I do not remember if tskSVG component have an Image property : Certainly not, if you do not see it : But the code is interesting to see, again, in VCL.Skia.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×