我正在使用netDxf.dxf文件转换为位图,但我不知道如何获得折线的顶点坐标,然后使用.net绘制它.

using netDxf.Entities;
using netDxf;
using System.Drawing.Drawing2D;



namespace teste_bitmap
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadDxfFile();
        }

        private void LoadDxfFile()
        {
            string filePath = "C:/molde 2.dxf";

            try
            {
                DxfDocument dxfDocument = DxfDocument.Load(filePath);

                if (dxfDocument == null)
                {
                    Application.Exit();
                }

                else 
                {
                    // Get the Image object from DrawDxfEntities method
                    System.Drawing.Image image = DrawDxfEntities(dxfDocument);
                    // Display the image in a PictureBox
                    pictureBox1.Image = image;
                }

                string entityInfoText = string.Join(Environment.NewLine, entityInfos);
                MessageBox.Show(entityInfoText, "Entity Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch { }
        }

        public System.Drawing.Image DrawDxfEntities(DxfDocument dxfDocument)
        {
            Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.Clear(Color.White);

            foreach (var block in dxfDocument.Blocks)
            {
                foreach (var entity in block.Entities)
                {
                    DrawDxfEntity(graphics, entity);
                }
            }

            return bitmap;
        }

        static List<string> entityInfos = new List<string>();

        static void DrawDxfEntity(Graphics graphics, EntityObject entity)
        {

            string entityInfo = string.Empty;

            if (entity is Line line)
            {
                entityInfo = $"Processing Line: StartPoint=({line.StartPoint.X}, {line.StartPoint.Y}), EndPoint=({line.EndPoint.X}, {line.EndPoint.Y})";
                graphics.DrawLine(Pens.Black, (float)line.StartPoint.X, (float)line.StartPoint.Y, (float)line.EndPoint.X, (float)line.EndPoint.Y);
            }
            else if (entity is Circle circle)
            {
                entityInfo = $"Processing Circle: Center=({circle.Center.X}, {circle.Center.Y}), Radius={circle.Radius}";
                graphics.DrawEllipse(Pens.Black, (float)(circle.Center.X - circle.Radius), (float)(circle.Center.Y - circle.Radius),
                    (float)(2 * circle.Radius), (float)(2 * circle.Radius));
            }


            else if (entity is Arc arc)

            {

                entityInfo = $"Processing Arc: Start Angle=({arc.StartAngle}), End Angle=({arc.EndAngle}), Radius={arc.Radius}";
                double sweepAngle = arc.EndAngle - arc.StartAngle;
                arc.EndAngle = sweepAngle;

                graphics.DrawArc(Pens.Black, (float)(arc.Center.X - arc.Radius), (float)(arc.Center.Y - arc.Radius),

                    (float)(2 * arc.Radius), (float)(2 * arc.Radius), (float)arc.StartAngle, (float)arc.EndAngle);
            }

            else if (entity is Polyline2D polyline)
            {
                
            }

            entityInfos.Add(entityInfo);

        }
    }
}

我试图通过使用以下命令获取顶点 polyline.Vertexes个 但我不能画出它们 graphics.DrawLines() 因为它需要是PointF,而我无法得到每个顶点的坐标

推荐答案

可以try 将顶点坐标转换为PointF.

using System.Collections.Generic;

else if (entity is Polyline2D polyline)
{
    List<PointF> points = new List<PointF>();

    foreach (var vertex in polyline.Vertexes)
    {
        points.Add(new PointF((float)vertex.Position.X, (float)vertex.Position.Y));
    }

    entityInfo = $"Processing Polyline: Vertices={polyline.Vertexes.Count}";
    graphics.DrawLines(Pens.Black, points.ToArray());
}

使用此代码,您将迭代Polyline2D中的每个顶点,然后将坐标转换为PointF并将其添加到点列表中,最后转换为graphics.DrawLines()以绘制多段线.

Csharp相关问答推荐

System. InvalidOperationException:无法将数据库中的字符串值i转换为映射的ItemType枚举中的任何值''''

为什么Blazor值在更改后没有立即呈现?

. NET在上一个操作完成之前,在此上下文实例上启动了第二个操作

Blazor. NET 8—阶段启动配置文件不启动网站VS2022

不带身份的Blazor服务器.Net 8 Cookie身份验证

模型绑定RazorPage表单

为什么在使用动态obj+类obj时会调用串联?

使用预定义对象减少Task.Run/Factory.StartNew中的关闭开销

异步任务导致内存泄漏

C#普罗米修斯指标

在try 使用访问服务器上的文件夹时,如何解决CORS错误.NET核心API

有没有更好的方法来在CosmosDB上插入非id?

HttpClient SendAsync与多线程环境中的ArchiveZip

当我try 在与XAMP的MySQL服务器连接的ASP.NET核心应用程序中添加迁移时出现错误

JSON串行化程序问题.SQLite中的空值

在C#/ASP.NET Core 7中,什么可能导致POST请求作为GET请求发送

与另一个对象位于同一位置的对象具有不同的变换位置

外部应用&&的LINQ;左外部连接&类似于PostgreSQL的查询

如何对构建在Clean架构和CQRS之上的控制器进行单元测试?

为什么使用User.IsInRole()总是返回FALSE?