Exemplo n.º 1
0
        /// <summary>
        /// 以视图坐标为标准,绘制椭圆;
        /// </summary>
        /// <param name="rectangle">以视图坐标为准的矩形</param>
        /// <param name="brush">填充色</param>
        /// <param name="pen">笔</param>
        public void NativeDrawEllipse(Brush brush, Pen pen, Vector2D center, double radiusX, double radiusY)
        {
            if (center == null)
            {
                throw new ArgumentNullException(nameof(center));
            }

            var centerPoint = Vector2DAdapter.ConvertToSystemPoint(center);

            DrawingContext.DrawEllipse(
                BrushAdapter.ConvertToSystemBrush(brush),
                PenAdapter.ConverterToSystemPen(pen),
                centerPoint,
                radiusX,
                radiusY
                );
        }
Exemplo n.º 2
0
        /// <summary>
        /// 以视图坐标为标准,绘制矩形;
        /// </summary>
        /// <param name="rectangle">以视图坐标为准的矩形</param>
        /// <param name="brush">填充色</param>
        /// <param name="pen">笔</param>
        public void NativeDrawRectangle(Rectangle2D2 rectangle, Brush brush, Pen pen)
        {
            if (rectangle == null)
            {
                throw new ArgumentNullException(nameof(rectangle));
            }

            ValidateDrawingContext();

            var vertexes = rectangle.GetVertexes();

            NativeDrawFill(
                vertexes.Select(p => Vector2DAdapter.ConvertToSystemPoint(p)),
                BrushAdapter.ConvertToSystemBrush(brush),
                PenAdapter.ConverterToSystemPen(pen)
                );
        }
Exemplo n.º 3
0
        /// <summary>
        /// 绘制文字;
        /// </summary>
        /// <param name="text"></param>
        /// <param name="emSize"></param>
        /// <param name="foreground"></param>
        /// <param name="origin"></param>
        public void DrawText(string text, double emSize, Brush foreground, Vector2D origin, double angle = 0)
        {
            ValidateDrawingContext();

            var originScreenPoint       = _canvasProxy.ToScreen(origin);
            var nativeOriginScreenPoint = Vector2DAdapter.ConvertToSystemPoint(originScreenPoint);

            var ft = new SystemMedia.FormattedText(
                text,
                System.Globalization.CultureInfo.CurrentCulture,
                FlowDirection.LeftToRight, TypeFace,
                emSize,
                BrushAdapter.ConvertToSystemBrush(foreground)
                );

            DrawFormattedTextCore(ft, origin, angle);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 根据所有的点,组成一个封闭区域,且可以填充,并且填充
        /// </summary>
        /// <param name="points">所有的顶点坐标</param>
        /// <param name="brush">区域颜色</param>
        private void DrawFill(IEnumerable <Vector2D> points, Brush brush, Pen pen)
        {
            if (points == null)
            {
                throw new ArgumentNullException(nameof(points));
            }
            if (pen == null)
            {
                throw new ArgumentNullException(nameof(pen));
            }

            ValidateDrawingContext();

            NativeDrawFill(
                points.Select(p => ConvertVectorToScreenPoint(p)),
                BrushAdapter.ConvertToSystemBrush(brush),
                PenAdapter.ConverterToSystemPen(pen)
                );
        }
        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
        }