Пример #1
0
        private void OnClear(object sender, EventArgs e)
        {
            cropPoints.Clear();
            cropPath?.Dispose();
            cropPath = null;

            skiaView.InvalidateSurface();
        }
Пример #2
0
        private IList <SKPath> ToPaths(IToolContext context, IList <IBaseShape> shapes)
        {
            if (shapes == null || shapes.Count <= 0)
            {
                return(null);
            }

            var paths = new List <SKPath>();

            for (int i = 0; i < shapes.Count; i++)
            {
                var fillType = SKPathFillType.Winding;
                if (shapes[i] is PathShape pathShape)
                {
                    fillType = SkiaHelper.ToSKPathFillType(pathShape.FillType);
                }
                var path = new SKPath()
                {
                    FillType = fillType
                };
                var result = SkiaHelper.AddShape(context, shapes[i], 0.0, 0.0, path);
                if (result == true && path.IsEmpty == false)
                {
                    paths.Add(path);
                }
                else
                {
                    path.Dispose();
                }
            }

            return(paths);
        }
Пример #3
0
        /// <summary>
        /// Draws the graph on a canvas.
        /// </summary>
        /// <param name="Canvas">Canvas to draw on.</param>
        /// <param name="Points">Points to draw.</param>
        /// <param name="Parameters">Graph-specific parameters.</param>
        /// <param name="PrevPoints">Points of previous graph of same type (if available), null (if not available).</param>
        /// <param name="PrevParameters">Parameters of previous graph of same type (if available), null (if not available).</param>
        /// <param name="DrawingArea">Current drawing area.</param>
        public void DrawGraph(SKCanvas Canvas, SKPoint[] Points, object[] Parameters, SKPoint[] PrevPoints, object[] PrevParameters,
                              DrawingArea DrawingArea)
        {
            SKPaint Pen   = null;
            SKPath  Path  = null;
            bool    First = true;

            try
            {
                Pen  = Graph.ToPen(Parameters[0], Parameters[1]);
                Path = new SKPath();

                foreach (SKPoint Point in Points)
                {
                    if (First)
                    {
                        First = false;
                        Path.MoveTo(Point);
                    }
                    else
                    {
                        Path.LineTo(Point);
                    }
                }

                Canvas.DrawPath(Path, Pen);
            }
            finally
            {
                Pen?.Dispose();
                Path?.Dispose();
            }
        }
Пример #4
0
        /// <summary>
        /// Create shape
        /// </summary>
        /// <param name="points"></param>
        /// <param name="shapeModel"></param>
        public override void CreateShape(IList <Point> points, IShapeModel shapeModel)
        {
            CreateCanvas();

            var shape  = new SKPath();
            var origin = points.ElementAtOrDefault(0);

            if (origin == default)
            {
                return;
            }

            shape.MoveTo((float)origin.X, (float)origin.Y);

            for (var i = 1; i < points.Count; i++)
            {
                shape.LineTo((float)points[i].X, (float)points[i].Y);
            }

            shape.Close();

            var pen = new SKPaint
            {
                Style = SKPaintStyle.Fill,
                Color = shapeModel.Color.ToSKColor()
            };

            _canvas.DrawPath(shape, pen);

            pen.Dispose();
            shape.Dispose();
        }
Пример #5
0
        private void DrawGraph(SKCanvas Canvas, SKPoint[] Points, object[] Parameters, SKPoint[] PrevPoints, object[] PrevParameters,
                               DrawingArea DrawingArea)
        {
            SKPaint Pen  = null;
            SKPath  Path = null;

            try
            {
                Pen  = Graph.ToPen(Parameters[0], Parameters[1]);
                Path = CreateSpline(Points);

                Canvas.DrawPath(Path, Pen);
            }
            finally
            {
                if (Pen != null)
                {
                    Pen.Dispose();
                }

                if (Path != null)
                {
                    Path.Dispose();
                }
            }
        }
