How to draw with .NET Core?
Asked Answered
L

4

21

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.

Licastro answered 8/1, 2017 at 18:43 Comment(1)
maybe check out this question, found some answers there that might be interesting for you.Intertwist
B
11

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.

Brumfield answered 8/1, 2017 at 18:49 Comment(5)
OK, I understand that there is nothing that is truly cross-platform to draw on the screen using .NET Core, so we would have to rely on a platform-specific set of "Platform Invokes", right?Licastro
I'm not really a pro when it comes to graphic manipulation, but at the end of the articel I included in my answer the author talks about this guys github.com/mellinoe and from a quick look he has 4 very interesting projects on there that all do .NET core graphics manipulation in a more abstracted way (ImGui.NET, vk, veldrid, ge) and he also contributed to something called Avalonia which you can also check out, but I haven't really figured out if this runs on .NET core EDIT: also have a look into OpenTK nuget.org/packages/OpenTK.NETCoreBrumfield
Avalonia? I'm collaborating with it, too! I'll take a look!Licastro
@Licastro I've just made an edit because I've found another interesting article, so maybe also look into thatBrumfield
Regardless of what final rendering you choose try to write your code logically with a clear tier boundary, this will help you if you need to change and do not cost much time. Your custom method DrawSphere(...) for example will give logical instructions to draw a sphere. Only code below this method will be drawing specific for the library you choose, while code above will be fully logical.Prisilla
S
3

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

Stagecoach answered 9/7, 2021 at 13:4 Comment(0)
D
2

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();
        }
    }
}
Dampen answered 15/5, 2020 at 12:35 Comment(0)
L
2

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!

Ley answered 8/2, 2021 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.