早前,我曾写过一篇:“在WPF中制作正圆形公章”。

有空再次研究,使用C#将此WPF程序写了出来。


运行效果图:

WPF公章制作之2_string



关键C#代码:
// OfficialSeal.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using brawdrawSharp = BrawDraw.Com.Utility.PublicClasses.Shape;
namespace BrawDraw.Com.WPF.PublicControls.Demo
{
public class OfficialSeal : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new OfficialSeal());
} public OfficialSeal()
{
string officialSealText = "BRAWDRAW图文印章示例";
Title = officialSealText; Canvas canv = new Canvas();
Content = canv;
Ellipse ellipse = new Ellipse();
ellipse.Width = 400;
ellipse.Height = 400;
ellipse.Stroke = new SolidColorBrush(Colors.Red);
ellipse.StrokeThickness = 2;
Canvas.SetLeft(ellipse, 10);
canv.Children.Add(ellipse); double angleAdd = 236.00 / officialSealText.Length;
int i = 0;
for (double angle = -112; angle < 123; angle += angleAdd)
{
TextBlock txtblk = new TextBlock();
txtblk.FontFamily = new FontFamily("方正大标宋简体,黑体,宋体");
txtblk.FontSize = 56;
txtblk.Foreground = new SolidColorBrush(Colors.Red);
txtblk.Text = officialSealText[i].ToString();
txtblk.RenderTransformOrigin = new Point(0.5, 0);
TransformGroup tg = new TransformGroup();
ScaleTransform st = new ScaleTransform(0.66, 1);
TranslateTransform tt = new TranslateTransform(0, -188);
tg.Children.Add(st);
tg.Children.Add(tt);
tg.Children.Add(new RotateTransform(angle));
txtblk.RenderTransform = tg;
canv.Children.Add(txtblk);
Canvas.SetLeft(txtblk, 180);
Canvas.SetTop(txtblk, 200);
i++;
} Path myPath = new Path();
myPath.Stroke = Brushes.Red;
myPath.StrokeThickness = 1;// 正五角星
StreamGeometry theGeometry = BuildPentagonalStars(new Point(180, 168), 80, 80);
theGeometry.FillRule = FillRule.EvenOdd;
theGeometry.Freeze();
myPath.Data = theGeometry;
myPath.Fill = Brushes.Red;
canv.Children.Add(myPath);
} StreamGeometry BuildPentagonalStars(Point location, int width, int height)
{
Point[] pointsPentagonalStars = brawdrawSharp.RegularPolygon.GetStarPoints(location, width, height);
StreamGeometry geometry = new StreamGeometry(); using (StreamGeometryContext ctx = geometry.Open())
{
ctx.BeginFigure(new Point(pointsPentagonalStars[0].X, pointsPentagonalStars[0].Y), true, true);
for (int i = 0; i < pointsPentagonalStars.Length; i++)
{
ctx.LineTo(pointsPentagonalStars[i], true, false);
}
} return geometry;
}

}
}



关于StreamGeometry相关文章参考:
如何:使用 StreamGeometry 创建形状 ​​​http://msdn2.microsoft.com/zh-cn/library/ms742199.aspx​​​
WPF中图形表示语法详解(Path之Data属性语法)