Пример #6
0
        private void DrawGraph(SKCanvas Canvas, SKPoint[] Points, object[] Parameters, SKPoint[] PrevPoints, object[] PrevParameters,
                               DrawingArea DrawingArea)
        {
            SKColor Color = Graph.ToColor(Parameters[0]);
            SKPaint Brush = new SKPaint
            {
                FilterQuality = SKFilterQuality.High,
                IsAntialias   = true,
                Style         = SKPaintStyle.Fill,
                Color         = Color
            };

            SKPath Path  = new SKPath();
            bool   First = true;

            foreach (SKPoint P in Points)
            {
                if (First)
                {
                    First = false;
                    Path.MoveTo(P);
                }
                else
                {
                    Path.LineTo(P);
                }
            }

            Path.Close();

            Canvas.DrawPath(Path, Brush);

            Brush.Dispose();
            Path.Dispose();
        }
        private void OnTouch(object sender, SKTouchEventArgs e)
        {
            switch (e.ActionType)
            {
            case SKTouchAction.Pressed:
                currentPath?.Dispose();
                currentPath = new SKPath();
                currentPath.MoveTo(e.Location);
                break;

            case SKTouchAction.Moved:
                if (e.InContact)
                {
                    currentPath?.LineTo(e.Location);
                }
                break;

            case SKTouchAction.Released:
            case SKTouchAction.Cancelled:
                break;
            }

            e.Handled = true;

            signatureView.InvalidateSurface();
        }
Пример #8
0
 /// <summary>
 /// Closes this instance and frees resources.
 /// <para>
 /// After a call to this method, the canvas can no longer be used for drawing.
 /// </para>
 /// </summary>
 protected void Close()
 {
     if (_canvas != null)
     {
         _canvas.Dispose();
         _canvas = null;
     }
     if (_bitmap != null)
     {
         _bitmap.Dispose();
         _bitmap = null;
     }
     if (_path != null)
     {
         _path.Dispose();
         _path = null;
     }
     if (_strokePaint != null)
     {
         _strokePaint.Dispose();
         _strokePaint = null;
         _fillPaint.Dispose();
         _fillPaint = null;
         _textPaint.Dispose();
         _textPaint = null;
         _regularTypeface.Dispose();
         _regularTypeface = null;
         _boldTypeface.Dispose();
         _boldTypeface = null;
     }
 }
Пример #9
0
        private SKPath ToPath(IToolContext context, IBaseShape shape)
        {
            var fillType = SKPathFillType.Winding;

            if (shape is PathShape pathShape)
            {
                fillType = SkiaHelper.ToSKPathFillType(pathShape.FillType);
            }

            var geometry = new SKPath()
            {
                FillType = fillType
            };

            if (SkiaHelper.AddShape(context, shape, 0.0, 0.0, geometry) == true)
            {
                return(geometry);
            }
            else
            {
                geometry.Dispose();
            }

            return(null);
        }
Пример #10
0
        void PaintRedBar(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            SKPath path = new SKPath
            {
                FillType = SKPathFillType.EvenOdd
            };

            path.AddPoly(new SKPoint[] { new SKPoint(0, 0), new SKPoint(info.Width, 8), new SKPoint(0, 8) });

            SKPaint paint = new SKPaint()
            {
                Style = SKPaintStyle.Fill,
                Color = new SKColor(0xFF922610)
            };

            canvas.DrawPath(path, paint);
            paint.Dispose();
            path.Dispose();

            //paint.Style = SKPaintStyle.Stroke;
            //paint.StrokeWidth = 2;
            //paint.Color = SKColors.Black;

            //canvas.DrawPath(path, paint);
        }
Пример #11
0
        void PaintHeaderBar(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            SKPath path = new SKPath
            {
                FillType = SKPathFillType.EvenOdd
            };

            path.AddRect(new SKRect(0, 0, info.Width, 8));

            SKPaint paint = new SKPaint()
            {
                Style = SKPaintStyle.Fill,
                Color = new SKColor(0xFFE69A28)
            };

            canvas.DrawPath(path, paint);

            paint.Style       = SKPaintStyle.Stroke;
            paint.StrokeWidth = 2;
            paint.Color       = SKColors.Black;

            canvas.DrawPath(path, paint);
            paint.Dispose();
            path.Dispose();
        }
