public static void UnicodeText (SKCanvas canvas, int width, int height) { // stops at 0x530 on Android string text = "\u03A3 and \u0750"; using (var paint = new SKPaint ()) using (var tf = SKTypeface.FromFamilyName ("Tahoma")) { canvas.DrawColor (SKColors.White); paint.IsAntialias = true; paint.TextSize = 60; paint.Typeface = tf; canvas.DrawText (text, 50, 100, paint); } using (var paint = new SKPaint ()) using (var tf = SKTypeface.FromFamilyName ("Times New Roman")) { paint.Color = XamDkBlue; paint.IsAntialias = true; paint.TextSize = 60; paint.Typeface = tf; canvas.DrawText (text, 50, 200, paint); } }
public static void MeasureTextSample (SKCanvas canvas, int width, int height) { canvas.DrawColor (SKColors.White); using (var paint = new SKPaint ()) { paint.TextSize = 64.0f; paint.IsAntialias = true; paint.Color = new SKColor (0x42, 0x81, 0xA4); paint.TextEncoding = SKTextEncoding.Utf32; canvas.DrawText ("Skia (UTF-32)", 0, 64.0f, paint); var bounds = new SKRect(); paint.MeasureText ("Skia (UTF-32)", ref bounds); bounds.Top += 64.0f; bounds.Bottom += 64.0f; paint.IsStroke = true; paint.Color = SKColors.Red; canvas.DrawRect (bounds, paint); } using (var paint = new SKPaint ()) { paint.TextSize = 64.0f; paint.IsAntialias = true; paint.Color = new SKColor (0x9C, 0xAF, 0xB7); paint.TextEncoding = SKTextEncoding.Utf16; canvas.DrawText ("Skia (UTF-16)", 0, 144.0f, paint); var bounds = new SKRect(); paint.MeasureText ("Skia (UTF-16)", ref bounds); bounds.Top += 144.0f; bounds.Bottom += 144.0f; paint.IsStroke = true; paint.Color = SKColors.Red; canvas.DrawRect (bounds, paint); } using (var paint = new SKPaint ()) { paint.TextSize = 64.0f; paint.IsAntialias = true; paint.Color = new SKColor (0xE6, 0xB8, 0x9C); paint.TextEncoding = SKTextEncoding.Utf8; canvas.DrawText ("Skia (UTF-8)", 0, 224.0f, paint); var bounds = new SKRect(); paint.MeasureText ("Skia (UTF-8)", ref bounds); bounds.Top += 224.0f; bounds.Bottom += 224.0f; paint.IsStroke = true; paint.Color = SKColors.Red; canvas.DrawRect (bounds, paint); } }
public static void UnicodeText(SKCanvas canvas, int width, int height) { // stops at 0x530 on Android string text = "\u03A3 and \u0750"; using (var paint = new SKPaint()) using (var tf = SKTypeface.FromFamilyName("Tahoma")) { canvas.DrawColor(SKColors.White); paint.IsAntialias = true; paint.TextSize = 60; paint.Typeface = tf; canvas.DrawText(text, 50, 100, paint); } using (var paint = new SKPaint()) using (var tf = SKTypeface.FromFamilyName("Times New Roman")) { paint.Color = XamDkBlue; paint.IsAntialias = true; paint.TextSize = 60; paint.Typeface = tf; canvas.DrawText(text, 50, 200, paint); } }
// https://fiddle.skia.org/c/3523e95a9f8b96228b6b4bdd5409cb94 public static void TextSample(SKCanvas canvas, int width, int height) { canvas.DrawColor(SKColors.White); using (var paint = new SKPaint()) { paint.TextSize = 64.0f; paint.IsAntialias = true; paint.Color = new SKColor(0x42, 0x81, 0xA4); paint.IsStroke = false; canvas.DrawText("Skia", width / 2f, 64.0f, paint); } 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; canvas.DrawText("Skia", width / 2f, 144.0f, paint); } using (var paint = new SKPaint()) { paint.TextSize = 64.0f; paint.IsAntialias = true; paint.Color = new SKColor(0xE6, 0xB8, 0x9C); paint.TextScaleX = 1.5f; paint.TextAlign = SKTextAlign.Right; canvas.DrawText("Skia", width / 2f, 224.0f, paint); } }
public static void BlurMaskFilter(SKCanvas canvas, int width, int height) { canvas.DrawColor(SKColors.White); using (var paint = new SKPaint()) using (var filter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 5.0f)) { paint.IsAntialias = true; paint.TextSize = 120; paint.TextAlign = SKTextAlign.Center; paint.MaskFilter = filter; canvas.DrawText("Skia", width / 2f, height / 2f, paint); } }
public static void EmbossMaskFilter(SKCanvas canvas, int width, int height) { canvas.DrawColor(SKColors.White); SKPoint3 direction = new SKPoint3(1.0f, 1.0f, 1.0f); using (var paint = new SKPaint()) using (var filter = SKMaskFilter.CreateEmboss(2.0f, direction, 0.3f, 0.1f)) { paint.IsAntialias = true; paint.TextSize = 120; paint.TextAlign = SKTextAlign.Center; paint.MaskFilter = filter; canvas.DrawText("Skia", width / 2f, height / 2f, paint); } }
} // 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
// 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); }
public static void MeasureTextSample(SKCanvas canvas, int width, int height) { canvas.DrawColor(SKColors.White); using (var paint = new SKPaint()) { paint.TextSize = 64.0f; paint.IsAntialias = true; paint.Color = new SKColor(0x42, 0x81, 0xA4); paint.TextEncoding = SKTextEncoding.Utf32; canvas.DrawText("Skia (UTF-32)", 0, 64.0f, paint); var bounds = new SKRect(); paint.MeasureText("Skia (UTF-32)", ref bounds); bounds.Top += 64.0f; bounds.Bottom += 64.0f; paint.IsStroke = true; paint.Color = SKColors.Red; canvas.DrawRect(bounds, paint); } using (var paint = new SKPaint()) { paint.TextSize = 64.0f; paint.IsAntialias = true; paint.Color = new SKColor(0x9C, 0xAF, 0xB7); paint.TextEncoding = SKTextEncoding.Utf16; canvas.DrawText("Skia (UTF-16)", 0, 144.0f, paint); var bounds = new SKRect(); paint.MeasureText("Skia (UTF-16)", ref bounds); bounds.Top += 144.0f; bounds.Bottom += 144.0f; paint.IsStroke = true; paint.Color = SKColors.Red; canvas.DrawRect(bounds, paint); } using (var paint = new SKPaint()) { paint.TextSize = 64.0f; paint.IsAntialias = true; paint.Color = new SKColor(0xE6, 0xB8, 0x9C); paint.TextEncoding = SKTextEncoding.Utf8; canvas.DrawText("Skia (UTF-8)", 0, 224.0f, paint); var bounds = new SKRect(); paint.MeasureText("Skia (UTF-8)", ref bounds); bounds.Top += 224.0f; bounds.Bottom += 224.0f; paint.IsStroke = true; paint.Color = SKColors.Red; canvas.DrawRect(bounds, 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 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); } } } }
// https://fiddle.skia.org/c/3523e95a9f8b96228b6b4bdd5409cb94 public static void TextSample (SKCanvas canvas, int width, int height) { canvas.DrawColor (SKColors.White); using (var paint = new SKPaint ()) { paint.TextSize = 64.0f; paint.IsAntialias = true; paint.Color = new SKColor (0x42, 0x81, 0xA4); paint.IsStroke = false; canvas.DrawText ("Skia", width / 2f, 64.0f, paint); } 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; canvas.DrawText ("Skia", width / 2f, 144.0f, paint); } using (var paint = new SKPaint ()) { paint.TextSize = 64.0f; paint.IsAntialias = true; paint.Color = new SKColor (0xE6, 0xB8, 0x9C); paint.TextScaleX = 1.5f; paint.TextAlign = SKTextAlign.Right; canvas.DrawText ("Skia", width / 2f, 224.0f, paint); } }