1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
-
- using System.Globalization;
- using System.Linq;
- using System.Windows;
- using System.Windows.Media;
- namespace FWindSoft.Wpf
- {
- /// <summary>
- /// 绘图相关方式
- /// </summary>
- public class DrawingUtil
- {
- /// <summary>
- /// 划线
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 画指定数量的文本内容
- /// </summary>
- /// <param name="text"></param>
- /// <param name="num"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|