Пример #12
0
        public SKImage CreateBackgroundImage()
        {
            SKPaint paint = null;
            SKPath  path  = null;

            try
            {
                var height = (int)Bounds.Height;
                var width  = (int)Bounds.Width;

                using (var surface = SKSurface.Create(width, height, SKColorType.N_32, SKAlphaType.Premul))
                {
                    var canvas = surface.Canvas;

                    //canvas.Clear (SKColors.Beige);

                    paint = new SKPaint
                    {
                        Color       = SKColors.DarkBlue,
                        IsStroke    = true,
                        StrokeWidth = 1,
                        StrokeCap   = SKStrokeCap.Round,
                        IsAntialias = true
                    };

                    //path = new SKPath ();
                    //path.MoveTo (0f, 0f);
                    //path.LineTo (width, height);
                    //path.Close ();

                    //canvas.DrawPath (path, paint);


                    //DrawArc (canvas, paint, 0, 90);

                    var start = -90;
                    var end   = 0;

                    //DrawCircle (canvas, paint, width / 2, height / 2, width / 2 - 4);
                    DrawArcFromTo(canvas, paint, width / 2, height / 2, width / 2 - 4, start, end);
                    DrawArcFromTo(canvas, paint, width / 2, height / 2, (int)((width / 2 - 4) * 0.8), start, end);

                    return(surface.Snapshot());
                }
            }
            finally
            {
                if (paint != null)
                {
                    paint.Dispose();
                }

                if (path != null)
                {
                    path.Dispose();
                }
            }
        }
Пример #13
0
        /// <summary>
        /// Draws the graph on a canvas.
        /// </summary>
        /// <param name="Canvas">Canvas to draw on.</param>
        /// <param name="Points">Points to draw.</param>
        /// <param name="Parameters">Graph-specific parameters.</param>
        /// <param name="PrevPoints">Points of previous graph of same type (if available), null (if not available).</param>
        /// <param name="PrevParameters">Parameters of previous graph of same type (if available), null (if not available).</param>
        /// <param name="DrawingArea">Current drawing area.</param>
        public void DrawGraph(SKCanvas Canvas, SKPoint[] Points, object[] Parameters, SKPoint[] PrevPoints, object[] PrevParameters,
                              DrawingArea DrawingArea)
        {
            SKPaint Brush = null;
            SKPath  Path  = null;

            try
            {
                Brush = new SKPaint()
                {
                    Color = Graph.ToColor(Parameters[0]),
                    Style = SKPaintStyle.Fill
                };
                Path = new SKPath();

                float HalfBarWidth = (DrawingArea.Width - Points.Length) * 0.45f / Points.Length;
                float x0, y0, x1, y1;
                int   i, c;

                if (!(PrevPoints is null))
                {
                    if ((c = PrevPoints.Length) != Points.Length)
                    {
                        PrevPoints = null;
                    }
                    else
                    {
                        for (i = 0; i < c; i++)
                        {
                            if (PrevPoints[i].X != Points[i].X)
                            {
                                PrevPoints = null;
                                break;
                            }
                        }
                    }
                }

                i = 0;
                foreach (SKPoint Point in Points)
                {
                    x0 = Point.X - HalfBarWidth + 1;
                    y0 = Point.Y;
                    x1 = Point.X + HalfBarWidth - 1;
                    y1 = PrevPoints != null ? PrevPoints[i++].Y : DrawingArea.OrigoY;

                    Canvas.DrawRect(new SKRect(x0, y0, x1, y1), Brush);
                }

                Canvas.DrawPath(Path, Brush);
            }
            finally
            {
                Brush?.Dispose();
                Path?.Dispose();
            }
        }
