using System.Globalization; using System.Linq; using System.Windows; using System.Windows.Media; namespace FWindSoft.Wpf { /// /// 绘图相关方式 /// public class DrawingUtil { /// /// 划线 /// /// public static DrawingGroup DrawLine() { DrawingGroup drawing = new DrawingGroup(); using (DrawingContext dc = drawing.Open()) { dc.DrawLine(new Pen(new SolidColorBrush(Colors.Black), 1), new Point(0, 9), new Point(100, 9)); } return drawing; } /// /// 画指定数量的文本内容 /// /// /// /// public static DrawingGroup DrawLineText(string text, int num) { DrawingGroup drawing = new DrawingGroup(){}; using (DrawingContext dc = drawing.Open()) { dc.DrawLine(new Pen(new SolidColorBrush(Colors.Black), 1), new Point(0, 9), new Point(100, 9)); Typeface typeFace = Fonts.SystemTypefaces.FirstOrDefault(face => face.FontFamily.FamilyNames.Values.Contains("宋体")); if (typeFace == null) typeFace = Fonts.SystemTypefaces.FirstOrDefault(); double fontSize = 10; FormattedText formatted = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeFace, fontSize, Brushes.Black); double reviseX = formatted.Width / 2; double reviseY = 9 - formatted.Height / 2; double space = 100d / (num + 1); for (int i = 0; i < num; i++) { Point point = new Point(space * (i + 1) - reviseX, reviseY); dc.DrawText(formatted, point); } } return drawing; } } }