Beispiel #1
0
        void Save(DrawingboardData content, RenderTargetBitmap snapShotSource)
        {
            string fileName        = content.ID;
            string snapShotAddress = StrokeBuilder.GetSnapShotAddresss(fileName);

            try
            {
                snapShotSource.Save(snapShotAddress);
                Thread saveThread = new Thread(this.SaveContentInBackground);
                saveThread.Start(content);
            }
            catch (Exception ex)
            {
                return;
            }
        }
Beispiel #2
0
        private Task <bool> SaveAsync(string path)
        {
            return(Task.Run(() =>
            {
                try
                {
                    _renderTarget?.Save(path);
                }
                catch (Exception)
                {
                    return false;
                }

                return true;
            }));
        }
Beispiel #3
0
        // 渲染并保存图片:传入文件全路径、要渲染的ItemsControl
        // todo 做成全局菜单栏里保存的方式
        public static void RenderImage(string path, ItemsControl itemsControl)
        {
            // 直接关闭导出窗口时path为空
            if (path == null)
            {
                return;
            }
            // 构造渲染图片的容器
            PixelSize          pixelSize          = new PixelSize((int)itemsControl.Width * 2, (int)itemsControl.Height * 2);
            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(pixelSize, new Vector(192, 192));

            // 在可视树上找到这个ItemsControl的的孩子ItemsPresenter
            IEnumerator <IVisual> panelChildren = itemsControl.GetVisualChildren().GetEnumerator();

            panelChildren.MoveNext();
            // ListBox > Border > ScrollViewer > Grid > ScrollContentPresenter > ItemsPresenter
            if (itemsControl is ListBox)
            {
                Border border = (Border)panelChildren.Current;
                panelChildren = border.GetVisualChildren().GetEnumerator();
                panelChildren.MoveNext();
                ScrollViewer scrollViewer = (ScrollViewer)panelChildren.Current;
                panelChildren = scrollViewer.GetVisualChildren().GetEnumerator();
                panelChildren.MoveNext();
                Grid grid = (Grid)panelChildren.Current;
                panelChildren = grid.GetVisualChildren().GetEnumerator();
                panelChildren.MoveNext();
                ScrollContentPresenter scrollContentPresenter = (ScrollContentPresenter)panelChildren.Current;
                panelChildren = scrollContentPresenter.GetVisualChildren().GetEnumerator();
                panelChildren.MoveNext();
            }
            // ItemsControl > Border > ItemsPresenter
            else
            {
                Border border = (Border)panelChildren.Current;
                panelChildren = border.GetVisualChildren().GetEnumerator();
                panelChildren.MoveNext();
            }
            ItemsPresenter itemsPresenter = (ItemsPresenter)panelChildren.Current;

            // 渲染并保存
            renderTargetBitmap.Render(itemsPresenter);
            renderTargetBitmap.Save(path);

            mainWindowVM.Tips = "导出图片至:" + path;
        }
Beispiel #4
0
        public static void RenderToFile(ObservableCard card, string filename)
        {
            var target = Render(card);

            if (null != target)
            {
                var pixelSize = new PixelSize((int)target.Width, (int)target.Height);
                var size      = new Size(target.Width, target.Height);
                var dpiVector = new Vector(96, 96);

                using var renderBitmap = new RenderTargetBitmap(pixelSize, dpiVector);

                target.Measure(size);
                target.Arrange(new Rect(size));
                renderBitmap.Render(target);
                renderBitmap.Save(filename);
            }
        }
Beispiel #5
0
        protected void RenderToFile(Control target, [CallerMemberName] string testName = "")
        {
            if (!Directory.Exists(OutputPath))
            {
                Directory.CreateDirectory(OutputPath);
            }

            string path = Path.Combine(OutputPath, testName + ".out.png");

            using (RenderTargetBitmap bitmap = new RenderTargetBitmap(
                       (int)target.Width,
                       (int)target.Height))
            {
                Size size = new Size(target.Width, target.Height);
                target.Measure(size);
                target.Arrange(new Rect(size));
                bitmap.Render(target);
                bitmap.Save(path);
            }
        }
Beispiel #6
0
        // 导出图片
        // todo 做成全局菜单栏里保存的方式
        public async void ExportImage()
        {
            string path = await GetSaveFileName();

            ItemsControl panel = ControlExtensions.FindControl <ItemsControl>(this, "panel");

            // 构造渲染图片的容器
            PixelSize          pixelSize          = new PixelSize((int)panel.Width, (int)panel.Height);
            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(pixelSize);

            // 在可视树上找到这个ItemsControl的的孩子ItemsPresenter
            IEnumerator <IVisual> panelChildren = panel.GetVisualChildren().GetEnumerator();

            panelChildren.MoveNext();
            ItemsPresenter itemsPresenter = (ItemsPresenter)panelChildren.Current;

            // 渲染并保存
            renderTargetBitmap.Render(itemsPresenter);
            renderTargetBitmap.Save(path);

            ResourceManager.mainWindowVM.Tips = "导出图片至:" + path;
        }