Пример #14
0
        /// <summary>
        /// Draws the graph on a canvas.
        /// </summary>
        /// <param name="Canvas">Canvas to draw on.</param>
        /// <param name="Points">Points to draw.</param>
        /// <param name="Parameters">Graph-specific parameters.</param>
        /// <param name="PrevPoints">Points of previous graph of same type (if available), null (if not available).</param>
        /// <param name="PrevParameters">Parameters of previous graph of same type (if available), null (if not available).</param>
        /// <param name="DrawingArea">Current drawing area.</param>
        public void DrawGraph(SKCanvas Canvas, SKPoint[] Points, object[] Parameters, SKPoint[] PrevPoints, object[] PrevParameters,
                              DrawingArea DrawingArea)
        {
            SKPaint Brush = null;
            SKPath  Path  = null;

            try
            {
                Brush = new SKPaint()
                {
                    Style = SKPaintStyle.Fill,
                    Color = Graph.ToColor(Parameters[0])
                };
                Path = new SKPath();

                Path = Plot2DCurvePainter.CreateSpline(Points);

                if (PrevPoints is null)
                {
                    IElement Zero;
                    ISet     Set = DrawingArea.MinY.AssociatedSet;

                    if (Set is IGroup Group)
                    {
                        Zero = Group.AdditiveIdentity;
                    }
                    else
                    {
                        Zero = new DoubleNumber(0);
                    }

                    IVector XAxis = VectorDefinition.Encapsulate(new IElement[] { DrawingArea.MinX, DrawingArea.MaxX }, false, null) as IVector;
                    IVector YAxis = VectorDefinition.Encapsulate(new IElement[] { Zero, Zero }, false, null) as IVector;

                    PrevPoints = DrawingArea.Scale(XAxis, YAxis);

                    if (DrawingArea.MinX is StringValue && DrawingArea.MaxX is StringValue)
                    {
                        PrevPoints[0].X = Points[0].X;
                        PrevPoints[1].X = Points[Points.Length - 1].X;
                    }
                }

                PrevPoints = (SKPoint[])PrevPoints.Clone();
                Array.Reverse(PrevPoints);
                Plot2DCurvePainter.CreateSpline(Path, PrevPoints);

                Path.LineTo(Points[0]);

                Canvas.DrawPath(Path, Brush);
            }
            finally
            {
                Brush?.Dispose();
                Path?.Dispose();
            }
        }
Пример #15
0
        protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (propertyName == WidthProperty.PropertyName || propertyName == HeightProperty.PropertyName)
            {
                path?.Dispose();
                path = null;
            }

            base.OnPropertyChanged(propertyName);
        }
Пример #16
0
        public void BeginRender(float width, float height)
        {
            var newImage = SKSurface.Create((int)width, (int)height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);

            _surface = newImage;
            if (_path != null)
            {
                _path.Dispose();
            }
            _path          = new SKPath();
            _path.FillType = SKPathFillType.Winding;
        }
Пример #17
0
 public void EndLines()
 {
     if (_inLines)
     {
         _inLines = false;
         _paints.Stroke.StrokeWidth = _lineWidth;
         _paints.Stroke.StrokeJoin  = SKStrokeJoin.Round;
         _c.DrawPath(_linesPath, _paints.Stroke);
         _linesPath.Dispose();
         _linesPath = null;
     }
 }
Пример #18
0
        /// <summary>
        /// Forces the invalidation of the chart element
        /// </summary>
        public override void Invalidate()
        {
            if (annotation != null)
            {
                //Dispose previously instantiated graphics path
                if (_SKPath != null)
                {
                    _SKPath.Dispose();
                    _SKPath = null;
                }

                // Recreate polyline annotation path
                if (Count > 0)
                {
                    _SKPath = new SKPath();
                    for (int index = 0; index < Count; index++)
                    {
                        var p = new SKPoint(this[index].X, this[index].Y);
                        if (index == 0)
                        {
                            _SKPath.MoveTo(p);
                        }
                        else
                        {
                            _SKPath.LineTo(p);
                        }
                    }
                    _SKPath.Close();
                }
                else
                {
                    _SKPath = new SKPath();
                }

                // Invalidate annotation
                annotation.SKPath = _SKPath;
                annotation.Invalidate();
            }
            base.Invalidate();
        }
 private void CreateRectangleContour()
 {
     // Прямоугольное сечение с начальными размерами
     if (contour != null)
     {
         contour.Dispose();
     }
     contour = new SKPath();
     contour.AddRect(new SKRect()
     {
         Left = 0, Right = 10, Bottom = 0, Top = 10
     });
 }
