将GraphicsPath转为SVG代码

GraphicsPathToSVG(c#代码)

       public static string GraphicsPathToSVG(GraphicsPath path)
        {
            PathData pathData = path.PathData;
            StringBuilder def = new System.Text.StringBuilder();
            int i = 0;

            byte bytType;
            int cCount = 0;

            string strX = null;
            string strY = null;

            def.Append("<path ");
            //if (IncludeId) def.Append("id=\"\" ");
            def.Append("fill-rule=\"evenodd\"");
            def.Append(" style=\"fill:orange;stroke:red;stroke-width:1\"  d=\"");

            for (i = 0; i <= pathData.Points.GetUpperBound(0); i++)
            {
                bytType = pathData.Types[i];
                strX = XmlConvert.ToString(Math.Round(pathData.Points[i].X, 2));
                strY = XmlConvert.ToString(Math.Round(pathData.Points[i].Y, 2));

                if (bytType == 0) //Moveto
                {
                    def.Append("M ");
                    def.Append(strX);
                    def.Append(",");
                    def.Append(strY);
                    def.Append(" ");
                    cCount = 0;
                }
                else
                {
                    if (bytType == 1) //Lineto
                    {
                        def.Append("L ");
                        def.Append(strX);
                        def.Append(",");
                        def.Append(strY);
                        def.Append(" ");
                        cCount = 0;
                    }
                    else
                    {
                        if (bytType > 128) //lineto or curve and closepath
                        {
                            if (cCount == 0)                            
                                def.Append("L ");                            
                            def.Append(strX);
                            def.Append(",");
                            def.Append(strY);
                            def.Append(" Z ");
                            cCount = 0;
                        }
                        else
                        {
                            if (cCount == 0) //Curve, in groups of 3                            
                                def.Append("C ");                            
                            def.Append(strX);
                            def.Append(",");
                            def.Append(strY);
                            def.Append(" ");
                            cCount += 1;
                            if (cCount == 3)                            
                                cCount = 0;                            
                        }
                    }
                }
            }

            def.Append("\"/>");

            return def.ToString();
        }

相关链接:

生成GraphicsPath  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`GraphicsPath` 是一个用于创建和操作图形路径的类,它属于 `System.Drawing.Drawing2D` 命名空间。路径是由一系列线条和曲线组成的形状,可以用于绘制、填充和剪裁图形。 `GraphicsPath` 类提供了一组方法来创建和修改路径,例如添加直线、曲线、矩形、椭圆等形状。你可以使用这些方法构建任意复杂的路径,然后将其应用于绘图操作。 路径可以用于各种绘图操作,例如绘制自定义形状的控件、绘制自由绘图、裁剪区域等。通过创建和操作路径,你可以实现各种有趣的图形效果和形状。 以下是一个简单的示例,展示如何使用 `GraphicsPath` 创建一个椭圆形的路径: ```csharp using System.Drawing; using System.Drawing.Drawing2D; // 创建一个 GraphicsPath 对象 GraphicsPath path = new GraphicsPath(); // 添加一个椭圆形到路径中 Rectangle rectangle = new Rectangle(50, 50, 100, 50); path.AddEllipse(rectangle); // 使用路径进行绘图操作 Graphics graphics = this.CreateGraphics(); Pen pen = new Pen(Color.Black, 2); graphics.DrawPath(pen, path); // 清理资源 path.Dispose(); pen.Dispose(); graphics.Dispose(); ``` 在上面的示例中,我们首先创建了一个 `GraphicsPath` 对象 `path`,然后使用 `AddEllipse` 方法将一个椭圆形添加到路径中。最后,使用路径和 `DrawPath` 方法绘制椭圆形的边框。最后,我们释放了所使用的资源,以避免内存泄漏。 通过 `GraphicsPath` 类的其他方法,你可以添加直线、曲线、矩形、多边形等形状到路径中,并进行组合、变换、剪裁等操作,以实现更复杂的图形效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值