Exemple #1
0
        private static void SaveCanvasAsImg()
        {
            var path = DialogService.Current.GetSaveFilePath(SaveAsImg_DefaultFileName);

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            try {
                using (var fs = File.Create(path)) {
                    CanvasService.CanvasDataContext.CommitEdit();

                    var drawObjects = CanvasService.CanvasDataContext.GetAllVisibleDrawObjects();
                    var setting     = new ExportImgSetting(fs);
                    ServiceProvider.GetInstance <IExportCanvasAsImgService>().ExportDrawObjectsAsImg(drawObjects, setting);
                }

                if (MsgBoxService.Show(
                        LanguageService.FindResourceString(
                            MsgText_ConfirmToShowExportedDirectory
                            ),
                        MessageBoxButton.YesNo
                        ) == MessageBoxResult.Yes)
                {
                    LocalExplorerService.OpenFolderAndSelectFile(path);
                }
            }
            catch (Exception ex) {
                LoggerService.WriteException(ex);
                MsgBoxService.Show(ex.Message);
            }
        }
        public void ExportDrawObjectsAsImg(IEnumerable <DrawObject> drawObjects, ExportImgSetting exportImgSetting)
        {
            if (drawObjects == null)
            {
                throw new ArgumentNullException(nameof(drawObjects));
            }

            if (exportImgSetting == null)
            {
                throw new ArgumentNullException(nameof(exportImgSetting));
            }

            if (exportImgSetting.ExportStream == null)
            {
                throw new ArgumentNullException(nameof(exportImgSetting.ExportStream));
            }

            var stream = exportImgSetting.ExportStream;

            if (!stream.CanWrite)
            {
                throw new ArgumentException($"{nameof(stream)} can't be written.");
            }

            var width  = exportImgSetting.Width;
            var height = exportImgSetting.Height;

            var renderTargetBitMap = new RenderTargetBitmap(
                width, height,
                ScreenResolution,
                ScreenResolution,
                PixelFormats.Default
                );

            ///使用<see cref="DrawingVisual"/>作为<see cref="Visual"/>参数;
            var drawingVisual = new DrawingVisual {
                Clip = new RectangleGeometry(new Rect {
                    Width  = width,
                    Height = height
                })
            };

            var drawingContext = drawingVisual.RenderOpen();

            var canvasProxy = new WindowsCanvasScreenConverter {
                ActualWidth       = width,
                ActualHeight      = height,
                PanScreenPosition = new Vector2D(width / 2, height / 2),
                Zoom = 1
            };

            var canvas = new WindowsCanvas(canvasProxy)
            {
                DrawingContext = drawingContext
            };

            var scope = GetScopeWithDrawObjects(drawObjects, width, height);

            canvasProxy.Zoom = scope.Zoom;
            canvasProxy.PanScreenPosition = scope.PanScreenPosition;

            //绘制背景;
            if (exportImgSetting.Background != null)
            {
                drawingContext.DrawRectangle(BrushAdapter.ConvertToSystemBrush(exportImgSetting.Background), null, new Rect(new System.Windows.Size(width, height)));
            }

            //绘制绘制对象;
            foreach (var drawObject in drawObjects.Where(p => exportImgSetting.ExportUnvisible || p.IsVisible))
            {
                drawObject.Draw(canvas, canvasProxy);
            }

            drawingContext.Close();

            renderTargetBitMap.Render(drawingVisual);

            var encoder = new PngBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(renderTargetBitMap));
            encoder.Save(stream);

#if DEBUG
            //if (stream.Length > 0)
            //{

            //    using (var fs = File.Create($"D://Random/{Guid.NewGuid().ToString("P")}.png"))
            //    {
            //        stream.Position = 0;
            //        stream.CopyTo(fs);
            //    }
            //}
#endif
        }