Пример #20
0
        void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    if (skPath != null)
                    {
                        skPath.Dispose();
                        skPath = null;
                    }
                }

                disposedValue = true;
            }
        }
Пример #21
0
        void ApplyTransform()
        {
            if (_path == null)
            {
                return;
            }

            if (_transformedPath != null)
            {
                _transformedPath.Dispose();
                _transformedPath = null;
            }

            if (!Transform.IsIdentity)
            {
                _transformedPath = new SKPath(_path);
                _transformedPath.Transform(Transform.ToSKMatrix());
            }
        }
Пример #22
0
        void ApplyTransform()
        {
            if (_path == null)
            {
                return;
            }

            if (_transformedPath != null)
            {
                _transformedPath.Dispose();
                _transformedPath = null;
            }

            // TODO: SkiaSharp does not expose Transform yet!!!
            //if (!Transform.IsIdentity)
            //{
            //	_transformedPath = new SKPath();
            //	_path.Transform(Transform.ToSKMatrix(), _transformedPath);
            //}
        }
Пример #23
0
        internal static SKPath Op(SKPathOp op, IList <SKPath> paths)
        {
            if (paths == null || paths.Count <= 0)
            {
                return(null);
            }

            if (paths.Count == 1)
            {
                using (var empty = new SKPath()
                {
                    FillType = paths[0].FillType
                })
                {
                    return(empty.Op(paths[0], op));
                }
            }
            else
            {
                var haveResult = false;
                var result     = new SKPath(paths[0])
                {
                    FillType = paths[0].FillType
                };

                for (int i = 1; i < paths.Count; i++)
                {
                    var next = result.Op(paths[i], op);
                    if (next != null)
                    {
                        result.Dispose();
                        result     = next;
                        haveResult = true;
                    }
                }

                return(haveResult ? result : null);
            }
        }
Пример #24
0
        private void DrawGraph(SKCanvas Canvas, SKPoint[] Points, object[] Parameters, SKPoint[] PrevPoints, object[] PrevParameters,
                               DrawingArea DrawingArea)
        {
            SKPaint Brush = null;
            SKPath  Path  = null;
            bool    First = true;

            try
            {
                Brush = new SKPaint()
                {
                    Style = SKPaintStyle.Fill,
                    Color = Graph.ToColor(Parameters[0])
                };
                Path = new SKPath();

                foreach (SKPoint Point in Points)
                {
                    if (First)
                    {
                        First = false;
                        Path.MoveTo(Point);
                    }
                    else
                    {
                        Path.LineTo(Point);
                    }
                }

                if (PrevPoints is null)
                {
                    IElement Zero;
                    ISet     Set = DrawingArea.MinY.AssociatedSet;

                    if (Set is IGroup Group)
                    {
                        Zero = Group.AdditiveIdentity;
                    }
                    else
                    {
                        Zero = new DoubleNumber(0);
                    }

                    IVector XAxis = VectorDefinition.Encapsulate(new IElement[] { DrawingArea.MinX, DrawingArea.MaxX }, false, this) as IVector;
                    IVector YAxis = VectorDefinition.Encapsulate(new IElement[] { Zero, Zero }, false, this) as IVector;

                    PrevPoints = DrawingArea.Scale(XAxis, YAxis);

                    if (DrawingArea.MinX is StringValue && DrawingArea.MaxX is StringValue)
                    {
                        PrevPoints[0].X = Points[0].X;
                        PrevPoints[1].X = Points[Points.Length - 1].X;
                    }
                }

                int i = PrevPoints.Length;

                while (--i >= 0)
                {
                    Path.LineTo(PrevPoints[i]);
                }

                Path.LineTo(Points[0]);

                Canvas.DrawPath(Path, Brush);
            }
            finally
            {
                if (Brush != null)
                {
                    Brush.Dispose();
                }

                if (Path != null)
                {
                    Path.Dispose();
                }
            }
        }
