SuperJMN
SuperJMN

Reputation: 13972

How to draw with .NET Core?

Is there any way to draw and display graphics on the screen with .NET Core? I would like to create a graphics application that runs on multiple platforms.

Upvotes: 21

Views: 19657

Answers (4)

Mehdi
Mehdi

Reputation: 1126

You can use System.Drawing.Common NuGet package supports .net core however be aware some methods are not supported cross-platform.

Upvotes: 3

Nino van der Mark
Nino van der Mark

Reputation: 721

Another library that supports basic 2D graphics, and listening for window events like input, is SFML which has C# bindings in the form of SFML.Net

Simply start a new NET Core Console application and add the SFML.Net NuGet package to the project.

Then replace the program's body with the following code:

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();
    }
}

This gives you a window with a green circle. When you close the graphics window the application will shut down.

Hopefully this will help get you started!

Upvotes: 2

8Observer8
8Observer8

Reputation: 1152

You can use https://www.nuget.org/packages/OpenTK.NetStandard/

Instruction: how to create your first window for OpenGL graphics

  • dotnet new console
  • dotnet add package OpenTK.NetStandard
  • dotnet run
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();
        }
    }
}

Upvotes: 2

Staeff
Staeff

Reputation: 5084

You can 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 is a better option for you.

If you are trying to make a "desktop application" with GUI elements you can also look into Electron combined with TypeScript (which is somewhat similar to C#), this is how they made Visual Studio Code for example

EDIT: I just found another very interesting article (by the same guy I've mentioned in the comments) called Building a 3D Game Engine with .NET Core, I'm pretty sure you can get some inspiration out of that how to use OpenTK, Veldrid and ImGui.NET for drawing on screen.

Upvotes: 11

Related Questions