.NET Core: using System.Drawing.Common on Linux in Docker - "Could not open display" error

1

I have code ported onto .NET Core that uses System.Drawing. Currently it seems System.Drawing.Common from corefx supports Linux. But I have difficulties with running my code on Linux.

Particularly I'm getting:

NotSupportedException "Could not open display (X-Server required. Check your DISPLAY environment variable)"

for this code:

Graphics gr = Graphics.FromHwnd(IntPtr.Zero);

Previously I was getting

DllNotFoundException "Unable to load DLL 'libX11': The specified module or one of its dependencies could not be found.\n (Exception from HRESULT: 0x8007007E)"

with stacktrace:

   at System.Drawing.LibX11Functions.XOpenDisplay(IntPtr display)
   at System.Drawing.Graphics.FromHwnd(IntPtr hwnd)
   at mycode

But this issue I fixed by installing libx11-dev package.

Here's my Dockerfile:

FROM microsoft/aspnetcore
ENV DOTNET_SKIP_FIRST_TIME_EXPERIENCE true

# install libgdiplus for System.Drawing
RUN apt-get update && \
    apt-get install -y --allow-unauthenticated libgdiplus libc6-dev

# install x11 for System.Drawing
RUN apt-get update && \
    apt-get install -y --allow-unauthenticated libx11-dev

So what can I do with "Could not open display (X-Server required. Check your DISPLAY environment variable)" error?

linux
.net-core
x11
system.drawing
asked on Stack Overflow Mar 23, 2018 by Shrike • edited Mar 26, 2018 by Shrike

1 Answer

0

Perhaps, you can use an image-based Graphics, rather than an window-based. Something like this:

var g = Graphics.FromImage(new Bitmap(1, 1));
answered on Stack Overflow Apr 23, 2018 by dRn

User contributions licensed under CC BY-SA 3.0