Exemplo n.º 1
0
        public void Example1()
        {
            var filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\shannon.webp");

            // Use SkiaSharp to load a WEBP image:
            var bmp = MemoryBitmap.Load(filePath, Codecs.SkiaCodec.Default);

            // Use OpenCV to resize the image:

            /*
             * bmp = bmp
             *  .WithOpenCv()
             *  .ToResizedMemoryBitmap(50, 50, OpenCvSharp.InterpolationFlags.Lanczos4);
             */

            using (var proxy = bmp.UsingOpenCv())
            {
                proxy.Mat.Circle(new OpenCvSharp.Point(150, 150), 35, OpenCvSharp.Scalar.Red, 10);

                // proxy.Mat.Blur(new OpenCvSharp.Size(4, 4));
                // OpenCvSharp.Cv2.Blur(proxy.Mat, proxy.Mat, new OpenCvSharp.Size(4, 4));
            }

            using (var proxy = bmp.UsingGDI())
            {
                // Use GDI to draw a triangle:
                var a = new System.Drawing.Point(5, 5);
                var b = new System.Drawing.Point(50, 50);
                var c = new System.Drawing.Point(5, 50);

                proxy.Canvas.DrawPolygon(System.Drawing.Pens.Red, new[] { a, b, c });
            }

            using (var proxy = bmp.UsingImageSharp())
            {
                proxy.Image.Mutate(ipc => ipc.DrawPolygon(SixLabors.ImageSharp.Color.Blue, 2, (50, 5), (50, 50), (5, 5)));
            }

            using (var proxy = bmp.UsingSkiaSharp())
            {
                var p0 = new SkiaSharp.SKPoint(5, 25);
                var p1 = new SkiaSharp.SKPoint(45, 25);

                using var skiaPaint = new SkiaSharp.SKPaint
                      {
                          TextSize    = 64.0f,
                          IsAntialias = true,
                          StrokeWidth = 20,
                          Color       = new SkiaSharp.SKColor(0, 255, 0),
                          Style       = SkiaSharp.SKPaintStyle.Fill
                      };

                proxy.Canvas.DrawLine(p0, p1, skiaPaint);
                proxy.Canvas.DrawText("SkiaSharp", new SkiaSharp.SKPoint(5, 200), skiaPaint);
            }

            // Use Imagesharp to save to PNG
            bmp.Save(AttachmentInfo.From("shannon.png"));
        }
Exemplo n.º 2
0
        public static SkiaSharp.SKPoint[] ToSKPoints(this IList <SKPoint> points)
        {
            var skPoints = new SkiaSharp.SKPoint[points.Count];

            for (var i = 0; i < points.Count; i++)
            {
                skPoints[i] = points[i].ToSKPoint();
            }

            return(skPoints);
        }
 /// <summary>
 /// Sets a custom position for the description text in pixels on the screen.
 /// </summary>
 /// <param name="x">xcoordinate</param>
 /// <param name="y">ycoordinate</param>
 public void SetPosition(float x, float y)
 {
     if (position == null)
     {
         position = new Point(x, y);
     }
     else
     {
         position.X = x;
         position.Y = y;
     }
 }
Exemplo n.º 4
0
        protected float CalculateMinimumRadiusForSpacedSlice(
            Point center,
            float radius,
            float angle,
            float arcStartPointX,
            float arcStartPointY,
            float startAngle,
            float sweepAngle)
        {
            float angleMiddle = startAngle + sweepAngle / 2.0f;

            // Other point of the arc
            float arcEndPointX = center.X + radius * (float)Math.Cos((startAngle + sweepAngle) * ChartUtil.FDegToRad);
            float arcEndPointY = center.Y + radius * (float)Math.Sin((startAngle + sweepAngle) * ChartUtil.FDegToRad);

            // Middle point on the arc
            float arcMidPointX = center.X + radius * (float)Math.Cos(angleMiddle * ChartUtil.FDegToRad);
            float arcMidPointY = center.Y + radius * (float)Math.Sin(angleMiddle * ChartUtil.FDegToRad);

            // This is the base of the contained triangle
            double basePointsDistance = Math.Sqrt(
                Math.Pow(arcEndPointX - arcStartPointX, 2) +
                Math.Pow(arcEndPointY - arcStartPointY, 2));

            // After reducing space from both sides of the "slice",
            //   the angle of the contained triangle should stay the same.
            // So let's find out the height of that triangle.
            float containedTriangleHeight = (float)(basePointsDistance / 2.0 *
                                                    Math.Tan((180.0 - angle) / 2.0 * ChartUtil.DegToRad));

            // Now we subtract that from the radius
            float spacedRadius = radius - containedTriangleHeight;

            // And now subtract the height of the arc that's between the triangle and the outer circle
            spacedRadius -= MathF.Sqrt(
                MathF.Pow(arcMidPointX - (arcEndPointX + arcStartPointX) / 2.0f, 2) +
                MathF.Pow(arcMidPointY - (arcEndPointY + arcStartPointY) / 2.0f, 2));

            return(spacedRadius);
        }
