Example #1
0
        private void OnDraw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            var ds = args.DrawingSession;

            EnsureSvgDrawingLoaded(sender);

            if (svgDrawing == null)
            {
                if (wantedSvgFile != null)
                {
                    ds.DrawText("Loading...",
                                sender.Size.ToVector2() / 2,
                                Colors.Black,
                                new CanvasTextFormat()
                    {
                        HorizontalAlignment = CanvasHorizontalAlignment.Center,
                        VerticalAlignment   = CanvasVerticalAlignment.Center
                    });
                }
            }
            else
            {
                var svgImage = svgDrawing.Draw(sender.Size);

                var bounds = svgImage.GetBounds(ds);

                if (bounds.Width > sender.Size.Width || bounds.Height > sender.Size.Height)
                {
                    double scale    = Math.Min(sender.Size.Height / bounds.Height, sender.Size.Width / bounds.Width);
                    Size   destSize = new Size(bounds.Width * scale, bounds.Height * scale);

                    args.DrawingSession.DrawImage(
                        svgImage,
                        new Rect(new Point(), destSize),
                        bounds);
                }
                else
                {
                    args.DrawingSession.DrawImage(
                        svgImage,
                        -(float)bounds.Left, -(float)bounds.Top);
                }
            }
        }