public static void BitmapShader(SKCanvas canvas, int width, int height) { var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".color-wheel.png"; // load the image from the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var source = SKBitmap.Decode(stream)) { // create the shader and paint //SkMatrix matrix; //matrix.setScale(0.75f, 0.75f); //matrix.preRotate(30.0f); var matrix = SKMatrix.MakeRotation(30.0f); using (var shader = SKShader.CreateBitmap(source, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat, matrix)) using (var paint = new SKPaint()) { paint.IsAntialias = true; paint.Shader = shader; // tile the bitmap canvas.Clear(SKColors.White); canvas.DrawPaint(paint); } } }
public static void BitmapShaderManipulated(SKCanvas canvas, int width, int height) { var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".color-wheel.png"; // load the image from the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var source = SKBitmap.Decode(stream)) { // invert the pixels var pixels = source.Pixels; for (int i = 0; i < pixels.Length; i++) { pixels[i] = new SKColor( (byte)(255 - pixels [i].Red), (byte)(255 - pixels [i].Green), (byte)(255 - pixels [i].Blue), pixels [i].Alpha); } source.Pixels = pixels; using (var shader = SKShader.CreateBitmap(source, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat)) using (var paint = new SKPaint()) { paint.IsAntialias = true; paint.Shader = shader; // tile the bitmap canvas.Clear(SKColors.White); canvas.DrawPaint(paint); } } }
public static void TurbulencePerlinNoiseShader(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); using (var shader = SKShader.CreatePerlinNoiseTurbulence(0.05f, 0.05f, 4, 0.0f)) using (var paint = new SKPaint()) { paint.Shader = shader; canvas.DrawPaint(paint); } }
public static void SweepGradientShader(SKCanvas canvas, int width, int height) { var colors = new[] { SKColors.Cyan, SKColors.Magenta, SKColors.Yellow, SKColors.Cyan }; var center = new SKPoint(width / 2f, height / 2f); using (var shader = SKShader.CreateSweepGradient(center, colors, null)) using (var paint = new SKPaint()) { paint.Shader = shader; canvas.DrawPaint(paint); } }
public static void ComposeShader(SKCanvas canvas, int width, int height) { var colors = new [] { SKColors.Blue, SKColors.Yellow }; var center = new SKPoint(width / 2f, height / 2f); using (var shader1 = SKShader.CreateRadialGradient(center, 180.0f, colors, null, SKShaderTileMode.Clamp)) using (var shader2 = SKShader.CreatePerlinNoiseTurbulence(0.025f, 0.025f, 2, 0.0f)) using (var shader = SKShader.CreateCompose(shader1, shader2)) using (var paint = new SKPaint()) { paint.Shader = shader; canvas.DrawPaint(paint); } }
public static void DrawGradient(SKCanvas canvas, int width, int height) { var ltColor = SKColors.White; var dkColor = SKColors.Black; using (var paint = new SKPaint()) { paint.IsAntialias = true; using (var shader = SKShader.CreateLinearGradient( new SKPoint(0, 0), new SKPoint(0, height), new [] { ltColor, dkColor }, null, SKShaderTileMode.Clamp)) { paint.Shader = shader; canvas.DrawPaint(paint); } } // Center and Scale the Surface var scale = (width < height ? width : height) / (240f); canvas.Translate(width / 2f, height / 2f); canvas.Scale(scale, scale); canvas.Translate(-128, -128); using (var paint = new SKPaint()) { paint.IsAntialias = true; using (var shader = SKShader.CreateTwoPointConicalGradient( new SKPoint(115.2f, 102.4f), 25.6f, new SKPoint(102.4f, 102.4f), 128.0f, new [] { ltColor, dkColor }, null, SKShaderTileMode.Clamp )) { paint.Shader = shader; canvas.DrawOval(new SKRect(51.2f, 51.2f, 204.8f, 204.8f), paint); } } }
public bool CopyTo(SKBitmap destination, SKColorType colorType) { if (destination == null) { throw new ArgumentNullException(nameof(destination)); } if (colorType == SKColorType.Unknown) { return(false); } using var srcPixmap = PeekPixels(); if (srcPixmap == null) { return(false); } using var temp = new SKBitmap(); var dstInfo = srcPixmap.Info.WithColorType(colorType); if (!temp.TryAllocPixels(dstInfo)) { return(false); } using var canvas = new SKCanvas(temp); using var paint = new SKPaint { Shader = ToShader(), BlendMode = SKBlendMode.Src }; canvas.DrawPaint(paint); destination.Swap(temp); return(true); }
public static void DrawGradient (SKCanvas canvas, int width, int height) { var ltColor = SKColors.White; var dkColor = SKColors.Black; using (var paint = new SKPaint ()) { paint.IsAntialias = true; using (var shader = SKShader.CreateLinearGradient ( new SKPoint (0, 0), new SKPoint (0, height), new [] {ltColor, dkColor}, null, SKShaderTileMode.Clamp)) { paint.Shader = shader; canvas.DrawPaint (paint); } } // Center and Scale the Surface var scale = (width < height ? width : height) / (240f); canvas.Translate (width/2f, height/2f); canvas.Scale (scale, scale); canvas.Translate (-128, -128); using (var paint = new SKPaint ()) { paint.IsAntialias = true; using (var shader = SKShader.CreateTwoPointConicalGradient ( new SKPoint (115.2f, 102.4f), 25.6f, new SKPoint (102.4f, 102.4f), 128.0f, new [] {ltColor, dkColor}, null, SKShaderTileMode.Clamp )) { paint.Shader = shader; canvas.DrawOval (new SKRect (51.2f, 51.2f, 204.8f, 204.8f), paint); } } }
public static void BitmapShader (SKCanvas canvas, int width, int height) { var assembly = typeof(Demos).GetTypeInfo ().Assembly; var imageName = assembly.GetName ().Name + ".color-wheel.png"; // load the image from the embedded resource stream using (var resource = assembly.GetManifestResourceStream (imageName)) using (var stream = new SKManagedStream(resource)) using (var source = SKBitmap.Decode (stream)) { // create the shader and paint //SkMatrix matrix; //matrix.setScale(0.75f, 0.75f); //matrix.preRotate(30.0f); var matrix = SKMatrix.MakeRotation (30.0f); using (var shader = SKShader.CreateBitmap (source, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat, matrix)) using (var paint = new SKPaint ()) { paint.IsAntialias = true; paint.Shader = shader; // tile the bitmap canvas.Clear (SKColors.White); canvas.DrawPaint (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 static void BitmapShaderManipulated (SKCanvas canvas, int width, int height) { var assembly = typeof(Demos).GetTypeInfo ().Assembly; var imageName = assembly.GetName ().Name + ".color-wheel.png"; // load the image from the embedded resource stream using (var resource = assembly.GetManifestResourceStream (imageName)) using (var stream = new SKManagedStream(resource)) using (var source = SKBitmap.Decode (stream)) { // invert the pixels var pixels = source.Pixels; for (int i = 0; i < pixels.Length; i++) { pixels[i] = new SKColor ( (byte)(255 - pixels [i].Red), (byte)(255 - pixels [i].Green), (byte)(255 - pixels [i].Blue), pixels [i].Alpha); } source.Pixels = pixels; using (var shader = SKShader.CreateBitmap (source, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat)) using (var paint = new SKPaint ()) { paint.IsAntialias = true; paint.Shader = shader; // tile the bitmap canvas.Clear (SKColors.White); canvas.DrawPaint (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); } } } }
#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); } } }
#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); } } }