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 RenderTexture(SKCanvas canvas, SKBitmap bitmap, float x, float y, float orientation = 0, float offsetX = 0, float offsetY = 0, LabelStyle.HorizontalAlignmentEnum horizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center, LabelStyle.VerticalAlignmentEnum verticalAlignment = LabelStyle.VerticalAlignmentEnum.Center, float opacity = 1f, float scale = 1f) { canvas.Save(); canvas.Translate(x, y); canvas.RotateDegrees(orientation, 0, 0); // todo: or degrees? canvas.Scale(scale, scale); x = offsetX + DetermineHorizontalAlignmentCorrection(horizontalAlignment, bitmap.Width); y = -offsetY + DetermineVerticalAlignmentCorrection(verticalAlignment, bitmap.Height); var halfWidth = bitmap.Width/2; var halfHeight = bitmap.Height/2; var rect = new SKRect(x - halfWidth, y - halfHeight, x + halfWidth, y + halfHeight); RenderTexture(canvas, bitmap, rect, opacity); canvas.Restore(); }
public static void Draw(SKCanvas canvas, IViewport viewport, IStyle style, IFeature feature, IDictionary<object, SKBitmapInfo> skBitmapCache, long currentIteration) { try { var raster = (IRaster)feature.Geometry; SKBitmapInfo textureInfo; if (!skBitmapCache.Keys.Contains(raster)) { textureInfo = BitmapHelper.LoadTexture(raster.Data); skBitmapCache[raster] = textureInfo; } else { textureInfo = skBitmapCache[raster]; } textureInfo.IterationUsed = currentIteration; skBitmapCache[raster] = textureInfo; var destination = WorldToScreen(viewport, feature.Geometry.GetBoundingBox()); BitmapHelper.RenderRaster(canvas, textureInfo.Bitmap, RoundToPixel(destination).ToSkia()); } catch (Exception ex) { Logger.Log(LogLevel.Error, ex.Message, ex); } }
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); } } }
/// <summary> /// Perform the restore now, instead of waiting for the Dispose. /// Will only do this once. /// </summary> public void Restore() { if (canvas != null) { canvas.RestoreToCount (saveCount); canvas = null; } }
// todo: // try to remove the feature argument. LabelStyle should already contain the feature specific text // The visible feature iterator should create this LabelStyle public static void Draw(SKCanvas canvas, IViewport viewport, IStyle style, IFeature feature, IGeometry geometry, IDictionary<int, SKBitmapInfo> symbolBitmapCache) { var point = geometry as Point; var destination = viewport.WorldToScreen(point); if (style is LabelStyle) // case 1) LabelStyle { LabelRenderer.Draw(canvas, (LabelStyle) style, feature, (float) destination.X, (float) destination.Y); } else if (style is SymbolStyle) { var symbolStyle = (SymbolStyle)style; if ( symbolStyle.BitmapId >= 0) // case 2) Bitmap Style { DrawPointWithBitmapStyle(canvas, symbolStyle, destination, symbolBitmapCache); } else // case 3) SymbolStyle without bitmap { DrawPointWithSymbolStyle(canvas, symbolStyle, destination, symbolStyle.SymbolType); } } else if (style is VectorStyle) // case 4) VectorStyle { DrawPointWithVectorStyle(canvas, (VectorStyle) style, destination); } else { throw new Exception($"Style of type '{style.GetType()}' is not supported for points"); } }
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); } }
public static void Draw(SKCanvas canvas, IViewport viewport, IStyle style, IGeometry geometry) { var multiPolygon = (MultiPolygon) geometry; foreach (var polygon in multiPolygon) { PolygonRenderer.Draw(canvas, viewport, style, polygon); } }
public static void Draw(SKCanvas canvas, IViewport viewport, IStyle style, IGeometry geometry) { var multiLineString = (MultiLineString) geometry; foreach (var lineString in multiLineString) { LineStringRenderer.Draw(canvas, viewport, style, lineString); } }
public static void Draw(SKCanvas canvas, IViewport viewport, IStyle style, IFeature feature, IGeometry geometry, IDictionary<int, SKBitmapInfo> symbolBitmapCache) { var multiPoint = (MultiPoint) geometry; foreach (var point in multiPoint) { PointRenderer.Draw(canvas, viewport, style, feature, point, symbolBitmapCache); } }
public SKAutoCanvasRestore(SKCanvas canvas, bool doSave) { this.canvas = canvas; this.saveCount = 0; if (canvas != null) { saveCount = canvas.SaveCount; if (doSave) { canvas.Save (); } } }
public void Paint(SkiaSharp.SKCanvas canvas, double x, double y) { var paint = new SKPaint { Color = SKColors.Black, IsAntialias = true, Style = SKPaintStyle.Fill, TextAlign = SKTextAlign.Center, TextSize = 24 }; canvas.DrawText(this.Text, (float)x, (float)y, paint); }
public static void DrawAsBitmap(SKCanvas canvas, LabelStyle style, IFeature feature, float x, float y) { var text = style.GetLabelText(feature); var key = text + "_" + style.Font.FontFamily + "_" + style.Font.Size + "_" + (float)style.Font.Size + "_" + style.BackColor + "_" + style.ForeColor; if (!LabelBitmapCache.Keys.Contains(key)) LabelBitmapCache[key] = new SKBitmapInfo { Bitmap = CreateLabelAsBitmap(style, text) }; var info = LabelBitmapCache[key]; BitmapHelper.RenderTexture(canvas, info.Bitmap, (int)Math.Round(x), (int)Math.Round(y), offsetX: (float)style.Offset.X, offsetY: (float)-style.Offset.Y, horizontalAlignment: style.HorizontalAlignment, verticalAlignment: style.VerticalAlignment); }
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; }
public async Task<Tuple<Stream, LoadingResult, ImageInformation>> Resolve(string identifier, TaskParameter parameters, CancellationToken token) { ImageSource source = parameters.Source; if (parameters.LoadingPlaceholderPath == identifier) source = parameters.LoadingPlaceholderSource; else if (parameters.ErrorPlaceholderPath == identifier) source = parameters.ErrorPlaceholderSource; var resolvedData = await Configuration.DataResolverFactory .GetResolver(identifier, source, parameters, Configuration) .Resolve(identifier, parameters, token); if (resolvedData?.Item1 == null) throw new FileNotFoundException(identifier); var svg = new SKSvg() { ThrowOnUnsupportedElement = false, }; SKPicture picture; using (var svgStream = resolvedData.Item1) { picture = svg.Load(resolvedData?.Item1); } using (var bitmap = new SKBitmap(200, 200, true)) using (var canvas = new SKCanvas(bitmap)) { float canvasMin = Math.Min(200, 200); float svgMax = Math.Max(svg.Picture.Bounds.Width, svg.Picture.Bounds.Height); float scale = canvasMin / svgMax; var matrix = SKMatrix.MakeScale(scale, scale); canvas.DrawPicture(picture, ref matrix); using (var image = SKImage.FromBitmap(bitmap)) { var stream = image.Encode()?.AsStream(); return new Tuple<Stream, LoadingResult, ImageInformation>(stream, resolvedData.Item2, resolvedData.Item3); } } }
} // End Sub DrawWithoutSurface public void DrawImageToCanvas(string fileName, SkiaSharp.SKCanvas canvas) { // Clear the canvas / Fill with white canvas.DrawColor(SKColors.White); using (System.IO.Stream fileStream = System.IO.File.OpenRead(fileName)) { // decode the bitmap from the stream using (SKManagedStream stream = new SKManagedStream(fileStream)) { using (SKBitmap bitmap = SKBitmap.Decode(stream)) { using (SKPaint paint = new SKPaint()) { canvas.DrawBitmap(bitmap, SKRect.Create(Width, Height), paint); } // End using paint } // End using bitmap } // End Using stream } // End Using fileStream } // End Sub DrawImageToCanvas
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; //--------------------------------------- }
// ExStart:RenderShapeToGraphics public static string RenderShapeToGraphics(string dataDir, Shape shape) { ShapeRenderer r = shape.GetShapeRenderer(); // Find the size that the shape will be rendered to at the specified scale and resolution. Size shapeSizeInPixels = r.GetSizeInPixels(1.0f, 96.0f); // Rotating the shape may result in clipping as the image canvas is too small. Find the longest side // And make sure that the graphics canvas is large enough to compensate for this. int maxSide = System.Math.Max(shapeSizeInPixels.Width, shapeSizeInPixels.Height); using (SkiaSharp.SKBitmap bitmap = new SkiaSharp.SKBitmap((int)(maxSide * 1.25), (int)(maxSide * 1.25))) { // Rendering to a graphics object means we can specify settings and transformations to be applied to // The shape that is rendered. In our case we will rotate the rendered shape. using (SkiaSharp.SKCanvas gr = new SkiaSharp.SKCanvas(bitmap)) { // Clear the shape with the background color of the document. gr.DrawColor(new SkiaSharp.SKColor(shape.Document.PageColor.R, shape.Document.PageColor.G, shape.Document.PageColor.B, shape.Document.PageColor.A)); // Center the rotation using translation method below gr.Translate((float)bitmap.Width / 8, (float)bitmap.Height / 2); // Rotate the image by 45 degrees. gr.RotateDegrees(45); // Undo the translation. gr.Translate(-(float)bitmap.Width / 8, -(float)bitmap.Height / 2); // Render the shape onto the graphics object. r.RenderToSize(gr, 0, 0, shapeSizeInPixels.Width, shapeSizeInPixels.Height); } // Save output to file. using (System.IO.FileStream fs = System.IO.File.Create(dataDir + "/RenderToSize_Out.png")) { SKData d = SKImage.FromBitmap(bitmap).Encode(SKEncodedImageFormat.Png, 100); d.SaveTo(fs); } } return("\nShape rendered to graphics successfully.\nFile saved at " + dataDir); }
/// <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); }
private static void DrawPointWithBitmapStyle(SKCanvas canvas, SymbolStyle symbolStyle, Point destination, IDictionary<int, SKBitmapInfo> symbolBitmapCache) { var stream = BitmapRegistry.Instance.Get(symbolStyle.BitmapId); stream.Position = 0; SKBitmapInfo textureInfo; if (!symbolBitmapCache.Keys.Contains(symbolStyle.BitmapId)) { textureInfo = BitmapHelper.LoadTexture(BitmapRegistry.Instance.Get(symbolStyle.BitmapId)); symbolBitmapCache[symbolStyle.BitmapId] = textureInfo; } else { textureInfo = symbolBitmapCache[symbolStyle.BitmapId]; } BitmapHelper.RenderTexture(canvas, textureInfo.Bitmap, (float) destination.X, (float) destination.Y, (float) symbolStyle.SymbolRotation, (float) symbolStyle.SymbolOffset.X, (float) symbolStyle.SymbolOffset.Y, opacity: (float) symbolStyle.Opacity, scale: (float) symbolStyle.SymbolScale); }
private void SkiaManager_BeforePaint(object sender, SkiaSharp.SKCanvas e) { var skiaManager = sender as SkiaManager; SKMatrix.MakeTranslation(0, e.LocalClipBounds.Height); var matrix = skiaManager.SketchSpaceToCanvasSpaceMatrix; Sketch.Width = SketchSize.X; Sketch.Height = SketchSize.Y; SKPoint sketchSize = new SKPoint(Sketch.Width, Sketch.Height); //matrix.SetScaleTranslate(1f, -1f, e.LocalClipBounds.Width / 2, e.LocalClipBounds.Height / 2); var sketchRatio = sketchSize.X / sketchSize.Y; var localClipRatio = e.LocalClipBounds.Width / e.LocalClipBounds.Height; var xFactor = e.LocalClipBounds.Width / sketchSize.X; var yFactor = e.LocalClipBounds.Height / sketchSize.Y; if (localClipRatio > sketchRatio) { xFactor = yFactor; } else { yFactor = xFactor; } var xTranslate = e.LocalClipBounds.MidX - (xFactor * sketchSize.X) / 2; var yTranslate = e.LocalClipBounds.Height - (e.LocalClipBounds.MidY - (yFactor * sketchSize.Y) / 2); matrix.SetScaleTranslate(xFactor, -yFactor, xTranslate, yTranslate); skiaManager.SketchSpaceToCanvasSpaceMatrix = matrix; Matrix3x2 epxToPx = Matrix3x2.CreateScale(1); Matrix3x2 pxToSketchSpace = Matrix3x2.Multiply(Matrix3x2.CreateTranslation(-xTranslate, -yTranslate), Matrix3x2.CreateScale(1 / xFactor, -1 / yFactor)); SketchInputManager.InputSpaceToSketchSpaceMatrix = Matrix3x2.Multiply(epxToPx, pxToSketchSpace); }
public static void DrawVxsSnap(SKCanvas g, VertexStoreSnap vxsSnap, SKPaint stroke) { using (var p = CreateGraphicsPath(vxsSnap)) { g.DrawPath(p, stroke); } }
public void Draw (SKCanvas canvas, ref SKMatrix matrix) => SkiaApi.sk_drawable_draw (Handle, canvas.Handle, ref matrix);
internal void Draw(DrawingContextImpl context, SKCanvas canvas, SKPoint origin, DrawingContextImpl.PaintWrapper foreground) { /* TODO: This originated from Native code, it might be useful for debugging character positions as * we improve the FormattedText support. Will need to port this to C# obviously. Rmove when * not needed anymore. SkPaint dpaint; ctx->Canvas->save(); ctx->Canvas->translate(origin.fX, origin.fY); for (int c = 0; c < Lines.size(); c++) { dpaint.setARGB(255, 0, 0, 0); SkRect rc; rc.fLeft = 0; rc.fTop = Lines[c].Top; rc.fRight = Lines[c].Width; rc.fBottom = rc.fTop + LineOffset; ctx->Canvas->drawRect(rc, dpaint); } for (int c = 0; c < Length; c++) { dpaint.setARGB(255, c % 10 * 125 / 10 + 125, (c * 7) % 10 * 250 / 10, (c * 13) % 10 * 250 / 10); dpaint.setStyle(SkPaint::kFill_Style); ctx->Canvas->drawRect(Rects[c], dpaint); } ctx->Canvas->restore(); */ SKPaint paint = _paint; IDisposable currd = null; var currentWrapper = foreground; try { SKPaint currFGPaint = ApplyWrapperTo(ref foreground, ref currd, paint); bool hasCusomFGBrushes = _foregroundBrushes.Any(); for (int c = 0; c < _skiaLines.Count; c++) { AvaloniaFormattedTextLine line = _skiaLines[c]; float x = TransformX(origin.X, 0, paint.TextAlign); if (!hasCusomFGBrushes) { var subString = _text.Substring(line.Start, line.Length); canvas.DrawText(subString, x, origin.Y + line.Top + _lineOffset, paint); } else { float currX = x; string subStr; int len; for (int i = line.Start; i < line.Start + line.Length;) { var fb = GetNextForegroundBrush(ref line, i, out len); if (fb != null) { //TODO: figure out how to get the brush size currentWrapper = context.CreatePaint(fb, new Size()); } else { if (!currentWrapper.Equals(foreground)) currentWrapper.Dispose(); currentWrapper = foreground; } subStr = _text.Substring(i, len); if (currFGPaint != currentWrapper.Paint) { currFGPaint = ApplyWrapperTo(ref currentWrapper, ref currd, paint); } canvas.DrawText(subStr, currX, origin.Y + line.Top + _lineOffset, paint); i += len; currX += paint.MeasureText(subStr); } } } } finally { if (!currentWrapper.Equals(foreground)) currentWrapper.Dispose(); currd?.Dispose(); } }
void DrawArc (SKCanvas canvas, SKPaint paint, double startAngleInDegrees, double endAngleInDegrees) { // find center and radius var centerx = (float)Bounds.Width / 2; var centery = (float)Bounds.Height / 2; var radius = Bounds.Width < Bounds.Height ? (float)Bounds.Width / 2 : (float)Bounds.Height / 2; var w = 0.558f; // convert degrees to radians var startAngleRadians = startAngleInDegrees * Math.PI / 180; var endAngleRadians = endAngleInDegrees * Math.PI / 180; // find x,y coordinates for start and end points var startx = centerx + radius * (float)Math.Cos (startAngleRadians); var starty = centery + radius * (float)Math.Sin (startAngleRadians); var startPoint = new SKPoint (startx, starty); var endx = centerx + radius * (float)Math.Cos (endAngleRadians); var endy = centery + radius * (float)Math.Sin (endAngleRadians); var endPoint = new SKPoint (endx, endy); // find linear distance & midpoint between start and end points var linearDistance = Math.Sqrt(Math.Pow(endy - starty, 2) / Math.Pow(endx - startx, 2)); var midx = (startx + endx) / 2; var midy = (starty + endy) / 2; var midPoint = new SKPoint (midx, midy); // rotate end point 45 degrees counterclockwise around midpoint to find Conic function anchor var anchorPoint = RotatePoint (endPoint, midPoint, 45); // build path var path = new SKPath (); //path.MoveTo (startx, starty); //path.ConicTo (anchorPoint.X, anchorPoint.Y, endx, endy, w); path.MoveTo (centerx, centery - radius); path.ConicTo (centerx + radius, centery - radius, centerx + radius, centery, w); path.ConicTo (centerx + radius, centery + radius, centerx, centery + radius, w); path.ConicTo (centerx - radius, centery + radius, centerx - radius, centery, w); //path.ConicTo (centerx - radius, centery - radius, centerx, centery - radius, w); //canvas.DrawPoints (SKPointMode.Points, new SKPoint [] { }, paint); canvas.DrawPath (path, paint); }
void DrawCircle (SKCanvas canvas, SKPaint paint, int xc, int yc, int radius) { if (radius <= 0) return; int x = radius; int y = 0; int cd2 = 0; var points = new List<SKPoint> (); points.Add (new SKPoint (xc - radius, yc)); points.Add (new SKPoint (xc + radius, yc)); points.Add (new SKPoint (xc, yc - radius)); points.Add (new SKPoint (xc, yc + radius)); while (x > y) { cd2 -= (--x) - (++y); if (cd2 < 0) cd2 += x++; // 8 octants - listed clockwise // right hemisphere, starting at the top points.Add (new SKPoint (xc + y, yc - x)); points.Add (new SKPoint (xc + x, yc - y)); points.Add (new SKPoint (xc + x, yc + y)); points.Add (new SKPoint (xc + y, yc + x)); // left hemisphere, continuing around from the bottom points.Add (new SKPoint (xc - y, yc + x)); points.Add (new SKPoint (xc - x, yc + y)); points.Add (new SKPoint (xc - x, yc - y)); points.Add (new SKPoint (xc - y, yc - x)); } canvas.DrawPoints (SKPointMode.Points, points.ToArray (), paint); }
public SKCanvasWrapper(SkiaSharp.SKCanvas canvas) { _canvas = canvas; }
#pragma warning restore 414 public static void DrawXamagon(SKCanvas canvas, int width, int height) { // Width 41.6587026 => 144.34135 // Height 56 => 147 var paddingFactor = .6f; var imageLeft = 41.6587026f; var imageRight = 144.34135f; var imageTop = 56f; var imageBottom = 147f; var imageWidth = imageRight - imageLeft; var scale = (((float)height > width ? width : height) / imageWidth) * paddingFactor; var translateX = (imageLeft + imageRight) / -2 + width / scale * 1 / 2; var translateY = (imageBottom + imageTop) / -2 + height / scale * 1 / 2; canvas.Scale(scale, scale); canvas.Translate(translateX, translateY); using (var paint = new SKPaint()) { paint.IsAntialias = true; paint.Color = SKColors.White; canvas.DrawPaint(paint); paint.StrokeCap = SKStrokeCap.Round; var t = paint.StrokeCap; using (var path = new SKPath()) { path.MoveTo(71.4311121f, 56f); path.CubicTo(68.6763107f, 56.0058575f, 65.9796704f, 57.5737917f, 64.5928855f, 59.965729f); path.LineTo(43.0238921f, 97.5342563f); path.CubicTo(41.6587026f, 99.9325978f, 41.6587026f, 103.067402f, 43.0238921f, 105.465744f); path.LineTo(64.5928855f, 143.034271f); path.CubicTo(65.9798162f, 145.426228f, 68.6763107f, 146.994582f, 71.4311121f, 147f); path.LineTo(114.568946f, 147f); path.CubicTo(117.323748f, 146.994143f, 120.020241f, 145.426228f, 121.407172f, 143.034271f); path.LineTo(142.976161f, 105.465744f); path.CubicTo(144.34135f, 103.067402f, 144.341209f, 99.9325978f, 142.976161f, 97.5342563f); path.LineTo(121.407172f, 59.965729f); path.CubicTo(120.020241f, 57.5737917f, 117.323748f, 56.0054182f, 114.568946f, 56f); path.LineTo(71.4311121f, 56f); path.Close(); paint.Color = XamDkBlue; canvas.DrawPath(path, paint); } using (var path = new SKPath()) { path.MoveTo(71.8225901f, 77.9780432f); path.CubicTo(71.8818491f, 77.9721857f, 71.9440029f, 77.9721857f, 72.0034464f, 77.9780432f); path.LineTo(79.444074f, 77.9780432f); path.CubicTo(79.773437f, 77.9848769f, 80.0929203f, 78.1757336f, 80.2573978f, 78.4623994f); path.LineTo(92.8795281f, 101.015639f); path.CubicTo(92.9430615f, 101.127146f, 92.9839987f, 101.251384f, 92.9995323f, 101.378901f); path.CubicTo(93.0150756f, 101.251354f, 93.055974f, 101.127107f, 93.1195365f, 101.015639f); path.LineTo(105.711456f, 78.4623994f); path.CubicTo(105.881153f, 78.167045f, 106.215602f, 77.975134f, 106.554853f, 77.9780432f); path.LineTo(113.995483f, 77.9780432f); path.CubicTo(114.654359f, 77.9839007f, 115.147775f, 78.8160066f, 114.839019f, 79.4008677f); path.LineTo(102.518299f, 101.500005f); path.LineTo(114.839019f, 123.568869f); path.CubicTo(115.176999f, 124.157088f, 114.671442f, 125.027775f, 113.995483f, 125.021957f); path.LineTo(106.554853f, 125.021957f); path.CubicTo(106.209673f, 125.019028f, 105.873247f, 124.81384f, 105.711456f, 124.507327f); path.LineTo(93.1195365f, 101.954088f); path.CubicTo(93.0560031f, 101.84258f, 93.0150659f, 101.718333f, 92.9995323f, 101.590825f); path.CubicTo(92.983989f, 101.718363f, 92.9430906f, 101.842629f, 92.8795281f, 101.954088f); path.LineTo(80.2573978f, 124.507327f); path.CubicTo(80.1004103f, 124.805171f, 79.7792269f, 125.008397f, 79.444074f, 125.021957f); path.LineTo(72.0034464f, 125.021957f); path.CubicTo(71.3274867f, 125.027814f, 70.8220664f, 124.157088f, 71.1600463f, 123.568869f); path.LineTo(83.4807624f, 101.500005f); path.LineTo(71.1600463f, 79.400867f); path.CubicTo(70.8647037f, 78.86725f, 71.2250368f, 78.0919422f, 71.8225901f, 77.9780432f); path.LineTo(71.8225901f, 77.9780432f); path.Close(); paint.Color = SKColors.White; canvas.DrawPath(path, paint); } } }
public static void Xfermode(SKCanvas canvas, int width, int height) { var modes = Enum.GetValues(typeof(SKXferMode)).Cast <SKXferMode> ().ToArray(); var cols = width < height ? 3 : 5; var rows = (modes.Length - 1) / cols + 1; var w = (float)width / cols; var h = (float)height / rows; var rect = SKRect.Create(w, h); var srcPoints = new[] { new SKPoint(0.0f, 0.0f), new SKPoint(w, 0.0f) }; var srcColors = new [] { SKColors.Magenta.WithAlpha(0), SKColors.Magenta }; var dstPoints = new [] { new SKPoint(0.0f, 0.0f), new SKPoint(0.0f, h) }; var dstColors = new [] { SKColors.Cyan.WithAlpha(0), SKColors.Cyan }; using (var text = new SKPaint()) using (var stroke = new SKPaint()) using (var src = new SKPaint()) using (var dst = new SKPaint()) using (var srcShader = SKShader.CreateLinearGradient(srcPoints [0], srcPoints [1], srcColors, null, SKShaderTileMode.Clamp)) using (var dstShader = SKShader.CreateLinearGradient(dstPoints [0], dstPoints [1], dstColors, null, SKShaderTileMode.Clamp)) { text.TextSize = 12.0f; text.IsAntialias = true; text.TextAlign = SKTextAlign.Center; stroke.IsStroke = true; src.Shader = srcShader; dst.Shader = dstShader; canvas.Clear(SKColors.White); for (var i = 0; i < modes.Length; ++i) { using (new SKAutoCanvasRestore(canvas, true)) { canvas.Translate(w * (i / rows), h * (i % rows)); canvas.ClipRect(rect); canvas.DrawColor(SKColors.LightGray); canvas.SaveLayer(null); canvas.Clear(SKColors.Transparent); canvas.DrawPaint(dst); src.XferMode = modes [i]; canvas.DrawPaint(src); canvas.DrawRect(rect, stroke); var desc = modes [i].ToString(); canvas.DrawText(desc, w / 2f, h / 2f, text); } } } }
public abstract void draw(SkiaSharp.SKCanvas g);
public static void ColorMatrixColorFilter(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".baboon.png"; // load the image from the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var bitmap = SKBitmap.Decode(stream)) { var f = new Action <SKRect, float[]>((rect, colorMatrix) => { using (var cf = SKColorFilter.CreateColorMatrix(colorMatrix)) using (var paint = new SKPaint()) { paint.ColorFilter = cf; canvas.DrawBitmap(bitmap, rect, paint); } }); var colorMatrix1 = new float[20] { 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 1f, 0f }; var grayscale = new float[20] { 0.21f, 0.72f, 0.07f, 0.0f, 0.0f, 0.21f, 0.72f, 0.07f, 0.0f, 0.0f, 0.21f, 0.72f, 0.07f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; var colorMatrix3 = new float[20] { -1f, 1f, 1f, 0f, 0f, 1f, -1f, 1f, 0f, 0f, 1f, 1f, -1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f }; var colorMatrix4 = new float[20] { 0.0f, 0.5f, 0.5f, 0f, 0f, 0.5f, 0.0f, 0.5f, 0f, 0f, 0.5f, 0.5f, 0.0f, 0f, 0f, 0.0f, 0.0f, 0.0f, 1f, 0f }; var highContrast = new float[20] { 4.0f, 0.0f, 0.0f, 0.0f, -4.0f * 255f / (4.0f - 1f), 0.0f, 4.0f, 0.0f, 0.0f, -4.0f * 255f / (4.0f - 1f), 0.0f, 0.0f, 4.0f, 0.0f, -4.0f * 255f / (4.0f - 1f), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; var colorMatrix6 = new float[20] { 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 1f, 0f }; var sepia = new float[20] { 0.393f, 0.769f, 0.189f, 0.0f, 0.0f, 0.349f, 0.686f, 0.168f, 0.0f, 0.0f, 0.272f, 0.534f, 0.131f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; var inverter = new float[20] { -1f, 0f, 0f, 0f, 255f, 0f, -1f, 0f, 0f, 255f, 0f, 0f, -1f, 0f, 255f, 0f, 0f, 0f, 1f, 0f }; var matices = new[] { colorMatrix1, grayscale, highContrast, sepia, colorMatrix3, colorMatrix4, colorMatrix6, inverter }; var cols = width < height ? 2 : 4; var rows = (matices.Length - 1) / cols + 1; var w = (float)width / cols; var h = (float)height / rows; for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { f(SKRect.Create(x * w, y * h, w, h), matices[y * cols + x]); } } } }
protected override void InternalDraw(SkiaSharp.SKCanvas canvas) { base.InternalDraw(canvas); using (var paint = new SKPaint()) { var renderBounds = RenderBounds; var smallestSize = Math.Min(renderBounds.Size.Width, renderBounds.Size.Height); var square = new Rectangle(renderBounds.Center.X - smallestSize / 2.0, renderBounds.Center.Y - smallestSize / 2.0, smallestSize, smallestSize); Rectangle imageBounds = renderBounds; paint.IsAntialias = true; if (IsCheckable) { paint.Color = SelectionBackgroundColor.ToSKColor(); canvas.DrawOval(square.ToSKRect(), paint); var darkerOutline = SelectionBackgroundColor.AddLuminosity(-0.1); paint.IsStroke = true; paint.Color = darkerOutline.ToSKColor(); paint.StrokeWidth = 1.0f; canvas.DrawOval(square.ToSKRect(), paint); imageBounds = renderBounds.Inflate(-BorderSize, -BorderSize); } if (Drawable != null && !IsSelected) { var drawableSize = Drawable.GetSize(imageBounds.Width, imageBounds.Height); double ratioX = (double)imageBounds.Width / (double)drawableSize.Width; double ratioY = (double)imageBounds.Height / (double)drawableSize.Height; double ratio = ratioX < ratioY ? ratioX : ratioY; double newWidth = drawableSize.Width * ratio; double newHeight = drawableSize.Height * ratio; float cx = (float)(imageBounds.X + (imageBounds.Width / 2.0) - (newWidth / 2.0)); float cy = (float)(imageBounds.Y + (imageBounds.Height / 2.0) - (newHeight / 2.0)); paint.XferMode = SKXferMode.SrcOver; paint.FilterQuality = SKFilterQuality.Low; Drawable.Draw(canvas, new Rectangle(cx, cy, newWidth, newHeight), paint); } if (IsCheckable && IsSelected) { paint.Color = SelectionColor.ToSKColor(); paint.IsStroke = false; canvas.DrawOval(square.ToSKRect(), paint); double ratioX = (double)imageBounds.Width / (double)TickBitmap.Width; double ratioY = (double)imageBounds.Height / (double)TickBitmap.Height; double ratio = ratioX < ratioY ? ratioX : ratioY; double newWidth = TickBitmap.Width * ratio; double newHeight = TickBitmap.Height * ratio; float cx = (float)(imageBounds.X + (imageBounds.Width / 2.0) - (newWidth / 2.0)); float cy = (float)(imageBounds.Y + (imageBounds.Height / 2.0) - (newHeight / 2.0)); paint.XferMode = SKXferMode.SrcOver; paint.FilterQuality = SKFilterQuality.Low; canvas.DrawBitmap(TickBitmap, new SKRect(cx, cy, (float)(cx + newWidth), (float)(cy + newHeight)), paint); } } }
protected virtual void OnDraw (SKCanvas canvas) { }
public static void FillPath(SKCanvas g, SKPath p, SKPaint fill) { g.DrawPath(p, fill); }
public static void DrawPath(SKCanvas g, SKPath p, SKPaint stroke) { g.DrawPath(p, stroke); }
private void DrawEllipseInternal(SKCanvas canvas, SKPaint brush, SKPaint pen, bool isStroked, bool isFilled, ref SKRect rect) { if (isFilled) { canvas.DrawOval(rect, brush); } if (isStroked) { canvas.DrawOval(rect, pen); } }
private void ReadElement(XElement e, SKCanvas canvas, SKPaint stroke, SKPaint fill) { ReadPaints(e, ref stroke, ref fill); // transform matrix var transform = ReadTransform(e.Attribute("transform")?.Value ?? string.Empty); canvas.Save(); canvas.Concat(ref transform); // SVG elements var elementName = e.Name.LocalName; switch (elementName) { case "text": if (stroke != null || fill != null) { ReadText(e, canvas, stroke?.Clone(), fill?.Clone()); } break; case "rect": if (stroke != null || fill != null) { var x = ReadNumber(e.Attribute("x")); var y = ReadNumber(e.Attribute("y")); var width = ReadNumber(e.Attribute("width")); var height = ReadNumber(e.Attribute("height")); var rx = ReadNumber(e.Attribute("rx")); var ry = ReadNumber(e.Attribute("ry")); var rect = SKRect.Create(x, y, width, height); if (rx > 0 || ry > 0) { if (fill != null) { canvas.DrawRoundRect(rect, rx, ry, fill); } if (stroke != null) { canvas.DrawRoundRect(rect, rx, ry, stroke); } } else { if (fill != null) { canvas.DrawRect(rect, fill); } if (stroke != null) { canvas.DrawRect(rect, stroke); } } } break; case "ellipse": if (stroke != null || fill != null) { var cx = ReadNumber(e.Attribute("cx")); var cy = ReadNumber(e.Attribute("cy")); var rx = ReadNumber(e.Attribute("rx")); var ry = ReadNumber(e.Attribute("ry")); if (fill != null) { canvas.DrawOval(cx, cy, rx, ry, fill); } if (stroke != null) { canvas.DrawOval(cx, cy, rx, ry, stroke); } } break; case "circle": if (stroke != null || fill != null) { var cx = ReadNumber(e.Attribute("cx")); var cy = ReadNumber(e.Attribute("cy")); var rr = ReadNumber(e.Attribute("r")); if (fill != null) { canvas.DrawCircle(cx, cy, rr, fill); } if (stroke != null) { canvas.DrawCircle(cx, cy, rr, stroke); } } break; case "path": if (stroke != null || fill != null) { var d = e.Attribute("d")?.Value; if (!string.IsNullOrWhiteSpace(d)) { var path = SKPath.ParseSvgPathData(d); if (fill != null) { canvas.DrawPath(path, fill); } if (stroke != null) { canvas.DrawPath(path, stroke); } } } break; case "polygon": case "polyline": if (stroke != null || fill != null) { var close = elementName == "polygon"; var p = e.Attribute("points")?.Value; if (!string.IsNullOrWhiteSpace(p)) { var path = ReadPolyPath(p, close); if (fill != null) { canvas.DrawPath(path, fill); } if (stroke != null) { canvas.DrawPath(path, stroke); } } } break; case "g": if (e.HasElements) { foreach (var gElement in e.Elements()) { ReadElement(gElement, canvas, stroke?.Clone(), fill?.Clone()); } } break; case "use": if (e.HasAttributes) { var href = ReadHref(e); if (href != null) { // TODO: copy/process other attributes var x = ReadNumber(e.Attribute("x")); var y = ReadNumber(e.Attribute("y")); var useTransform = SKMatrix.MakeTranslation(x, y); canvas.Save(); canvas.Concat(ref useTransform); ReadElement(href, canvas, stroke?.Clone(), fill?.Clone()); canvas.Restore(); } } break; case "line": if (stroke != null) { var x1 = ReadNumber(e.Attribute("x1")); var x2 = ReadNumber(e.Attribute("x2")); var y1 = ReadNumber(e.Attribute("y1")); var y2 = ReadNumber(e.Attribute("y2")); canvas.DrawLine(x1, y1, x2, y2, stroke); } break; case "switch": if (e.HasElements) { foreach (var ee in e.Elements()) { var requiredFeatures = ee.Attribute("requiredFeatures"); var requiredExtensions = ee.Attribute("requiredExtensions"); var systemLanguage = ee.Attribute("systemLanguage"); // TODO: evaluate requiredFeatures, requiredExtensions and systemLanguage var isVisible = requiredFeatures == null && requiredExtensions == null && systemLanguage == null; if (isVisible) { ReadElement(ee, canvas, stroke?.Clone(), fill?.Clone()); } } } break; case "defs": case "title": case "desc": case "description": // already read earlier break; default: LogOrThrow($"SVG element '{elementName}' is not supported"); break; } // restore matrix canvas.Restore(); }
private void DrawPathInternal(SKCanvas canvas, SKPaint brush, SKPaint pen, bool isStroked, bool isFilled, SKPath path) { if (isFilled) { canvas.DrawPath(path, brush); } if (isStroked) { canvas.DrawPath(path, pen); } }
void DrawArcFromTo (SKCanvas canvas, SKPaint paint, int xc, int yc, int radius, float startAngleInDegrees, float endAngleInDegrees) { if (radius <= 0) return; int x = radius; int y = 0; int cd2 = 0; // convert degrees to radians var startAngleRadians = startAngleInDegrees * Math.PI / 180; var endAngleRadians = endAngleInDegrees * Math.PI / 180; // find x,y coordinates for start and end points var startx = xc + radius * (float)Math.Cos (startAngleRadians); var starty = yc + radius * (float)Math.Sin (startAngleRadians); var startPoint = new SKPoint (startx, starty); var endx = xc + radius * (float)Math.Cos (endAngleRadians); var endy = yc + radius * (float)Math.Sin (endAngleRadians); var endPoint = new SKPoint (endx, endy); // build the path var points = new List<SKPoint> (); // 4 cardinal points var p00 = new SKPoint (xc - radius, yc); var ap00 = Math.Atan2 (p00.Y - yc, p00.X - xc) * 180 / Math.PI; if (IsAngleBetween ((int)ap00, (int)startAngleInDegrees, (int)endAngleInDegrees)) points.Add (p00); var p01 = new SKPoint (xc + radius, yc); var ap01 = Math.Atan2 (p01.Y - yc, p01.X - xc) * 180 / Math.PI; if (IsAngleBetween ((int)ap01, (int)startAngleInDegrees, (int)endAngleInDegrees)) points.Add (p01); var p02 = new SKPoint (xc, yc - radius); var ap02 = Math.Atan2 (p02.Y - yc, p02.X - xc) * 180 / Math.PI; if (IsAngleBetween ((int)ap02, (int)startAngleInDegrees, (int)endAngleInDegrees)) points.Add (p02); var p03 = new SKPoint (xc, yc + radius); var ap03 = Math.Atan2 (p03.Y - yc, p03.X - xc) * 180 / Math.PI; if (IsAngleBetween ((int)ap03, (int)startAngleInDegrees, (int)endAngleInDegrees)) points.Add (p03); while (x > y) { x--; y++; cd2 -= x - y; if (cd2 < 0) cd2 += x++; // 8 octants - listed clockwise // right hemisphere, starting at the top var p0 = new SKPoint (xc + y, yc - x); var arp0 = Math.Atan2 (p0.Y - yc, p0.X - xc) * 180 / Math.PI; if (IsAngleBetween ((int)arp0, (int)startAngleInDegrees, (int)endAngleInDegrees)) points.Add (p0); var p1 = new SKPoint (xc + x, yc - y); var arp1 = Math.Atan2 (p1.Y - yc, p1.X - xc) * 180 / Math.PI; if (IsAngleBetween ((int)arp1, (int)startAngleInDegrees, (int)endAngleInDegrees)) points.Add (p1); var p2 = new SKPoint (xc + x, yc + y); var arp2 = Math.Atan2 (p2.Y - yc, p2.X - xc) * 180 / Math.PI; if (IsAngleBetween ((int)arp2, (int)startAngleInDegrees, (int)endAngleInDegrees)) points.Add (p2); var p3 = new SKPoint (xc + y, yc + x); var arp3 = Math.Atan2 (p3.Y - yc, p3.X - xc) * 180 / Math.PI; if (IsAngleBetween ((int)arp3, (int)startAngleInDegrees, (int)endAngleInDegrees)) points.Add (p3); // left hemisphere, continuing around from the bottom var p4 = new SKPoint (xc - y, yc + x); var arp4 = Math.Atan2 (p4.Y - yc, p4.X - xc) * 180 / Math.PI; if (IsAngleBetween ((int)arp4, (int)startAngleInDegrees, (int)endAngleInDegrees)) points.Add (p4); var p5 = new SKPoint (xc - x, yc + y); var arp5 = Math.Atan2 (p5.Y - yc, p5.X - xc) * 180 / Math.PI; if (IsAngleBetween ((int)arp5, (int)startAngleInDegrees, (int)endAngleInDegrees)) points.Add (p5); var p6 = new SKPoint (xc - x, yc - y); var arp6 = Math.Atan2 (p6.Y - yc, p6.X - xc) * 180 / Math.PI; if (IsAngleBetween ((int)arp6, (int)startAngleInDegrees, (int)endAngleInDegrees)) points.Add (p6); var p7 = new SKPoint (xc - y, yc - x); var arp7 = Math.Atan2 (p7.Y - yc, p7.X - xc) * 180 / Math.PI; if (IsAngleBetween ((int)arp7, (int)startAngleInDegrees, (int)endAngleInDegrees)) points.Add (p7); } canvas.DrawPoints (SKPointMode.Points, points.ToArray (), paint); }
private void DrawGridInternal(SKCanvas canvas, SKPaint stroke, ref SKRect rect, double offsetX, double offsetY, double cellWidth, double cellHeight, bool isStroked) { float ox = rect.Left; float oy = rect.Top; float sx = (float)(ox + offsetX); float sy = (float)(oy + offsetY); float ex = ox + (rect.Right - rect.Left); float ey = oy + (rect.Bottom - rect.Top); for (float x = sx; x < ex; x += (float)cellWidth) { var p0 = new SKPoint(x, oy); var p1 = new SKPoint(x, ey); DrawLineInternal(canvas, stroke, isStroked, ref p0, ref p1); } for (float y = sy; y < ey; y += (float)cellHeight) { var p0 = new SKPoint(ox, y); var p1 = new SKPoint(ex, y); DrawLineInternal(canvas, stroke, isStroked, ref p0, ref p1); } }
private void ReadElement(XElement e, SKCanvas canvas) { ReadElement(e, canvas, null, CreatePaint()); }
private void DrawBackgroundInternal(SKCanvas canvas, ArgbColor color, Rect2 rect) { using (SKPaint brush = ToSKPaintBrush(color)) { SKRect srect = SKRect.Create( _scaleToPage(rect.X), _scaleToPage(rect.Y), _scaleToPage(rect.Width), _scaleToPage(rect.Height)); canvas.DrawRect(srect, brush); } }
public DrawingContextImpl(SKCanvas canvas) { Canvas = canvas; Canvas.Clear(); }
public SKAutoCanvasRestore(SKCanvas canvas) : this(canvas, true) { }
public static void FillVxsSnap(SKCanvas g, VertexStoreSnap vxsSnap, SKPaint fill) { using (var p = CreateGraphicsPath(vxsSnap)) { g.DrawPath(p, fill); } }
public void Draw (SKCanvas canvas, float x, float y) { var matrix = SKMatrix.MakeTranslation (x, y); Draw (canvas, ref matrix); }