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; } }
public static void DilateImageFilter(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)) using (var filter = SKImageFilter.CreateDilate(5, 5)) using (var paint = new SKPaint()) { paint.ImageFilter = filter; canvas.DrawBitmap(bitmap, SKRect.Create(width, height), paint); } }
public static void ColorTableColorFilter(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".baboon.png"; var ct = new byte[256]; for (int i = 0; i < 256; ++i) { var x = (i - 96) * 255 / 64; ct[i] = x < 0 ? (byte)0 : x > 255 ? (byte)255 : (byte)x; } // 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)) using (var cf = SKColorFilter.CreateTable(null, ct, ct, ct)) using (var paint = new SKPaint()) { paint.ColorFilter = cf; canvas.DrawBitmap(bitmap, SKRect.Create(width, height), paint); } }
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]); } } } }
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 BitmapDecoder(SKCanvas canvas, int width, int height) { var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".color-wheel.png"; canvas.Clear(SKColors.White); // load the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var codec = SKCodec.Create(stream)) using (var paint = new SKPaint()) using (var tf = SKTypeface.FromFamilyName("Arial")) { var info = codec.Info; paint.IsAntialias = true; paint.TextSize = 14; paint.Typeface = tf; paint.Color = SKColors.Black; // decode the image using (var bitmap = new SKBitmap(info.Width, info.Height, info.ColorType, info.IsOpaque ? SKAlphaType.Opaque : SKAlphaType.Premul)) { IntPtr length; var result = codec.GetPixels(bitmap.Info, bitmap.GetPixels(out length)); if (result == SKCodecResult.Success || result == SKCodecResult.IncompleteInput) { var x = 25; var y = 25; canvas.DrawBitmap(bitmap, x, y); x += bitmap.Info.Width + 25; y += 14; canvas.DrawText(string.Format("Result: {0}", result), x, y, paint); y += 20; canvas.DrawText(string.Format("Size: {0}px x {1}px", bitmap.Width, bitmap.Height), x, y, paint); y += 20; canvas.DrawText(string.Format("Pixels: {0} @ {1}b/px", bitmap.Pixels.Length, bitmap.BytesPerPixel), x, y, paint); } } } }
public void Clear(Color color) => _canvas.Clear(color.ToSkia());
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]); } } } }
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 CustomFonts (SKCanvas canvas, int width, int height) { string text = "\u03A3 and \u0750"; using (var paint = new SKPaint ()) { canvas.Clear (SKColors.White); paint.IsAntialias = true; using (var tf = SKTypeface.FromFile (CustomFontPath)) { paint.Color = XamGreen; paint.TextSize = 40; paint.Typeface = tf; canvas.DrawText (text, 50, 50, paint); } using (var stream = new SKFileStream (CustomFontPath)) using (var tf = SKTypeface.FromStream (stream)) { paint.Color = XamDkBlue; paint.TextSize = 40; paint.Typeface = tf; canvas.DrawText (text, 50, 100, paint); } var assembly = typeof(Demos).GetTypeInfo ().Assembly; var fontName = assembly.GetName ().Name + ".embedded-font.ttf"; using (var resource = assembly.GetManifestResourceStream (fontName)) using (var memory = new MemoryStream ()) { resource.CopyTo (memory); var bytes = memory.ToArray (); using (var stream = new SKMemoryStream (bytes)) using (var tf = SKTypeface.FromStream (stream)) { paint.Color = XamLtBlue; paint.TextSize = 40; paint.Typeface = tf; canvas.DrawText (text, 50, 150, paint); } } using (var resource = assembly.GetManifestResourceStream (fontName)) using (var stream = new SKManagedStream (resource)) using (var tf = SKTypeface.FromStream (stream)) { paint.Color = XamPurple; paint.TextSize = 40; paint.Typeface = tf; canvas.DrawText (text, 50, 200, paint); } } }
public static void ManageDrawMatrix (SKCanvas canvas, int width, int height) { var size = ((float)height > width ? width : height) * 0.5f; var center = new SKPoint ((width - size) / 2f, (height - size) / 2f); // draw these at specific locations var leftRect = SKRect.Create (center.X - size / 2f, center.Y, size, size); var rightRect = SKRect.Create (center.X + size / 2f, center.Y, size, size); // draw this at the current location / transformation var rotatedRect = SKRect.Create (0f, 0f, size, size); using (var paint = new SKPaint ()) { paint.IsAntialias = true; canvas.Clear (XamPurple); // draw paint.Color = XamDkBlue; canvas.DrawRect (leftRect, paint); // save canvas.Save(); // transform canvas.Translate (width / 2f, center.Y); canvas.RotateDegrees (45); // draw paint.Color = XamGreen; canvas.DrawRect (rotatedRect, paint); // undo transform / restore canvas.Restore(); // draw paint.Color = XamLtBlue; canvas.DrawRect (rightRect, paint); } }
public static void FilledHeptagram (SKCanvas canvas, int width, int height) { var size = ((float)height > width ? width : height) * 0.75f; var R = 0.45f * size; var TAU = 6.2831853f; using (var path = new SKPath ()) { path.MoveTo (R, 0.0f); for (int i = 1; i < 7; ++i) { var theta = 3f * i * TAU / 7f; path.LineTo (R * (float)Math.Cos (theta), R * (float)Math.Sin (theta)); } path.Close (); using (var paint = new SKPaint ()) { paint.IsAntialias = true; canvas.Clear (SKColors.White); canvas.Translate (width / 2f, height / 2f); canvas.DrawPath (path, paint); } } }
public static void PathEffects (SKCanvas canvas, int width, int height) { canvas.Clear (SKColors.White); var step = height / 4; using (var paint = new SKPaint ()) using (var effect = SKPathEffect.CreateDash (new [] { 15f, 5f }, 0)) { paint.IsStroke = true; paint.StrokeWidth = 4; paint.PathEffect = effect; canvas.DrawLine (10, step, width - 10 - 10, step, paint); } using (var paint = new SKPaint ()) using (var effect = SKPathEffect.CreateDiscrete (10, 10)) { paint.IsStroke = true; paint.StrokeWidth = 4; paint.PathEffect = effect; canvas.DrawLine (10, step * 2, width - 10 - 10, step * 2, paint); } using (var paint = new SKPaint ()) using (var dashEffect = SKPathEffect.CreateDash (new [] { 15f, 5f }, 0)) using (var discreteEffect = SKPathEffect.CreateDiscrete (10, 10)) using (var effect = SKPathEffect.CreateCompose (dashEffect, discreteEffect)) { paint.IsStroke = true; paint.StrokeWidth = 4; paint.PathEffect = effect; canvas.DrawLine (10, step * 3, width - 10 - 10, step * 3, paint); } }
public static void CreatePdfSample (SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); using (var paint = new SKPaint ()) { paint.TextSize = 24.0f; paint.IsAntialias = true; paint.Color = new SKColor (0x9C, 0xAF, 0xB7); paint.StrokeWidth = 3; paint.TextAlign = SKTextAlign.Center; canvas.DrawText ("tap to open", width / 2f, 100f, paint); } using (var stream = new SKFileWStream (Path.Combine (WorkingDirectory, "document.pdf"))) using (var document = SKDocument.CreatePdf (stream)) using (var pdfCanvas = document.BeginPage (width, height)) using (var paint = new SKPaint ()) { paint.TextSize = 64.0f; paint.IsAntialias = true; paint.Color = new SKColor (0x9C, 0xAF, 0xB7); paint.IsStroke = true; paint.StrokeWidth = 3; paint.TextAlign = SKTextAlign.Center; pdfCanvas.DrawText ("...PDF...", width / 2f, 100f, paint); document.EndPage (); document.Close (); } }
public static void ChainedImageFilter(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".baboon.png"; var size = width > height ? height : width; var small = size / 5; // 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)) using (var filterMag = SKImageFilter.CreateMagnifier(SKRect.Create(small*2, small*2, small*3, small*3), small)) using (var filterBlur = SKImageFilter.CreateBlur(5, 5, filterMag)) using (var paint = new SKPaint()) { paint.ImageFilter = filterBlur; canvas.DrawBitmap(bitmap, SKRect.Create(width, height), 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 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 BitmapDecoder(SKCanvas canvas, int width, int height) { var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".color-wheel.png"; canvas.Clear(SKColors.White); // load the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var decoder = new SKImageDecoder(stream)) using (var paint = new SKPaint()) using (var tf = SKTypeface.FromFamilyName("Arial")) { paint.IsAntialias = true; paint.TextSize = 14; paint.Typeface = tf; paint.Color = SKColors.Black; // read / set decoder settings decoder.DitherImage = true; decoder.PreferQualityOverSpeed = true; decoder.SampleSize = 2; // decode the image using (var bitmap = new SKBitmap()) { var result = decoder.Decode(stream, bitmap); if (result != SKImageDecoderResult.Failure) { var info = bitmap.Info; var x = 25; var y = 25; canvas.DrawBitmap(bitmap, x, y); x += info.Width + 25; y += 14; canvas.DrawText(string.Format("Result: {0}", result), x, y, paint); y += 20; canvas.DrawText(string.Format("Size: {0}px x {1}px", bitmap.Width, bitmap.Height), x, y, paint); y += 20; canvas.DrawText(string.Format("Pixels: {0} @ {1}b/px", bitmap.Pixels.Length, bitmap.BytesPerPixel), x, y, paint); } } } }
public DrawingContextImpl(SKCanvas canvas) { Canvas = canvas; Canvas.Clear(); }
public static void BitmapLattice (SKCanvas canvas, int width, int height) { canvas.Clear (SKColors.White); var assembly = typeof (Demos).GetTypeInfo ().Assembly; var imageName = assembly.GetName ().Name + ".nine-patch.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 patchCenter = new SKRectI (33, 33, 256 - 33, 256 - 33); // 2x3 for portrait, or 3x2 for landscape var land = width > height; var min = land ? Math.Min (width / 3f, height / 2f) : Math.Min (width / 2f, height / 3f); var wide = SKRect.Inflate (SKRect.Create (0, land ? min : (min * 2f), min * 2f, min), -6, -6); var tall = SKRect.Inflate (SKRect.Create (land ? (min * 2f) : min, 0, min, min * 2f), -6, -6); var square = SKRect.Inflate (SKRect.Create (0, 0, min, min), -6, -6); var text = SKRect.Create (land ? min : 0, land ? 0 : min, min, min / 5f); text.Offset (text.Width / 2f, text.Height * 1.5f); text.Right = text.Left; // draw the bitmaps canvas.DrawBitmapNinePatch (bitmap, patchCenter, square); canvas.DrawBitmapNinePatch (bitmap, patchCenter, tall); canvas.DrawBitmapNinePatch (bitmap, patchCenter, wide); // describe what we see using (var paint = new SKPaint ()) { paint.TextAlign = SKTextAlign.Center; paint.TextSize = text.Height * 0.75f; canvas.DrawText ("The corners", text.Left, text.Top, paint); text.Offset (0, text.Height); canvas.DrawText ("should always", text.Left, text.Top, paint); text.Offset (0, text.Height); canvas.DrawText ("be square", text.Left, text.Top, paint); } } }