public static void RenderRaster(SKCanvas canvas, SKBitmap bitmap, SKRect rect, float opacity = 1f) { // Better for quality. Helps to compare to WPF var color = new SKColor(255, 255, 255, (byte)(255 * opacity)); var paint = new SKPaint { Color = color, FilterQuality = SKFilterQuality.High }; canvas.DrawBitmap(bitmap, rect, paint); // Better for performance: canvas.DrawBitmap(bitmap, rect); }
public static void Draw(SKCanvas canvas, IViewport viewport, IStyle style, IGeometry geometry) { var lineString = ((LineString) geometry).Vertices; float lineWidth = 1; var lineColor = new Color(); var vectorStyle = style as VectorStyle; if (vectorStyle != null) { lineWidth = (float) vectorStyle.Line.Width; lineColor = vectorStyle.Line.Color; } var line = WorldToScreen(viewport, lineString); var path = ToSkia(line); using (var paint = new SKPaint()) { paint.IsStroke = true; paint.StrokeWidth = lineWidth; paint.Color = lineColor.ToSkia(); paint.StrokeJoin = SKStrokeJoin.Round; canvas.DrawPath(path, paint); } }
private void _skiaView_PaintSurface(object sender, SKPaintGLSurfaceEventArgs e) { Stopwatch renderStopWatch = null; if (RenderOptions.ShowClipRects) { renderStopWatch = Stopwatch.StartNew(); } e.Surface.Canvas.Clear(new SKColor(Container.BackColor.R, Container.BackColor.G, Container.BackColor.B, Container.BackColor.A)); _root.Measure(new Primitives.Size(Container.ClientSize.Width, Container.ClientSize.Height)); _root.Arrange(new Primitives.Rect(0, 0, Container.ClientSize.Width, Container.ClientSize.Height)); _root.Render(e.Surface.Canvas); if (renderStopWatch != null) { renderStopWatch.Stop(); using (var ptRect = new SkiaSharp.SKPaint() .ApplyBrush(new SolidColorBrush(new Color(0, 0, 0)))) using (var ptText = new SkiaSharp.SKPaint() .ApplyBrush(new SolidColorBrush(new Color(255, 255, 255)))) { e.Surface.Canvas.DrawRect(Container.ClientSize.Width - 50.0f, 0.0f, 100.0f, 10.0f, ptRect); var elapsedString = renderStopWatch.ElapsedMilliseconds == 0 ? "-" : $"{(1.0 / renderStopWatch.ElapsedMilliseconds * 1000).ToString("##.00", CultureInfo.InvariantCulture)}FPS"; e.Surface.Canvas.DrawText(elapsedString, new SKPoint(Container.ClientSize.Width - 50.0f, 10.0f), ptText); } renderStopWatch.Reset(); } }
public static void Draw(SKCanvas canvas, IViewport viewport, IStyle style, IGeometry geometry) { var polygon = (Polygon)geometry; float lineWidth = 1; var lineColor = Color.Black; // default var fillColor = Color.Gray; // default var vectorStyle = style as VectorStyle; if (vectorStyle != null) { lineWidth = (float) vectorStyle.Outline.Width; lineColor = vectorStyle.Outline.Color; fillColor = vectorStyle.Fill?.Color; } using (var path = ToSkia(viewport, polygon)) { using (var paint = new SKPaint()) { paint.IsAntialias = true; paint.StrokeWidth = lineWidth; paint.Style = SKPaintStyle.Fill; paint.Color = fillColor.ToSkia(); canvas.DrawPath(path, paint); paint.Style = SKPaintStyle.Stroke; paint.Color = lineColor.ToSkia(); canvas.DrawPath(path, paint); } } }
void BoundingBox_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e) { if (ViewModel.lastPrediction?.BoundingBox == null) { return; } var boundingBox = ViewModel.lastPrediction.BoundingBox; e.Surface.Canvas.Clear(); var SKSkiaView = sender as SKCanvasView; double scaleFactor = e.Info.Width / SKSkiaView.Width; float width = (float)e.Info.Width; float height = (float)e.Info.Height; float lineStrokeWidth = 2 * (float)scaleFactor; float boxX = (float)((width * boundingBox.Left)); float boxY = (float)((height * boundingBox.Top)); float boxWidth = (float)((width * boundingBox.Width)); float boxHeight = (float)((height * boundingBox.Height)); var paint = new SkiaSharp.SKPaint() { Style = SkiaSharp.SKPaintStyle.Stroke, StrokeWidth = lineStrokeWidth, Color = SkiaSharp.SKColors.White, StrokeCap = SKStrokeCap.Round, }; e.Surface.Canvas.DrawRect(boxX, boxY, boxWidth, boxHeight, paint); }
public FormattedTextImpl(string text, string fontFamilyName, double fontSize, FontStyle fontStyle, TextAlignment textAlignment, FontWeight fontWeight, TextWrapping wrapping) { _text = text ?? string.Empty; // Replace 0 characters with zero-width spaces (200B) _text = _text.Replace((char)0, (char)0x200B); var typeface = TypefaceCache.GetTypeface(fontFamilyName, fontStyle, fontWeight); _paint = new SKPaint(); //currently Skia does not measure properly with Utf8 !!! //Paint.TextEncoding = SKTextEncoding.Utf8; _paint.TextEncoding = SKTextEncoding.Utf16; _paint.IsStroke = false; _paint.IsAntialias = true; _paint.LcdRenderText = true; _paint.SubpixelText = true; _paint.Typeface = typeface; _paint.TextSize = (float)fontSize; _paint.TextAlign = textAlignment.ToSKTextAlign(); _wrapping = wrapping; Rebuild(); }
//----------------------- public SkiaCanvasPainter(int w, int h) { _fill = new SKPaint(); _stroke = new SKPaint(); _stroke.IsStroke = true; _width = w; _height = h; }
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 (); } }
public void DrawImage(IBitmap source, double opacity, Rect sourceRect, Rect destRect) { var impl = (BitmapImpl)source.PlatformImpl; var s = sourceRect.ToSKRect(); var d = destRect.ToSKRect(); using (var paint = new SKPaint() { Color = new SKColor(255, 255, 255, (byte)(255 * opacity)) }) { Canvas.DrawBitmap(impl.Bitmap, s, d, paint); } }
private static SKBitmap CreateLabelAsBitmap(LabelStyle style, string text, SKPaint paint) { var rect = new SKRect(); paint.MeasureText(text, ref rect); var backRect = new SKRect(0, 0, rect.Width + 6, rect.Height + 6); var bitmap = new SKBitmap((int)backRect.Width, (int)backRect.Height); using (var target = new SKCanvas(bitmap)) { target.Clear(); DrawBackground(style, backRect, target); target.DrawText(text, -rect.Left + 3, -rect.Top +3, paint); return bitmap; } }
protected override void Draw(SKCanvas canvas, int width, int height) { float time = _frame / (LengthSec * Fps); using (var paint = new SKPaint()) { paint.Color = Foreground.ToSkia(); paint.StrokeWidth = (float)StrokeWidth; paint.IsAntialias = true; paint.IsStroke = true; for (int a = 0; a < 3; ++a) { var matrix = SKMatrix.MakeRotation(2 * (float)Math.PI * time / 6 + 2 * (float)Math.PI * a / 3); matrix.TransX = width / 2f; matrix.TransY = height / 2f; canvas.SetMatrix(matrix); const int n = 12; const int sp = 39; for (int i = -n; i <= n; ++i) { float y = (float)(i * sp * Math.Pow(2, time)); float tt = (float)Math.Min(1, Math.Max(0, 1.09 * time - 0.00275 * Math.Abs(y) + 0.075)); float x; if (i % 2 == 0) x = width; else x = width * tt; if (x > 0) canvas.DrawLine(-x, y, x, y, paint); } } } ++_frame; if (_frame > LengthSec * Fps) _frame = 0; }
private void _skiaView_PaintSurface(object sender, SKPaintSurfaceEventArgs e) { Stopwatch renderStopWatch = null; if (RenderOptions.ShowFrameRate) { renderStopWatch = Stopwatch.StartNew(); } //e.Surface.Canvas.Clear(new SKColor(Container.BackgroundColor.R, Container.BackgroundColor.G, Container.BackgroundColor.B, Container.BackgroundColor.A)); e.Surface.Canvas.Clear(SKColors.White); using (new SKAutoCanvasRestore(e.Surface.Canvas)) { var deviceSize = new Primitives.Size(e.Info.Width, e.Info.Height); var deviceIndSize = new Primitives.Size(_skiaView.Width, _skiaView.Height); var scaleDpi = new Primitives.Point(e.Info.Width / _skiaView.Width, e.Info.Height / _skiaView.Height); e.Surface.Canvas.Scale((float)scaleDpi.X, (float)scaleDpi.Y); _root.Measure(deviceIndSize); _root.Arrange(new Primitives.Rect(0, 0, deviceIndSize.Width, deviceIndSize.Height)); _root.Render(e.Surface.Canvas); } if (renderStopWatch != null) { renderStopWatch.Stop(); using (var ptRect = new SkiaSharp.SKPaint() .ApplyBrush(new SolidColorBrush(new Primitives.Color(0, 0, 0)))) using (var ptText = new SkiaSharp.SKPaint() .ApplyBrush(new SolidColorBrush(new Primitives.Color(255, 255, 255)))) { e.Surface.Canvas.DrawRect(e.Info.Width - 50.0f, 0.0f, 100.0f, 10.0f, ptRect); var elapsedString = renderStopWatch.ElapsedMilliseconds == 0 ? "-" : $"{(1.0 / renderStopWatch.ElapsedMilliseconds * 1000).ToString("##.00", CultureInfo.InvariantCulture)}FPS"; e.Surface.Canvas.DrawText(elapsedString, new SKPoint(e.Info.Width - 50.0f, 10.0f), ptText); } renderStopWatch.Reset(); } }
void CreateGraphicsFromNativeHdc(int width, int height) { skBitmap = new SKBitmap(width, height); skCanvas = new SKCanvas(skBitmap); // stroke = new SKPaint(); stroke.IsStroke = true; // fill = new SKPaint(); fill.IsStroke = false; // textFill = new SKPaint(); textFill.IsAntialias = true; //--------------------------------------- //--------------------------------------- this.CurrentFont = new RequestFont("tahoma", 14); this.CurrentTextColor = Color.Black; //--------------------------------------- }
static void Main(string[] args) { #region skiasharp var surface = SkiaSharp.SKSurface.Create(new SkiaSharp.SKImageInfo(100, 100)); var canvas = surface.Canvas; canvas.Clear(SkiaSharp.SKColors.Transparent); var paint = new SkiaSharp.SKPaint { Color = SkiaSharp.SKColors.Blue }; canvas.DrawCircle(50, 50, 45, paint); var image = surface.Snapshot(); var data = image.Encode(SkiaSharp.SKEncodedImageFormat.Png, 100); var base64 = Convert.ToBase64String(data.AsSpan().ToArray()); Console.WriteLine("data:image/png;base64," + base64); #endregion }
/// <summary> /// Combines 2 images into one, given the shared ImageStats for both supplied images. /// </summary> /// <param name="info">the ImageStats object used to generate both bottom and top tiles.</param> /// <param name="bottomTile">the tile to use as the base of the image. Expected to be opaque.</param> /// <param name="topTile">The tile to layer on top. Expected to be at least partly transparent or translucent.</param> /// <returns></returns> public byte[] LayerTiles(ImageStats info, byte[] bottomTile, byte[] topTile) { SkiaSharp.SKBitmap bitmap = new SkiaSharp.SKBitmap(info.imageSizeX, info.imageSizeY, SkiaSharp.SKColorType.Rgba8888, SkiaSharp.SKAlphaType.Premul); SkiaSharp.SKCanvas canvas = new SkiaSharp.SKCanvas(bitmap); SkiaSharp.SKPaint paint = new SkiaSharp.SKPaint(); canvas.Scale(1, 1, info.imageSizeX / 2, info.imageSizeY / 2); paint.IsAntialias = true; var baseBmp = SkiaSharp.SKBitmap.Decode(bottomTile); var topBmp = SkiaSharp.SKBitmap.Decode(topTile); canvas.DrawBitmap(baseBmp, 0, 0); canvas.DrawBitmap(topBmp, 0, 0); var ms = new MemoryStream(); var skms = new SkiaSharp.SKManagedWStream(ms); bitmap.Encode(skms, SkiaSharp.SKEncodedImageFormat.Png, 100); var output = ms.ToArray(); skms.Dispose(); ms.Close(); ms.Dispose(); return(output); }
protected sealed override void RenderCore(RenderContext context) { using (new SKAutoCanvasRestore(context.Canvas)) { context.Canvas.Translate((int)_visualOffset.X, (int)_visualOffset.Y); context.Canvas.ApplyTransform(Transform, RenderSize); context.Canvas.ClipRect(new SkiaSharp.SKRect(0.0f, 0.0f, (float)RenderSize.Width, (float)RenderSize.Height)); RenderOverride(context); if (RenderOptions.ShowClipRects) { var pt = new SkiaSharp.SKPaint() .IsStroke() .ApplyBrush(new SolidColorBrush(new Color(255, 0, 0)), 1.0f); context.Canvas.DrawRect(new SkiaSharp.SKRect(0.5f, 0.5f, (float)RenderSize.Width - 0.5f, (float)RenderSize.Height - 0.5f), pt); } } base.RenderCore(context); }
public int SaveLayer(SKPaint paint) { return SkiaApi.sk_canvas_save_layer (Handle, IntPtr.Zero, paint == null ? IntPtr.Zero : paint.Handle); }
public void DrawText(string text, SKPath path, float hOffset, float vOffset, SKPaint paint) { if (text == null) throw new ArgumentNullException ("text"); if (paint == null) throw new ArgumentNullException (nameof (paint)); if (paint == null) throw new ArgumentNullException (nameof (paint)); var bytes = Util.GetEncodedText (text, paint.TextEncoding); SkiaApi.sk_canvas_draw_text_on_path (Handle, bytes, bytes.Length, path.Handle, hOffset, vOffset, paint.Handle); }
public void DrawText(IntPtr buffer, int length, SKPoint[] points, SKPaint paint) { if (buffer == IntPtr.Zero) throw new ArgumentNullException ("buffer"); if (paint == null) throw new ArgumentNullException (nameof (paint)); if (points == null) throw new ArgumentNullException ("points"); SkiaApi.sk_canvas_draw_pos_text (Handle, buffer, length, points, paint.Handle); }
public void DrawText(IntPtr buffer, int length, float x, float y, SKPaint paint) { if (buffer == IntPtr.Zero) throw new ArgumentNullException ("buffer"); if (paint == null) throw new ArgumentNullException (nameof (paint)); SkiaApi.sk_canvas_draw_text (Handle, buffer, length, x, y, paint.Handle); }
public void DrawText(IntPtr buffer, int length, SKPath path, float hOffset, float vOffset, SKPaint paint) { if (buffer == IntPtr.Zero) throw new ArgumentNullException ("buffer"); if (paint == null) throw new ArgumentNullException (nameof (paint)); if (paint == null) throw new ArgumentNullException (nameof (paint)); SkiaApi.sk_canvas_draw_text_on_path (Handle, buffer, length, path.Handle, hOffset, vOffset, paint.Handle); }
public void DrawText(string text, SKPoint [] points, SKPaint paint) { if (text == null) throw new ArgumentNullException ("text"); if (paint == null) throw new ArgumentNullException (nameof (paint)); if (points == null) throw new ArgumentNullException ("points"); var bytes = Util.GetEncodedText (text, paint.TextEncoding); SkiaApi.sk_canvas_draw_pos_text (Handle, bytes, bytes.Length, points, paint.Handle); }
public void DrawRoundRect(SKRect rect, float rx, float ry, SKPaint paint) { if (paint == null) throw new ArgumentNullException (nameof (paint)); SkiaApi.sk_canvas_draw_round_rect (Handle, ref rect, rx, ry, paint.Handle); }
public void DrawPoints(SKPointMode mode, SKPoint [] points, SKPaint paint) { if (paint == null) throw new ArgumentNullException (nameof (paint)); if (points == null) throw new ArgumentNullException (nameof (points)); SkiaApi.sk_canvas_draw_points (Handle, mode, (IntPtr)points.Length, points, paint.Handle); }
public void DrawPoint(float x, float y, SKPaint paint) { if (paint == null) throw new ArgumentNullException (nameof (paint)); SkiaApi.sk_canvas_draw_point (Handle, x, y, paint.Handle); }
public void DrawPath(SKPath path, SKPaint paint) { if (paint == null) throw new ArgumentNullException (nameof (paint)); if (path == null) throw new ArgumentNullException (nameof (path)); SkiaApi.sk_canvas_draw_path (Handle, path.Handle, paint.Handle); }
public IDisposable ApplyTo(SKPaint paint) { var state = new PaintState(paint, paint.Color, paint.Shader); paint.Color = Paint.Color; paint.Shader = Paint.Shader; return state; }
public PaintWrapper(SKPaint paint) { Paint = paint; _disposable1 = null; }
internal PaintWrapper CreatePaint(IBrush brush, Size targetSize) { SKPaint paint = new SKPaint(); var rv = new PaintWrapper(paint); paint.IsStroke = false; // TODO: SkiaSharp does not contain alpha yet! double opacity = brush.Opacity * _currentOpacity; //paint.SetAlpha(paint.GetAlpha() * opacity); paint.IsAntialias = true; SKColor color = new SKColor(255, 255, 255, 255); var solid = brush as ISolidColorBrush; if (solid != null) color = solid.Color.ToSKColor(); paint.Color = (new SKColor(color.Red, color.Green, color.Blue, (byte)(color.Alpha * opacity))); if (solid != null) { return rv; } var gradient = brush as GradientBrush; if (gradient != null) { var tileMode = gradient.SpreadMethod.ToSKShaderTileMode(); var stopColors = gradient.GradientStops.Select(s => s.Color.ToSKColor()).ToArray(); var stopOffsets = gradient.GradientStops.Select(s => (float)s.Offset).ToArray(); var linearGradient = brush as LinearGradientBrush; if (linearGradient != null) { var start = linearGradient.StartPoint.ToPixels(targetSize).ToSKPoint(); var end = linearGradient.EndPoint.ToPixels(targetSize).ToSKPoint(); // would be nice to cache these shaders possibly? var shader = SKShader.CreateLinearGradient(start, end, stopColors, stopOffsets, tileMode); paint.Shader = shader; shader.Dispose(); } else { var radialGradient = brush as RadialGradientBrush; if (radialGradient != null) { var center = radialGradient.Center.ToPixels(targetSize).ToSKPoint(); var radius = (float)radialGradient.Radius; // TODO: There is no SetAlpha in SkiaSharp //paint.setAlpha(128); // would be nice to cache these shaders possibly? var shader = SKShader.CreateRadialGradient(center, radius, stopColors, stopOffsets, tileMode); paint.Shader = shader; shader.Dispose(); } } return rv; } var tileBrush = brush as TileBrush; if (tileBrush != null) { var helper = new TileBrushImplHelper(tileBrush, targetSize); var bitmap = new BitmapImpl((int)helper.IntermediateSize.Width, (int)helper.IntermediateSize.Height); rv.AddDisposable(bitmap); using (var ctx = bitmap.CreateDrawingContext()) helper.DrawIntermediate(ctx); SKMatrix translation = SKMatrix.MakeTranslation(-(float)helper.DestinationRect.X, -(float)helper.DestinationRect.Y); SKShaderTileMode tileX = tileBrush.TileMode == TileMode.None ? SKShaderTileMode.Clamp : tileBrush.TileMode == TileMode.FlipX || tileBrush.TileMode == TileMode.FlipXY ? SKShaderTileMode.Mirror : SKShaderTileMode.Repeat; SKShaderTileMode tileY = tileBrush.TileMode == TileMode.None ? SKShaderTileMode.Clamp : tileBrush.TileMode == TileMode.FlipY || tileBrush.TileMode == TileMode.FlipXY ? SKShaderTileMode.Mirror : SKShaderTileMode.Repeat; paint.Shader = SKShader.CreateBitmap(bitmap.Bitmap, tileX, tileY, translation); paint.Shader.Dispose(); } return rv; }
public void DrawLine(float x0, float y0, float x1, float y1, SKPaint paint) { if (paint == null) throw new ArgumentNullException (nameof (paint)); SkiaApi.sk_canvas_draw_line (Handle, x0, y0, x1, y1, paint.Handle); }
public PaintState(SKPaint paint, SKColor color, SKShader shader) { _paint = paint; _color = color; _shader = shader; }
public void DrawOval(SKRect rect, SKPaint paint) { if (paint == null) throw new ArgumentNullException (nameof (paint)); SkiaApi.sk_canvas_draw_oval (Handle, ref rect, paint.Handle); }
private static int LineBreak(string textInput, int textIndex, int stop, SKPaint paint, float maxWidth, out int trailingCount) { int lengthBreak; if (maxWidth == -1) { lengthBreak = stop - textIndex; } else { float measuredWidth; string subText = textInput.Substring(textIndex, stop - textIndex); lengthBreak = (int)paint.BreakText(subText, maxWidth, out measuredWidth) / 2; } //Check for white space or line breakers before the lengthBreak int startIndex = textIndex; int index = textIndex; int word_start = textIndex; bool prevBreak = true; trailingCount = 0; while (index < stop) { int prevText = index; char currChar = textInput[index++]; bool currBreak = IsBreakChar(currChar); if (!currBreak && prevBreak) { word_start = prevText; } prevBreak = currBreak; if (index > startIndex + lengthBreak) { if (currBreak) { // eat the rest of the whitespace while (index < stop && IsBreakChar(textInput[index])) { index++; } trailingCount = index - prevText; } else { // backup until a whitespace (or 1 char) if (word_start == startIndex) { if (prevText > startIndex) { index = prevText; } } else { index = word_start; } } break; } if ('\n' == currChar) { int ret = index - startIndex; int lineBreakSize = 1; if (index < stop) { currChar = textInput[index++]; if ('\r' == currChar) { ret = index - startIndex; ++lineBreakSize; } } trailingCount = lineBreakSize; return ret; } if ('\r' == currChar) { int ret = index - startIndex; int lineBreakSize = 1; if (index < stop) { currChar = textInput[index++]; if ('\n' == currChar) { ret = index - startIndex; ++lineBreakSize; } } trailingCount = lineBreakSize; return ret; } } return index - startIndex; }
private static SKPaint ApplyWrapperTo(ref DrawingContextImpl.PaintWrapper wrapper, ref IDisposable curr, SKPaint paint) { curr?.Dispose(); curr = wrapper.ApplyTo(paint); return wrapper.Paint; }
public void DrawPicture(SKPicture picture, SKPaint paint = null) { if (picture == null) throw new ArgumentNullException ("picture"); SkiaApi.sk_canvas_draw_picture (Handle, picture.Handle, IntPtr.Zero, paint == null ? IntPtr.Zero : paint.Handle); }
public void DrawImageNinePatch(SKImage image, SKRectI center, SKRect dst, SKPaint paint = null) { if (image == null) throw new ArgumentNullException (nameof (image)); // the "center" rect must fit inside the image "rect" if (!SKRect.Create (image.Width, image.Height).Contains (center)) throw new ArgumentOutOfRangeException (nameof (center)); var xDivs = new [] { center.Left, center.Right }; var yDivs = new [] { center.Top, center.Bottom }; DrawImageLattice (image, xDivs, yDivs, dst, paint); }