Beispiel #7
0
        protected async Task RenderToFile(Control target, [CallerMemberName] string testName = "")
        {
            if (!Directory.Exists(OutputPath))
            {
                Directory.CreateDirectory(OutputPath);
            }

            var immediatePath = Path.Combine(OutputPath, testName + ".immediate.out.png");
            var deferredPath  = Path.Combine(OutputPath, testName + ".deferred.out.png");
            var factory       = AvaloniaLocator.Current.GetService <IPlatformRenderInterface>();

            using (RenderTargetBitmap bitmap = new RenderTargetBitmap(
                       (int)target.Width,
                       (int)target.Height))
            {
                Size size = new Size(target.Width, target.Height);
                target.Measure(size);
                target.Arrange(new Rect(size));
                bitmap.Render(target);
                bitmap.Save(immediatePath);
            }

            using (var rtb = factory.CreateRenderTargetBitmap((int)target.Width, (int)target.Height, 96, 96))
                using (var renderer = new DeferredRenderer(target, rtb))
                {
                    Size size = new Size(target.Width, target.Height);
                    target.Measure(size);
                    target.Arrange(new Rect(size));
                    renderer.UnitTestUpdateScene();

                    // Do the deferred render on a background thread to expose any threading errors in
                    // the deferred rendering path.
                    await Task.Run((Action)renderer.UnitTestRender);

                    rtb.Save(deferredPath);
                }
        }
Beispiel #8
0
 public static void DrawAsPng <TContent>
     (this Painter <AvaloniaCanvas, TContent, AvaloniaColor> painter,
     System.IO.Stream target,
     float textPainterCanvasWidth      = TextPainter.DefaultCanvasWidth,
     CSharpMathTextAlignment alignment = CSharpMathTextAlignment.TopLeft) where TContent : class
 {
     if (!(painter.Measure(textPainterCanvasWidth) is { } size))
     {
         return;
     }
     // RenderTargetBitmap does not support zero width/height. ArgumentException will be thrown.
     if (size.Width is 0)
     {
         size.Width = 1;
     }
     if (size.Height is 0)
     {
         size.Height = 1;
     }
     using var bitmap =
               new RenderTargetBitmap(new PixelSize((int)size.Width, (int)size.Height));
     bitmap.Render(new DrawVisual <TContent>(painter, size, alignment));
     bitmap.Save(target);
 }
Beispiel #9
0
        protected async Task RenderToFile(Control target, [CallerMemberName] string testName = "", double dpi = 96)
        {
            if (!Directory.Exists(OutputPath))
            {
                Directory.CreateDirectory(OutputPath);
            }

            var immediatePath  = Path.Combine(OutputPath, testName + ".immediate.out.png");
            var deferredPath   = Path.Combine(OutputPath, testName + ".deferred.out.png");
            var compositedPath = Path.Combine(OutputPath, testName + ".composited.out.png");
            var factory        = AvaloniaLocator.Current.GetService <IPlatformRenderInterface>();
            var pixelSize      = new PixelSize((int)target.Width, (int)target.Height);
            var size           = new Size(target.Width, target.Height);
            var dpiVector      = new Vector(dpi, dpi);

            using (RenderTargetBitmap bitmap = new RenderTargetBitmap(pixelSize, dpiVector))
            {
                target.Measure(size);
                target.Arrange(new Rect(size));
                bitmap.Render(target);
                bitmap.Save(immediatePath);
            }


            using (var rtb = factory.CreateRenderTargetBitmap(pixelSize, dpiVector))
                using (var renderer = new DeferredRenderer(target, rtb))
                {
                    target.Measure(size);
                    target.Arrange(new Rect(size));
                    renderer.UnitTestUpdateScene();

                    // Do the deferred render on a background thread to expose any threading errors in
                    // the deferred rendering path.
                    await Task.Run((Action)renderer.UnitTestRender);

                    threadingInterface.MainThread = Thread.CurrentThread;

                    rtb.Save(deferredPath);
                }

            var timer = new ManualRenderTimer();

            var compositor = new Compositor(new RenderLoop(timer, Dispatcher.UIThread), null);

            using (var rtb = factory.CreateRenderTargetBitmap(pixelSize, dpiVector))
            {
                var root = new TestRenderRoot(dpiVector.X / 96, rtb);
                using (var renderer = new CompositingRenderer(root, compositor)
                {
                    RenderOnlyOnRenderThread = false
                })
                {
                    root.Initialize(renderer, target);
                    renderer.Start();
                    Dispatcher.UIThread.RunJobs();
                    timer.TriggerTick();
                }

                // Free pools
                for (var c = 0; c < 11; c++)
                {
                    TestThreadingInterface.RunTimers();
                }
                rtb.Save(compositedPath);
            }
        }