Exemplo n.º 1
0
        public static IPrintDocumentSource Print(Win2DScene scene, float padding)
        {
            Guard.NotNull(scene, nameof(scene));
            Guard.GreaterThan(padding, 0, nameof(padding));

            var printDocument = new CanvasPrintDocument();

            var sceneBounds = scene.RenderBounds;

            Action <CanvasDrawingSession, PrintPageDescription> renderForPrint = (session, page) =>
            {
                session.Clear(Colors.White);

                var size = page.PageSize.ToVector2();

                var ratio = sceneBounds.Width / sceneBounds.Height;

                var targetSizeX = Math.Min(size.X - (2 * padding), sceneBounds.Width);
                var targetSizeY = targetSizeX / ratio;

                if (targetSizeY > page.PageSize.Height)
                {
                    targetSizeY = Math.Min(size.Y - (2 * padding), sceneBounds.Height);
                    targetSizeX = targetSizeY * ratio;
                }

                var zoom = targetSizeX / sceneBounds.Width;

                session.Transform =
                    Matrix3x2.CreateTranslation(
                        -sceneBounds.Position.X,
                        -sceneBounds.Position.Y) *
                    Matrix3x2.CreateScale(zoom) *
                    Matrix3x2.CreateTranslation(
                        0.5f * (size.X - targetSizeX),
                        0.5f * (size.Y - targetSizeY));

                scene.Render(session, false, Rect2.Infinite);
            };

            printDocument.Preview += (sender, args) =>
            {
                sender.SetPageCount(1);

                renderForPrint(args.DrawingSession, args.PrintTaskOptions.GetPageDescription(1));
            };

            printDocument.Print += (sender, args) =>
            {
                using (var session = args.CreateDrawingSession())
                {
                    renderForPrint(session, args.PrintTaskOptions.GetPageDescription(1));
                }
            };

            return(printDocument);
        }
Exemplo n.º 2
0
        public static async Task RenderScreenshotAsync(Win2DScene scene, ICanvasResourceCreator device, Stream stream, Vector3 background, float?dpi = null, float padding = 20)
        {
            Guard.NotNull(scene, nameof(scene));
            Guard.NotNull(stream, nameof(stream));
            Guard.NotNull(device, nameof(device));
            Guard.GreaterThan(padding, 0, nameof(padding));

            var sceneBounds = scene.RenderBounds;

            var w = sceneBounds.Size.X + (2 * padding);
            var h = sceneBounds.Size.Y + (2 * padding);

            var dpiValue = dpi ?? DisplayInformation.GetForCurrentView().LogicalDpi;

            var dpiFactor = dpiValue / DpiWithPixelMapping;

            var wPixels = (int)(w * dpiFactor);
            var hPixels = (int)(h * dpiFactor);

            var maxPixels = Math.Min(MaxSize, device.Device.MaximumBitmapSizeInPixels - 100);

            var wDiff = wPixels - maxPixels;
            var hDiff = hPixels - maxPixels;

            if (wDiff > 0 || hDiff > 0)
            {
                if (wDiff > hDiff)
                {
                    dpiValue = (int)(dpiValue * ((float)maxPixels / wPixels));
                }
                else
                {
                    dpiValue = (int)(dpiValue * ((float)maxPixels / hPixels));
                }
            }

            using (var target = new CanvasRenderTarget(device, w, h, dpiValue))
            {
                using (var session = target.CreateDrawingSession())
                {
                    session.Clear(background.ToColor());

                    session.Transform =
                        Matrix3x2.CreateTranslation(
                            -sceneBounds.Position.X + padding,
                            -sceneBounds.Position.Y + padding);

                    scene.Render(session, false, Rect2.Infinite);
                }

                await target.SaveAsync(stream.AsRandomAccessStream(), CanvasBitmapFileFormat.Png).AsTask();
            }
        }