Пример #25
0
 /// <inheritdoc />
 public override void StartPath()
 {
     _path?.Dispose();
     _path = new SKPath();
 }
Пример #26
0
 public override void DisableLayerEffect()
 {
     _clipPath?.Dispose();
     _clipPath = null;
 }
Пример #27
0
 public void Dispose()
 {
     Geometry?.Dispose();
 }
Пример #28
0
        /// <summary>
        /// Draws 3D border.
        /// </summary>
        /// <param name="graph">Graphics to draw the border on.</param>
        /// <param name="borderSkin">Border skin object.</param>
        /// <param name="rect">Rectangle of the border.</param>
        /// <param name="backColor">Color of rectangle</param>
        /// <param name="backHatchStyle">Hatch style</param>
        /// <param name="backImage">Back Image</param>
        /// <param name="backImageWrapMode">Image mode</param>
        /// <param name="backImageTransparentColor">Image transparent color.</param>
        /// <param name="backImageAlign">Image alignment</param>
        /// <param name="backGradientStyle">Gradient type</param>
        /// <param name="backSecondaryColor">Gradient End Color</param>
        /// <param name="borderColor">Border Color</param>
        /// <param name="borderWidth">Border Width</param>
        /// <param name="borderDashStyle">Border Style</param>
        public virtual void DrawBorder(
            ChartGraphics graph,
            BorderSkin borderSkin,
            SKRect rect,
            SKColor backColor,
            ChartHatchStyle backHatchStyle,
            string backImage,
            ChartImageWrapMode backImageWrapMode,
            SKColor backImageTransparentColor,
            ChartImageAlignmentStyle backImageAlign,
            GradientStyle backGradientStyle,
            SKColor backSecondaryColor,
            SKColor borderColor,
            int borderWidth,
            ChartDashStyle borderDashStyle)
        {
            SKRect absolute = ChartGraphics.Round(rect);

            // Calculate shadow colors (0.2 - 0.6)
            float   colorDarkeningIndex = 0.2f + (0.4f * (borderSkin.PageColor.Red + borderSkin.PageColor.Green + borderSkin.PageColor.Blue) / 765f);
            SKColor shadowColor         = new(
                (byte)(borderSkin.PageColor.Red * colorDarkeningIndex),
                (byte)(borderSkin.PageColor.Green * colorDarkeningIndex),
                (byte)(borderSkin.PageColor.Blue * colorDarkeningIndex));

            if (borderSkin.PageColor == SKColors.Transparent)
            {
                shadowColor = new(0, 0, 0, 60);
            }

            colorDarkeningIndex += 0.2f;
            SKColor shadowLightColor = new(
                (byte)(borderSkin.PageColor.Red * colorDarkeningIndex),
                (byte)(borderSkin.PageColor.Green * colorDarkeningIndex),
                (byte)(borderSkin.PageColor.Blue * colorDarkeningIndex));

            // Calculate rounded rect radius
            float radius = defaultRadiusSize;

            radius = Math.Max(radius, 2f * resolution / 96.0f);
            radius = Math.Min(radius, rect.Width / 2f);
            radius = Math.Min(radius, rect.Height / 2f);
            radius = (float)Math.Ceiling(radius);

            // Fill page background color
            using (SKPaint brush = new() { Style = SKPaintStyle.Fill, Color = borderSkin.PageColor })
            {
                graph.FillRectangle(brush, rect);
            }

            // Top/Left shadow
            SKRect shadowRect = absolute;

            shadowRect.Right  -= radius * .3f;
            shadowRect.Bottom -= radius * .3f;
            graph.DrawRoundedRectShadowAbs(shadowRect, cornerRadius, radius + 1 * resolution / 96.0f, shadowLightColor, borderSkin.PageColor, 1.4f);

            // Bottom/Right shadow
            shadowRect         = absolute;
            shadowRect.Left    = absolute.Left + radius / 3f;
            shadowRect.Top     = absolute.Top + radius / 3f;
            shadowRect.Right  -= radius / 3.5f;
            shadowRect.Bottom -= radius / 3.5f;
            graph.DrawRoundedRectShadowAbs(shadowRect, cornerRadius, radius, shadowColor, borderSkin.PageColor, 1.3f);

            // Draw Background
            shadowRect         = absolute;
            shadowRect.Left    = absolute.Left + 3f * resolution / 96.0f;
            shadowRect.Top     = absolute.Top + 3f * resolution / 96.0f;
            shadowRect.Right  -= radius * .75f;
            shadowRect.Bottom -= radius * .75f;
            SKPath path = ChartGraphics.CreateRoundedRectPath(shadowRect, cornerRadius);

            graph.DrawPathAbs(
                path,
                backColor,
                backHatchStyle,
                backImage,
                backImageWrapMode,
                backImageTransparentColor,
                backImageAlign,
                backGradientStyle,
                backSecondaryColor,
                borderColor,
                borderWidth,
                borderDashStyle,
                PenAlignment.Inset);

            // Dispose Graphic path
            if (path != null)
            {
                path.Dispose();
            }

            // Bottom/Right inner shadow
            SKRegion innerShadowRegion = new(
                ChartGraphics.CreateRoundedRectPath(
                    new SKRect(
                        shadowRect.Left - radius,
                        shadowRect.Top - radius,
                        shadowRect.Width + radius - radius * 0.25f,
                        shadowRect.Height + radius - radius * 0.25f),
                    cornerRadius));

            innerShadowRegion.Op(ChartGraphics.CreateRoundedRectPath(shadowRect, cornerRadius), SKRegionOperation.Difference);
            graph.Clip = innerShadowRegion;
            graph.DrawRoundedRectShadowAbs(
                shadowRect,
                cornerRadius,
                radius,
                SKColors.Transparent,
                new(SKColors.Gray.Red, SKColors.Gray.Green, SKColors.Gray.Blue, 128),
                .5f);
            graph.Clip = new();
        }
