public static void DrawCaptionLabels(this SKCanvas canvas, string label, SKColor labelColor, string value, SKColor valueColor, float textSize, SKPoint point, SKTextAlign horizontalAlignment) { var hasLabel = !string.IsNullOrEmpty(label); var hasValueLabel = !string.IsNullOrEmpty(value); if (hasLabel || hasValueLabel) { var hasOffset = hasLabel && hasValueLabel; var captionMargin = textSize * 0.60f; var space = hasOffset ? captionMargin : 0; if (hasLabel) { using (var paint = new SKPaint() { TextSize = textSize, IsAntialias = true, Color = labelColor, IsStroke = false, TextAlign = horizontalAlignment, }) { var bounds = new SKRect(); var text = label; paint.MeasureText(text, ref bounds); var y = point.Y - ((bounds.Top + bounds.Bottom) / 2) - space; canvas.DrawText(text, point.X, y, paint); } } if (hasValueLabel) { using (var paint = new SKPaint() { TextSize = textSize, IsAntialias = true, FakeBoldText = true, Color = valueColor, IsStroke = false, TextAlign = horizontalAlignment, }) { var bounds = new SKRect(); var text = value; paint.MeasureText(text, ref bounds); var y = point.Y - ((bounds.Top + bounds.Bottom) / 2) + space; canvas.DrawText(text, point.X, y, paint); } } } }
private void OnPaintCanvas(object sender, SKPaintSurfaceEventArgs e) { SKImageInfo imageInfo = e.Info; SKSurface surface = e.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); canvas.DrawBitmap(backgroundBitmap, new SKRect(0, 0, imageInfo.Width, imageInfo.Height)); SKTypeface typeface; using (var asset = Assets.Open("LittleBird.ttf")) { var fontStream = new MemoryStream(); asset.CopyTo(fontStream); fontStream.Flush(); fontStream.Position = 0; typeface = SKTypeface.FromStream(fontStream); } byte alpha = (byte)(100 + 155 * scale); SKPaint textPaint = new SKPaint { Style = SKPaintStyle.Fill, Typeface = typeface, Color = SKColors.White.WithAlpha(alpha), TextAlign = SKTextAlign.Right, TextSize = 64 }; canvas.DrawText("tap Screen to start playing...", imageInfo.Width - 20, imageInfo.Height / 6 * 5, textPaint); SKPaint rectPaint = new SKPaint { Style = SKPaintStyle.Fill, Color = SKColors.Black.WithAlpha(100) }; SKPaint scorePaint = new SKPaint { Color = SKColors.White, TextAlign = SKTextAlign.Right, TextSize = 32, Typeface = typeface }; scoreButton = new SKRect(imageInfo.Width - scorePaint.MeasureText("Ranking") - 30, 10, imageInfo.Width - 10, 68); canvas.DrawRoundRect(scoreButton, 5, 5, rectPaint); canvas.DrawText("Ranking", imageInfo.Width - 20, 48, scorePaint); if (username != "") { SKPaint usernamePaint = textPaint; usernamePaint.TextSize = 32; usernamePaint.Color = SKColors.Black; canvas.DrawText(username, scoreButton.Left - 20, 48, usernamePaint); } }
private void ReadTextSpans(XElement e, SKCanvas canvas, SKPoint location, SKPaint stroke, SKPaint fill) { var nodes = e.Nodes().ToArray(); for (int i = 0; i < nodes.Length; i++) { var c = nodes[i]; bool isFirst = i == 0; bool isLast = i == nodes.Length - 1; if (c.NodeType == XmlNodeType.Text) { // TODO: check for preserve whitespace var textSegments = ((XText)c).Value.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); var count = textSegments.Length; if (count > 0) { if (isFirst) { textSegments[0] = textSegments[0].TrimStart(); } if (isLast) { textSegments[count - 1] = textSegments[count - 1].TrimEnd(); } var text = WSRe.Replace(string.Concat(textSegments), " "); canvas.DrawText(text, location.X, location.Y, fill); location.X += fill.MeasureText(text); } } else if (c.NodeType == XmlNodeType.Element) { var ce = (XElement)c; if (ce.Name.LocalName == "tspan") { var spanFill = fill.Clone(); // the current span may want to change the cursor position location.X = ReadOptionalNumber(ce.Attribute("x")) ?? location.X; location.Y = ReadOptionalNumber(ce.Attribute("y")) ?? location.Y; ReadFontAttributes(ce, spanFill); var text = ce.Value.Trim(); canvas.DrawText(text, location.X, location.Y, spanFill); location.X += spanFill.MeasureText(text); } } } }
/// <summary> /// 获取验证码的另一种方式 /// </summary> /// <returns></returns> public IActionResult Code() { #region 反射SK支持的全部颜色 //List<SKColor> colors = new List<SKColor>(); //var skcolors = new SKColors(); //var type = skcolors.GetType(); //foreach (FieldInfo field in type.GetFields()) //{ // colors.Add( (SKColor)field.GetValue(skcolors)); //} #endregion //int maxcolorindex = colors.Count-1; string text = "1A3V"; var zu = text.ToList(); SKBitmap bmp = new SKBitmap(80, 30); using (SKCanvas canvas = new SKCanvas(bmp)) { //背景色 canvas.DrawColor(SKColors.White); using (SKPaint sKPaint = new SKPaint()) { sKPaint.TextSize = 16; //字体大小 sKPaint.IsAntialias = true; //开启抗锯齿 sKPaint.Typeface = SKTypeface.FromFamilyName("微软雅黑", SKTypefaceStyle.Bold); //字体 SKRect size = new SKRect(); sKPaint.MeasureText(zu[0].ToString(), ref size); //计算文字宽度以及高度 float temp = (bmp.Width / 4 - size.Size.Width) / 2; float temp1 = bmp.Height - (bmp.Height - size.Size.Height) / 2; Random random = new Random(); for (int i = 0; i < 4; i++) { sKPaint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255)); canvas.DrawText(zu[i].ToString(), temp + 20 * i, temp1, sKPaint);//画文字 } //干扰线 for (int i = 0; i < 5; i++) { sKPaint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255)); canvas.DrawLine(random.Next(0, 40), random.Next(1, 29), random.Next(41, 80), random.Next(1, 29), sKPaint); } } //页面展示图片 using (SKImage img = SKImage.FromBitmap(bmp)) { using (SKData p = img.Encode()) { return(File(p.ToArray(), "image/Png")); } } } }
protected void DrawLabels(SKCanvas canvas, SKPoint[] points, SKSize itemSize, int height, float footerHeight) { for (int i = 0; i < this.Entries.Count(); i++) { var entry = this.Entries.ElementAt(i); var point = points[i]; if (!string.IsNullOrEmpty(entry.Label)) { using (var paint = new SKPaint()) { paint.TextSize = this.LabelTextSize; paint.IsAntialias = true; paint.Color = entry.TextColor; paint.IsStroke = false; var bounds = new SKRect(); var text = entry.Label; paint.MeasureText(text, ref bounds); if (bounds.Width > itemSize.Width) { while (bounds.Width > itemSize.Width) { text = text.Substring(0, text.Length - 1); paint.MeasureText(text, ref bounds); } text = text.Substring(0, Math.Min(3, text.Length)); paint.MeasureText(text, ref bounds); } if (bounds.Width > itemSize.Width) { text = text.Substring(0, Math.Min(1, text.Length)); paint.MeasureText(text, ref bounds); } canvas.DrawText(text, point.X - (bounds.Width / 2), height - (this.MarginY + (this.LabelTextSize / 2)), paint); } } } }
public AnimatedRotation3DPage() { Title = "Animated Rotation 3D"; canvasView = new SKCanvasView(); canvasView.PaintSurface += OnCanvasViewPaintSurface; Content = canvasView; // Measure the text textPaint.MeasureText(text, ref textBounds); }
public Vector2 MeasureString([NotNull] Font font, [CanBeNull] string str, Vector2 maxBounds, [CanBeNull] StringFormat stringFormat = null) { var skBounds = new SKRect(0, 0, maxBounds.X, maxBounds.Y); using (var paint = new SKPaint()) { SetSKPaintFontProperties(paint, font, stringFormat); paint.MeasureText(str, ref skBounds); } return(new Vector2(skBounds.Width, skBounds.Height)); }
public SizeF MeasureString(string text, Font font) { using var paint = new SKPaint(); paint.TextEncoding = SKTextEncoding.Utf16; font.ApplyToSKPaint(paint, PageUnit, DpiX); SKRect bounds = SKRect.Empty; var width = paint.MeasureText(text, ref bounds); return(new SizeF(width, font.GetHeight(this))); }
public void PaintTextGL(object sender, SKSurface surface, GRBackendRenderTarget backendRenderTarget) { float dx = -10; float dy = 10; float sigmaX = 3; float sigmaY = 3; var Bind = sender as Xamarin.Forms.View; string text = (string)Bind.BindingContext; var canvas = surface.Canvas; canvas.Clear(); using (SKPaint textPaintFill = new SKPaint { Style = SKPaintStyle.Fill, IsAntialias = true, Color = SKColor.Parse("#E7D014") }) { using (SKPaint textPaint = new SKPaint { Style = SKPaintStyle.Stroke, StrokeWidth = 4, IsAntialias = true, Color = SKColor.Parse("#000") }) { textPaintFill.Typeface = GetTypeface("AmericanCaptain.ttf"); textPaint.Typeface = textPaintFill.Typeface; float textWidth = textPaintFill.MeasureText(text); textPaintFill.TextSize = 0.85f * backendRenderTarget.Width * textPaintFill.TextSize / textWidth; textPaint.TextSize = 0.85f * backendRenderTarget.Width * textPaint.TextSize / textWidth; SKRect textBounds = new SKRect(); textPaint.MeasureText(text, ref textBounds); textPaintFill.ImageFilter = SKImageFilter.CreateDropShadow( dx, dy, sigmaX, sigmaY, SKColor.Parse("#55000000")); textPaintFill.MeasureText(text, ref textBounds); float xText = backendRenderTarget.Width / 2 - textBounds.MidX; float yText = backendRenderTarget.Height / 2 - textBounds.MidY; canvas.DrawText(text, xText, yText, textPaint); canvas.DrawText(text, xText, yText, textPaintFill); } } }
public void SetText(string text) { var bmp = new SKBitmap((int)TextPaint.MeasureText(text) + 1, (int)TextPaint.FontSpacing + 1, SKColorType.Rgb888x, SKAlphaType.Opaque); using (var c = new SKCanvas(bmp)) { c.Clear(new SKColor(255, 255, 255)); c.DrawText(text, 0, TextPaint.FontSpacing - TextPaint.FontMetrics.Descent, TextPaint); } UploadTexture(bmp); }
public virtual /*interface Colorer*/ SKRect MeasureText(string text, SKPaint paint) { if (string.IsNullOrEmpty(text)) { return(new SKRect(0, 0, 0, 0)); // or MeasureText will crash } var bounds = new SKRect(); float length = paint.MeasureText(text, ref bounds); return(bounds); }
static void DrawText(SKCanvas canvas, string tag, float startLeft, float startTop, float scaledBoxWidth, float scaledBoxHeight) { var textPaint = new SKPaint { IsAntialias = true, Color = SKColors.White, Style = SKPaintStyle.Fill, Typeface = SKTypeface.FromFamilyName("Arial") }; var text = tag.Humanize(); var textWidth = textPaint.MeasureText(text); textPaint.TextSize = 0.9f * scaledBoxWidth * textPaint.TextSize / textWidth; var textBounds = new SKRect(); textPaint.MeasureText(text, ref textBounds); var xText = (startLeft + (scaledBoxWidth / 2)) - textBounds.MidX; var yText = (startTop + (scaledBoxHeight / 2)) + textBounds.MidY; var paint = new SKPaint { Style = SKPaintStyle.Fill, Color = new SKColor(0, 0, 0, 120) }; var backgroundRect = textBounds; backgroundRect.Offset(xText, yText); backgroundRect.Inflate(10, 10); canvas.DrawRoundRect(backgroundRect, 5, 5, paint); canvas.DrawText(text, xText, yText, textPaint); }
protected override void OnPaintSurface(SKPaintSurfaceEventArgs eventArgs) { var givenCanvas = eventArgs.Surface.Canvas; givenCanvas.Clear(); this.canvasWidth = eventArgs.Info.Width; this.canvasHeight = eventArgs.Info.Height; if (!this.HasShadow) { this.shadowPadding = 0; } // Draw the Checkbox container SKRoundRect roundRectCheckbox = new SKRoundRect(new SKRect(this.shadowPadding, 0, this.canvasWidth - this.shadowPadding, this.canvasHeight - (this.shadowPadding * 2)), ((this.canvasHeight - (this.shadowPadding * 2)) / 6), ((this.canvasHeight - (this.shadowPadding * 2)) / 6)); SKPaint paintCheckbox = new SKPaint() { Color = this.colorCurrent.ToSKColor(), Style = SKPaintStyle.Fill, IsAntialias = true }; if (this.HasShadow) { paintCheckbox.ImageFilter = SKImageFilter.CreateDropShadow( 0, 8, 0, 5, Color.FromRgba(0, 0, 0, 0.4).ToSKColor(), SKDropShadowImageFilterShadowMode.DrawShadowAndForeground ); } givenCanvas.DrawRoundRect(roundRectCheckbox, paintCheckbox); if (this.IsChecked) { SKPaint paintIcon = new SKPaint() { TextSize = 48f, IsAntialias = true, Color = this.ColorIcon.ToSKColor(), TextAlign = SKTextAlign.Center, Typeface = this.GetTypeface("fa-solid-900.ttf") }; SKRect rectIcon = new SKRect(); paintIcon.MeasureText("\uf00d", ref rectIcon); givenCanvas.DrawText("\uf00d", this.canvasWidth / 2, this.canvasHeight / 2 - rectIcon.MidY - this.shadowPadding, paintIcon); } }
private float GetLargestTextSize() { float largest_text_size = 0.0f; //Measure the headers if (DisplayHeaderRow) { using (var paint = new SKPaint() { Color = TableColumnHeaderTextColor, TextSize = TableColumnHeaderTextSize, IsAntialias = true, IsStroke = false }) { foreach (var h in TableColumnHeaders) { if (!string.IsNullOrEmpty(h)) { var text_width = paint.MeasureText(h); if (text_width > largest_text_size) { largest_text_size = text_width; } } } } } //Measure the table data using (var paint = new SKPaint() { Color = TableCellContentTextColor, TextSize = TableCellContentTextSize, IsAntialias = true, IsStroke = false }) { for (int r = 0; r < TableCellData.Count; r++) { for (int c = 0; c < TableCellData[r].Count; c++) { var text_width = paint.MeasureText(TableCellData[r][c].ToString()); if (text_width > largest_text_size) { largest_text_size = text_width; } } } } return(largest_text_size); }
private static SKRect MeasureRect(string text, SKPaint paint) { var rect = new SKRect(); paint.MeasureText(text, ref rect); var padding = 3; // todo get this from LabelStyle rect = SKRect.Inflate(rect, padding, padding); //rect = SnapToPixel(rect); return(rect); }
public static SKCanvas WriteSideDataInfo(this SKCanvas g, string title, string info, SKPaint font, float x, float y) { var brown = new SKColor(143, 89, 2); var gray = new SKColor(186, 189, 182); font.Color = brown; g.DrawText(title, x, y + font.TextSize, font); font.Color = gray; g.DrawText(info, x + font.MeasureText(title), y + font.TextSize, font); return(g); }
public ButtonContent(string text, SKPaint background, SKPaint forground, SKPaint border = null) { _text = text; _background = background; _forground = forground; _border = border; var bounds = new SKRect(); _forground.MeasureText(_text, ref bounds); _textSize = bounds.Size; }
public void Draw(SKCanvas c) { DrawCenteredTitle(c, DisplayName, 67.5f, out var textSize); Helper.DrawMultilineText(c, Description, Width, Margin, ETextSide.Center, new SKRect(Margin, textSize + 56.25f, Width - Margin, Height - 37.5f), descriptionPaint, out var yPos); if (IconImage != null) { c.DrawBitmap(IconImage, new SKRect(0, yPos, Width, Height), new SKPaint { FilterQuality = SKFilterQuality.High, IsAntialias = true }); } int yaPos = 0; foreach (Statistic ability in Abilities) { int xToAdd = ability.Icon != null ? ability.Icon.Width : 0; textSize = 42.5f; var namePaint = new SKPaint { IsAntialias = true, FilterQuality = SKFilterQuality.High, Typeface = Text.TypeFaces.DisplayNameTypeface, TextSize = textSize, Color = SKColors.White, TextAlign = SKTextAlign.Left, }; // resize if too long while (namePaint.MeasureText(ability.DisplayName) > Width - 128) { namePaint.TextSize = textSize -= 2; } c.DrawText(ability.DisplayName, Width + Margin + xToAdd + 10, yaPos + Margin + textSize, namePaint); Helper.DrawMultilineText(c, ability.Description, Width, Width + Margin + xToAdd + 10, ETextSide.Left, new SKRect(Width + Margin + xToAdd + 10, textSize + yaPos + 27.5f, Width + AdditionalWidth - Margin, Height - 27.5f), descriptionPaint, out var _); if (ability.Icon != null) { c.DrawBitmap(ability.Icon, new SKRect(Width + Margin, yaPos, Width + Margin + ability.Icon.Width, yaPos + ability.Icon.Height), new SKPaint { FilterQuality = SKFilterQuality.High, IsAntialias = true }); } yaPos += ability.Height + 48 + (Margin * 2); } }
public void Render(IStateOwner pOwner, SKCanvas pRenderTarget, GameplayGameState Source, GameStateSkiaDrawParameters Element) { var g = pRenderTarget; SKTypeface standardFont = TetrisGame.RetroFontSK; Type[] useTypes = new Type[] { typeof(Tetromino_I), typeof(Tetromino_O), typeof(Tetromino_J), typeof(Tetromino_T), typeof(Tetromino_L), typeof(Tetromino_S), typeof(Tetromino_Z) }; int[] PieceCounts = null; var useStats = Source.GameStats; var Bounds = Element.Bounds; var Factor = Bounds.Height / 280.28d; var DesiredFontPixelHeight = 22d; // PixelsToPoints((int)(Bounds.Height * (30d / 644d))); float DesiredFontSize = (float)(DesiredFontPixelHeight * pOwner.ScaleFactor); if (useStats is TetrisStatistics ts) { PieceCounts = new int[] { ts.I_Piece_Count, ts.O_Piece_Count, ts.J_Piece_Count, ts.T_Piece_Count, ts.L_Piece_Count, ts.S_Piece_Count, ts.Z_Piece_Count }; } else { PieceCounts = new int[] { 0, 0, 0, 0, 0, 0, 0 }; } float StartYPos = Bounds.Top; // + (int)(140 * Factor); float useXPos = Bounds.Left; // + (int)(30 * Factor); //ImageAttributes ShadowTet = TetrisGame.GetShadowAttributes(); for (int i = 0; i < useTypes.Length; i++) { if (Source.GameHandler is StandardTetrisHandler) { BlackBrush.TextSize = DesiredFontSize; WhiteBrush.TextSize = DesiredFontSize; SKPoint BaseCoordinate = new SKPoint(useXPos, StartYPos + (int)((float)i * (40d * Factor))); String StatText = "" + PieceCounts[i]; SKRect StatTextSize = new SKRect(); BlackBrush.MeasureText(StatText, ref StatTextSize); SKPoint TextPos = new SKPoint(useXPos + (int)(100d * Factor), BaseCoordinate.Y + StatTextSize.Height); //SizeF StatTextSize = g.MeasureString(StatText, standardFont); SKBitmap TetrominoImage = Source.GetTetrominoSKBitmap(useTypes[i]); PointF ImagePos = new PointF(BaseCoordinate.X, BaseCoordinate.Y + (StatTextSize.Height / 2 - TetrominoImage.Height / 2)); SKRect DrawRect = new SKRect(ImagePos.X, ImagePos.Y, ImagePos.X + TetrominoImage.Width * 1.5f, ImagePos.Y + TetrominoImage.Height * 1.5f); g.DrawBitmap(TetrominoImage, DrawRect, null); g.DrawTextSK(StatText, new SKPoint(Bounds.Left + TextPos.X + 4, Bounds.Top + TextPos.Y + 4), standardFont, SKColors.White, DesiredFontSize, pOwner.ScaleFactor); g.DrawTextSK(StatText, TextPos, standardFont, SKColors.Black, DesiredFontSize, pOwner.ScaleFactor); } //g.DrawString(StatText, standardFont, Brushes.White, new PointF(TextPos.X + 4, TextPos.Y + 4)); //g.DrawString(StatText, standardFont, Brushes.Black, TextPos); } }
public void Render(IStateOwner pOwner, SKCanvas pRenderTarget, CharParticle Source, GameStateSkiaDrawParameters Element) { var Alphause = TranslateAlpha(Source); var CharPoint = TranslatePosition(pOwner, pRenderTarget, Source.Position, Element); var FontSizeTranslate = new BCPoint(1, Source.FontInfo.FontSize); var TranslatedFontSize = TranslatePosition(pOwner, pRenderTarget, FontSizeTranslate, Element); var useColor = new SKColor(Source.Color.R, Source.Color.G, Source.Color.B, Alphause); SKRect Bound = new SKRect(); skp.MeasureText(Source.Text, ref Bound); skp.TextSize = TranslatedFontSize.Y; SKPaint Foreground = new SKPaint() { Color = useColor, TextSize = TranslatedFontSize.Y, Typeface = TetrisGame.RetroFontSK }; SKPaint Background = new SKPaint() { Color = new SKColor(0, 0, 0, Alphause), TextSize = TranslatedFontSize.Y, Typeface = TetrisGame.RetroFontSK }; DrawTextInformationSkia skinfo = new DrawTextInformationSkia() { Text = Source.Text, Position = CharPoint, CharacterHandler = new DrawCharacterHandlerSkia(new VerticalWavePositionCharacterPositionCalculatorSkia() { Height = (float)(pOwner.ScaleFactor * 6) }), ForegroundPaint = Foreground, ShadowPaint = Background }; skinfo.DrawFont = new SKFontInfo(TetrisGame.RetroFontSK, TranslatedFontSize.Y); //pRenderTarget.DrawText(Source.Text, CharPoint.X,CharPoint.Y,TetrisGame.RetroFontSK, skp); //pRenderTarget.DrawTextSK(Source.Text, CharPoint, TetrisGame.RetroFontSK, useColor, skp.TextSize,1); SKMatrix cloned = SKMatrix.Identity; if (Source.Angle != 0) { cloned = pRenderTarget.TotalMatrix; pRenderTarget.RotateDegrees((float)Source.Angle); } pRenderTarget.DrawTextSK(skinfo); if (Source.Angle != 0) { pRenderTarget.SetMatrix(cloned); } //CharPoint -= new BCPoint(skp) // skp.MeasureText(Source.Character); }
private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height, string?libraryName) { var bitmap = new SKBitmap(width, height); using var canvas = new SKCanvas(bitmap); canvas.Clear(SKColors.Black); using var backdrop = GetNextValidImage(paths, 0, out _); if (backdrop == null) { return(bitmap); } // resize to the same aspect as the original var backdropHeight = Math.Abs(width * backdrop.Height / backdrop.Width); using var residedBackdrop = SkiaEncoder.ResizeImage(backdrop, new SKImageInfo(width, backdropHeight, backdrop.ColorType, backdrop.AlphaType, backdrop.ColorSpace)); // draw the backdrop canvas.DrawImage(residedBackdrop, 0, 0); // draw shadow rectangle var paintColor = new SKPaint { Color = SKColors.Black.WithAlpha(0x78), Style = SKPaintStyle.Fill }; canvas.DrawRect(0, 0, width, height, paintColor); // draw library name var textPaint = new SKPaint { Color = SKColors.White, Style = SKPaintStyle.Fill, TextSize = 112, TextAlign = SKTextAlign.Center, Typeface = SKTypeface.FromFamilyName("sans-serif", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright), IsAntialias = true }; // scale down text to 90% of the width if text is larger than 95% of the width var textWidth = textPaint.MeasureText(libraryName); if (textWidth > width * 0.95) { textPaint.TextSize = 0.9f * width * textPaint.TextSize / textWidth; } canvas.DrawText(libraryName, width / 2f, (height / 2f) + (textPaint.FontMetrics.XHeight / 2), textPaint); return(bitmap); }
/// <summary> /// Renderer for PerformanceWidget /// </summary> /// <param name="x">X position of widget on screen</param> /// <param name="y">Y position of widget on screen</param> /// <param name="textSize">Size of text</param> /// <param name="textColor">Color for text</param> /// <param name="backgroundColor">Color for background</param> public PerformanceWidgetRenderer(float x, float y, int textSize, SKColor textColor, SKColor backgroundColor) { _textSize = textSize; _textPaint = new SKPaint { Color = textColor, TextSize = textSize, }; _backgroundPaint = new SKPaint { Color = backgroundColor, Style = SKPaintStyle.Fill, }; for (var i = 0; i < _textHeader.Length; i++) { _widthHeader = System.Math.Max(_widthHeader, _textPaint.MeasureText(_textHeader[5])); } var width = _widthHeader + 20 + _textPaint.MeasureText("0000.000"); _rect = new SKRect(x, y, x + width + 4, y + _textHeader.Length * (textSize + 2) - 2 + 4); _envelope = new MRect(_rect.Left, _rect.Top, _rect.Right, _rect.Bottom); }
public void Render(Label label, SKCanvas canvas, Size size) { // TODO: Need to work on layout, and adjust x/y points as determined by X.F. layout system using (var paint = new SKPaint()) { paint.TextSize = (float)label.FontSize * 4; // Note: Need to work on scaling factor for Android. x4 here is a just a workaround for my particular simulator device. paint.Color = SKColors.Black; var length = paint.MeasureText(label.Text); canvas.DrawText(label.Text, 0, paint.TextSize, paint); } }
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); const string TEXT1 = "CODE"; const string TEXT2 = "MORE"; using (SKPaint paint = new SKPaint()) { // Set text width to fit in width of canvas paint.TextSize = 100; float textWidth = paint.MeasureText(TEXT1); paint.TextSize *= 0.9f * info.Width / textWidth; // Center first text string SKRect textBounds = new SKRect(); paint.MeasureText(TEXT1, ref textBounds); float xText = info.Width / 2 - textBounds.MidX; float yText = info.Height / 2 - textBounds.MidY; paint.Color = SKColors.Blue.WithAlpha((byte)(0xFF * (1 - transparency))); canvas.DrawText(TEXT1, xText, yText, paint); // Center second text string textBounds = new SKRect(); paint.MeasureText(TEXT2, ref textBounds); xText = info.Width / 2 - textBounds.MidX; yText = info.Height / 2 - textBounds.MidY; paint.Color = SKColors.Blue.WithAlpha((byte)(0xFF * transparency)); canvas.DrawText(TEXT2, xText, yText, paint); } }
//----------------------------------------------------------------------------- // Call this whenever either the Source or the Label changes private void SetElementWidth() { if (Label == null) { return; // this will get called again when the Label is set. } // determine space needed for text -- units are actual device pixels using (SKPaint textPaint = new SKPaint()) { textPaint.TextSize = textPaint.TextSize * (float)DeviceDisplay.MainDisplayInfo.Density; textPaint.MeasureText("X", ref textBounds); textCoreHeight = textBounds.Height; textPaint.MeasureText(Label, ref textBounds); } float minWidth = textBounds.Width + DiuToPixels(40); // in actual device pixels // make sure minWidth is >= source bitmap width if (bmpSrc != null) { var h = DiuToPixels((float)this.Height); var scale = (float)h / (float)bmpSrc.Height; var scaledBmpWidth = scale * bmpSrc.Width; if (bmpSrc != null) { if (scaledBmpWidth > minWidth) { minWidth = scaledBmpWidth; } } } // set element width textBounds.Left = 0; textBounds.Right = minWidth; var w = PixelsToDiu(minWidth); WidthRequest = w; }
/// <summary> /// To the image source. /// </summary> /// <param name="icon">The icon.</param> /// <param name="size">The size.</param> /// <param name="color">The color.</param> /// <returns></returns> public static async Task <Windows.UI.Xaml.Media.ImageSource> ToImageSourceAsync(this IIcon icon, Int32 size, Color color) { var character = $"{icon.Character}"; var module = Iconize.FindModuleOf(icon); using (var surface = SKSurface.Create(size, size, SKImageInfo.PlatformColorType, SKAlphaType.Premul)) { using (var paint = new SKPaint()) { using (var typeface = SKTypeface.FromFile(Path.Combine(Package.Current.InstalledLocation.Path, module.GetType().GetTypeInfo().Assembly.GetName().Name, module.FontPath))) { paint.Color = color.ToSKColor(); paint.IsAntialias = true; paint.Typeface = typeface; // Adjust TextSize property so text is 90% of size var textWidth = paint.MeasureText(character); paint.TextSize = 0.9f * size * paint.TextSize / textWidth; // Find the text bounds var textBounds = new SKRect(); paint.MeasureText(character, ref textBounds); // Calculate offsets to center the text var xText = (size / 2) - textBounds.MidX; var yText = (size / 2) - textBounds.MidY; // And draw the text surface.Canvas.DrawText(character, xText, yText, paint); } } var bitmap = new BitmapImage(); await bitmap.SetSourceAsync(surface.Snapshot().Encode().AsStream().AsRandomAccessStream()); return(bitmap); } }
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); string text = Text; // Create an SKPaint object to display the text SKPaint textPaint = new SKPaint { //Style = SKPaintStyle.Stroke, //StrokeWidth = 1, FakeBoldText = Bold, Color = TextColor.ToSKColor() }; // Adjust TextSize property so text is 95% of screen width float textWidth = textPaint.MeasureText(text); textPaint.TextSize = 0.95f * info.Width * textPaint.TextSize / textWidth; // Find the text bounds SKRect textBounds = new SKRect(); textPaint.MeasureText(text, ref textBounds); // Calculate offsets to center the text on the screen float xText = info.Width / 2 - textBounds.MidX; float yText = info.Height / 2 - textBounds.MidY; // And draw the text canvas.DrawText(text, xText, yText, textPaint); }
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); float z = (float)zSlider.Value; float surfaceScale = (float)surfaceScaleSlider.Value; float lightConstant = (float)lightConstantSlider.Value; using (SKPaint paint = new SKPaint()) { paint.IsAntialias = true; // Size text to 90% of canvas width paint.TextSize = 100; float textWidth = paint.MeasureText(TEXT); paint.TextSize *= 0.9f * info.Width / textWidth; // Find coordinates to center text SKRect textBounds = new SKRect(); paint.MeasureText(TEXT, ref textBounds); float xText = info.Rect.MidX - textBounds.MidX; float yText = info.Rect.MidY - textBounds.MidY; // Create distant light image filter paint.ImageFilter = SKImageFilter.CreateDistantLitDiffuse( new SKPoint3(2, 3, z), SKColors.White, surfaceScale, lightConstant); canvas.DrawText(TEXT, xText, yText, paint); } }
protected override void OnPaintSurface(SKPaintSurfaceEventArgs e) { bool colorFlag = false; base.OnPaintSurface(e); SKImageInfo info = e.Info; SKSurface surface = e.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); var sw = Stopwatch.StartNew(); SKPaint paint = new SKPaint { Style = SKPaintStyle.Fill, TextSize = 48, StrokeWidth = 25 }; var circleCount = Math.Min(info.Width, info.Height) / 2; for (int count = circleCount; count >= 0; count -= 2) { paint.Color = (colorFlag ^= true) ? SKColors.Blue : SKColors.Red; canvas.DrawCircle(count, count, count, paint); } paint.Color = SKColors.Black; sw.Stop(); var str = $"{sw.ElapsedMilliseconds:#,###} ms."; float textWidth = paint.MeasureText(str); canvas.DrawText(str, info.Width / 2 - textWidth / 2, info.Height / 2, paint); var stream = new MemoryStream(); var tmp = new SkiaSharp.SKSvg(); tmp.Load(stream); var sm = new SKMatrix { ScaleX = 10, ScaleY = 10, TransX = 10, TransY = 10 }; canvas.DrawPicture(tmp.Picture, ref sm); }
private static void AddClassicTitleToChartImage(SKBitmap chartImage, LastAlbum album) { var textColor = chartImage.GetTextColor(); using var textPaint = new SKPaint { TextSize = 11, IsAntialias = true, Color = textColor, Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright) }; var artistBounds = new SKRect(); var albumBounds = new SKRect(); using var bitmapCanvas = new SKCanvas(chartImage); textPaint.MeasureText(album.ArtistName, ref artistBounds); textPaint.MeasureText(album.Name, ref albumBounds); bitmapCanvas.DrawText(album.ArtistName, 4, 12, textPaint); bitmapCanvas.DrawText(album.Name, 4, 22, textPaint); }