如何使用 .NET Core 绘图?

How to draw with .NET Core?

有什么方法可以使用 .NET Core 在屏幕上绘制和显示图形吗?我想创建一个在多个平台上运行的图形应用程序。

你可以actually use OpenGL to draw graphics with .NET Core, but it seems a bit cumbersome, if you are just committed to using C# and not .NET Core maybe Unity对你来说是更好的选择。

如果您尝试使用 GUI 元素制作 "desktop application",您还可以查看 Electron combined with TypeScript(有点类似于 C#),这就是他们制作 Visual Studio 代码的方式例如

编辑: 我刚刚发现了另一篇非常有趣的文章(作者是我在评论中提到的同一个人),名为 Building a 3D Game Engine with .NET Core,我很确定你可以从如何使用 OpenTK、Veldrid 和 ImGui.NET 在屏幕上绘图中获得一些灵感。

您可以使用https://www.nuget.org/packages/OpenTK.NetStandard/

说明:如何创建您的第一个 window OpenGL 图形

  • dotnet 新控制台
  • dotnet 添加包 OpenTK.NetStandard
  • 网络运行
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace dotnet_opentk
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var window = new Window())
            {
                window.Run();
            }
        }
    }

    class Window : GameWindow
    {
        protected override void OnLoad(System.EventArgs e)
        {
            GL.ClearColor(0.1f, 0.2f, 0.3f, 1f);

            Console.WriteLine(GL.GetString(StringName.Version));
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit);
            SwapBuffers();
        }
    }
}

另一个支持基本 2D 图形并侦听 window 事件(如输入)的库是 SFML,它具有 SFML.Net

形式的 C# 绑定

只需启动一个新的 NET Core 控制台应用程序并将 SFML.Net NuGet 包添加到项目中。

然后用以下代码替换程序的主体:

using SFML.Graphics;
using SFML.Window;
using System;

class Program
{
    static void Main(string[] args)
    {
        RenderWindow window = new RenderWindow(new VideoMode(640, 480), "This is a new window");

        CircleShape cs = new CircleShape(100.0f);
        cs.FillColor = Color.Green;

        window.SetActive();
        window.Closed += new EventHandler(OnClose);

        while (window.IsOpen)
        {
            window.Clear();
            window.DispatchEvents();
            window.Draw(cs);
            window.Display();
        }
    }

    static void OnClose(object sender, EventArgs e)
    {
        RenderWindow window = (RenderWindow)sender;
        window.Close();
    }
}

这会给你一个带有绿色圆圈的 window。当您关闭图形时 window 应用程序将关闭。

希望这能帮助您入门!

您可以使用 System.Drawing.Common NuGet 包支持 .net 核心,但请注意某些方法不支持跨平台。