Пример #29
0
 public void Dispose()
 {
     _path.Dispose();
 }
Пример #30
0
        private SKImage GenerateGauge(HttpRequestHeader Header)
        {
            if (!Header.TryGetQueryParameter("Width", out string s) || !int.TryParse(s, out int Width))
            {
                Width = 480;
            }
            else if (Width <= 0)
            {
                throw new BadRequestException();
            }

            if (!Header.TryGetQueryParameter("Height", out s) || !int.TryParse(s, out int Height))
            {
                Height = 300;
            }
            else if (Width <= 0)
            {
                throw new BadRequestException();
            }

            using (SKSurface Surface = SKSurface.Create(Width, Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul))
            {
                SKCanvas Canvas = Surface.Canvas;
                Canvas.Clear(SKColors.White);

                float    NeedleX0     = Width * 0.5f;
                float    NeedleY0     = Height * 0.9f;
                float    OuterRadius  = (float)Math.Min(Height * 0.8, Width * 0.4);
                float    LabelRadius  = OuterRadius * 1.01f;
                float    InnerRadius  = OuterRadius * 0.6f;
                float    NeedleRadius = OuterRadius * 0.95f;
                float    NutRadius    = OuterRadius * 0.05f;
                SKRect   Rect;
                SKPath   Path     = new SKPath();
                SKShader Gradient = SKShader.CreateSweepGradient(new SKPoint(NeedleX0, NeedleY0),
                                                                 new SKColor[] { (lastMotion.HasValue && lastMotion.Value ? SKColors.Green : SKColors.Black), SKColors.White },
                                                                 new float[] { 0, 1 });
                SKPaint GaugeBackground = new SKPaint()
                {
                    IsAntialias = true,
                    Style       = SKPaintStyle.Fill,
                    Shader      = Gradient
                };
                SKPaint GaugeOutline = new SKPaint()
                {
                    IsAntialias = true,
                    Style       = SKPaintStyle.Stroke,
                    Color       = SKColors.Black
                };

                Rect = new SKRect(NeedleX0 - OuterRadius, NeedleY0 - OuterRadius, NeedleX0 + OuterRadius, NeedleY0 + OuterRadius);
                Path.ArcTo(Rect, -180, 180, true);

                Rect = new SKRect(NeedleX0 - InnerRadius, NeedleY0 - InnerRadius, NeedleX0 + InnerRadius, NeedleY0 + InnerRadius);
                Path.ArcTo(Rect, 0, -180, false);
                Path.Close();

                Canvas.DrawPath(Path, GaugeBackground);
                Canvas.DrawPath(Path, GaugeOutline);

                GaugeBackground.Dispose();
                GaugeOutline.Dispose();
                Gradient.Dispose();
                Path.Dispose();

                SKPaint Font = new SKPaint()
                {
                    IsAntialias  = true,
                    Color        = SKColors.Black,
                    HintingLevel = SKPaintHinting.Full,
                    TextSize     = Height * 0.05f
                };

                SKPaint Needle = new SKPaint()
                {
                    IsAntialias = true,
                    Color       = SKColors.Black,
                    Style       = SKPaintStyle.Fill
                };

                Font.GetFontMetrics(out SKFontMetrics FontMetrics);
                float TextHeight = FontMetrics.Descent - FontMetrics.Ascent;
                float TextWidth;

                for (int i = 0; i <= 100; i += 10)
                {
                    s         = i.ToString() + "%";
                    TextWidth = Font.MeasureText(s);

                    float LabelDeg = -90 + i * 1.8f;
                    float LabelRad = (float)(LabelDeg * Math.PI / 180);
                    float LabelX   = (float)(LabelRadius * Math.Sin(LabelRad) + NeedleX0);
                    float LabelY   = (float)(NeedleY0 - LabelRadius * Math.Cos(LabelRad));
                    float OuterX   = (float)(OuterRadius * Math.Sin(LabelRad) + NeedleX0);
                    float OuterY   = (float)(NeedleY0 - OuterRadius * Math.Cos(LabelRad));
                    float NeedleX1 = (float)(NeedleRadius * Math.Sin(LabelRad) + NeedleX0);
                    float NeedleY1 = (float)(NeedleY0 - NeedleRadius * Math.Cos(LabelRad));

                    Canvas.DrawLine(OuterX, OuterY, NeedleX1, NeedleY1, Needle);

                    Canvas.Translate(LabelX, LabelY);
                    Canvas.RotateDegrees(LabelDeg);
                    Canvas.Translate(-TextWidth * 0.5f, -TextHeight * 0.5f);
                    Canvas.DrawText(s, 0, 0, Font);
                    Canvas.ResetMatrix();
                }

                if (this.lastLight.HasValue)
                {
                    float  AngleDeg = (float)(this.lastLight.Value - 50) * 90.0f / 50;
                    double AngleRad = AngleDeg * Math.PI / 180;
                    float  NeedleX1 = (float)(NeedleRadius * Math.Sin(AngleRad) + NeedleX0);
                    float  NeedleY1 = (float)(NeedleY0 - NeedleRadius * Math.Cos(AngleRad));

                    Path = new SKPath();
                    Rect = new SKRect(NeedleX0 - NutRadius, NeedleY0 - NutRadius, NeedleX0 + NutRadius, NeedleY0 + NutRadius);
                    Path.ArcTo(Rect, AngleDeg - 180, -180, true);

                    Path.LineTo(NeedleX1, NeedleY1);
                    Path.Close();

                    Canvas.DrawPath(Path, Needle);

                    Path.Dispose();
                    Needle.Dispose();
                }

                return(Surface.Snapshot());
            }
        }