DrawingUtil.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Media;
  6. namespace FWindSoft.Wpf
  7. {
  8. /// <summary>
  9. /// 绘图相关方式
  10. /// </summary>
  11. public class DrawingUtil
  12. {
  13. /// <summary>
  14. /// 划线
  15. /// </summary>
  16. /// <returns></returns>
  17. public static DrawingGroup DrawLine()
  18. {
  19. DrawingGroup drawing = new DrawingGroup();
  20. using (DrawingContext dc = drawing.Open())
  21. {
  22. dc.DrawLine(new Pen(new SolidColorBrush(Colors.Black), 1), new Point(0, 9), new Point(100, 9));
  23. }
  24. return drawing;
  25. }
  26. /// <summary>
  27. /// 画指定数量的文本内容
  28. /// </summary>
  29. /// <param name="text"></param>
  30. /// <param name="num"></param>
  31. /// <returns></returns>
  32. public static DrawingGroup DrawLineText(string text, int num)
  33. {
  34. DrawingGroup drawing = new DrawingGroup(){};
  35. using (DrawingContext dc = drawing.Open())
  36. {
  37. dc.DrawLine(new Pen(new SolidColorBrush(Colors.Black), 1), new Point(0, 9), new Point(100, 9));
  38. Typeface typeFace = Fonts.SystemTypefaces.FirstOrDefault(face => face.FontFamily.FamilyNames.Values.Contains("宋体"));
  39. if (typeFace == null)
  40. typeFace = Fonts.SystemTypefaces.FirstOrDefault();
  41. double fontSize = 10;
  42. FormattedText formatted = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
  43. typeFace, fontSize, Brushes.Black);
  44. double reviseX = formatted.Width / 2;
  45. double reviseY = 9 - formatted.Height / 2;
  46. double space = 100d / (num + 1);
  47. for (int i = 0; i < num; i++)
  48. {
  49. Point point = new Point(space * (i + 1) - reviseX, reviseY);
  50. dc.DrawText(formatted, point);
  51. }
  52. }
  53. return drawing;
  54. }
  55. }
  56. }