Using System.Drawing on Linux with .NET Core 3.0

This guide covers how to get a sample .NET Core 3.0 application building and running on Linux, specifically Ubuntu 16.04.

These steps should be transferrable to other Ubuntu versions and/or Linux distributions but it has not been tested. A list of supported distributions and versions can be found here.

Setup

First install any required external dependencies, which in this case means libgdiplus. This is required as the Linux implementation of System.Drawing.Common sits on top of it.

    sudo apt-get -f install libgdiplus

Next, retrieve the latest .NET Core 3.0 release for your system and place it under ~/dotnet-sdk. Once there, perform the following steps:

    cd ~/dotnet-sdk
    sudo mkdir -p /usr/share/dotnet
    sudo tar -zxf dotnet-sdk-3.0.100-preview5-011568-linux-x64.tar.gz -C /usr/share/dotnet
    sudo ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet

Confirm the above worked by checking the installed .NET version,

    dotnet --version

which should output something similar to:

    3.0.100-preview5-011568

Creating the Test Application

Now to create a simple test application that will use System.Drawing.Common to create a bitmap image and save it to disk.

    dotnet new console -o test-app
    cd test-app
    dotnet add package System.Drawing.Common

Then replace the contents of Program.cs with:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
using System;
using System.Drawing;

namespace test_app
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("-- System.Drawing Test App --");
            Console.WriteLine("Creating test bitmap ...");

            new Bitmap(128, 128).Save("test.bmp");

            Console.WriteLine("... done!");
        }
    }
}

Finally, build and run it:

    dotnet build
    cd bin/Debug/netcoreapp3.0/
    ./test-app

Assuming it was successful there should now be a test.bmp next to your test application.