Exemplo n.º 5
0
 private void _joystick_TiltChanged(SkiaSharp.SKPoint tilt)
 {
 }
Exemplo n.º 6
0
        public void DrawPrimitivesWithMultipleAdapters()
        {
            void _drawUsingMultipleDevices(SpanBitmap <Pixel.BGRA32> img)
            {
                var slice = img.Slice((10, 10, img.Width - 20, img.Height - 20)); // crop the source image with a 10 pixels margin

                // cast to OpenCV adapter to blur and find edges in the image.
                slice
                .WithOpenCv()
                .ApplyBlur((4, 4))
                .ApplyCanny(100, 200);

                // cast to GDI Adapter to draw primitives.
                slice.MutateAsGDI(dc =>
                {
                    var a = new System.Drawing.Point(5, 5);
                    var b = new System.Drawing.Point(50, 50);
                    var c = new System.Drawing.Point(5, 50);

                    var f = new System.Drawing.Font("arial", 30);

                    // draw a triangle
                    dc.DrawPolygon(System.Drawing.Pens.Yellow, new[] { a, b, c });
                    // draw text
                    dc.DrawString("GDI Text", f, System.Drawing.Brushes.Yellow, new System.Drawing.PointF(5, 60));
                });

                // cast to SkiaSharp Adapter to draw primitives.
                slice
                .WithSkiaSharp()
                .Draw
                    (canvas =>
                {
                    var p0 = new SkiaSharp.SKPoint(5, 120);
                    var p1 = new SkiaSharp.SKPoint(250, 120);

                    using var skiaPaint = new SkiaSharp.SKPaint
                          {
                              TextSize    = 64.0f,
                              IsAntialias = true,
                              StrokeWidth = 20,
                              Color       = new SkiaSharp.SKColor(0, 0, 255),
                              Style       = SkiaSharp.SKPaintStyle.Fill
                          };

                    canvas.DrawLine(p0, p1, skiaPaint);
                    canvas.DrawText("SkiaSharp Text", new SkiaSharp.SKPoint(5, 200), skiaPaint);
                });

                // cast to imagesharp Adapter to draw primitives
                slice.MutateAsImageSharp(ipc =>
                {
                    ipc.FillPolygon(SixLabors.ImageSharp.Color.Green, (5, 250), (50, 250), (5, 300));
                    ipc.DrawText("ImageSharp Text", SixLabors.Fonts.SystemFonts.CreateFont("Arial", 40), SixLabors.ImageSharp.Color.Green, new SixLabors.ImageSharp.PointF(80, 250));
                });

                // wpf

                /* not working yet
                 * slice
                 *  .WithWPF()
                 *  .Draw
                 *  (dc =>
                 *  {
                 *      dc.DrawEllipse(System.Windows.Media.Brushes.Red, null, new System.Windows.Point(5, 5), 10, 10);
                 *      dc.DrawEllipse(System.Windows.Media.Brushes.Blue, null, new System.Windows.Point(50, 50), 100, 100);
                 *  }
                 *  );*/
            }

            // load an image with Sixlabors Imagesharp, notice we use BGRA32 because RGBA32 is NOT supported by GDI.
            var img = SixLabors.ImageSharp.Image.Load <SixLabors.ImageSharp.PixelFormats.Bgra32>(ResourceInfo.From("shannon.jpg"));

            // render using multiple devices

            img.WriteAsSpanBitmap(self => _drawUsingMultipleDevices(self));

            // save the result back with ImageSharp

            AttachmentInfo
            .From("result.jpg")
            .WriteObject(f => img.Save(f));

            img.Dispose();
        }
Exemplo n.º 7
0
 //------------------------------------------------
 public static PixelFarm.Drawing.Point ToPoint(this SkiaSharp.SKPoint pointf)
 {
     return(new PixelFarm.Drawing.Point((int)pointf.X, (int)pointf.Y));
 }
Exemplo n.º 8
0
 public static PixelFarm.Drawing.PointF ToPointF(this SkiaSharp.SKPoint pointf)
 {
     return(new PixelFarm.Drawing.PointF(pointf.X, pointf.Y));
 }
Exemplo n.º 9
0
 public static System.Windows.Point ToPoint(this SkiaSharp.SKPoint point)
 {
 }