Advertisement
Guest User

Untitled

a guest
May 25th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. void Main()
  2. {
  3. DrawTextToPng("Hogemoge-fugaf@-!!!$&'&piyo_-=hiogfew", 10);
  4. }
  5.  
  6. public void DrawTextToPng(string text, int xPadding = 0)
  7. {
  8. // draw some text
  9. var paint = new SKPaint
  10. {
  11. Color = SKColors.White,
  12. IsAntialias = true,
  13. Style = SKPaintStyle.Fill,
  14. TextAlign = SKTextAlign.Center,
  15. TextSize = 24
  16. };
  17. var width = paint.MeasureText(text);
  18.  
  19. var info = new SKImageInfo((int)width + xPadding, 50);
  20. using (var surface = SKSurface.Create(info))
  21. {
  22. var canvas = surface.Canvas;
  23.  
  24. // make sure the canvas is blank
  25. canvas.Clear(SKColors.Black);
  26.  
  27. var coord = new SKPoint(info.Width / 2, (info.Height + paint.TextSize) / 2);
  28. canvas.DrawText(text, coord, paint);
  29.  
  30. // save the file
  31. using (var image = surface.Snapshot())
  32. using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
  33. using (var stream = File.OpenWrite("output.png"))
  34. {
  35. data.SaveTo(stream);
  36. }
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement