Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 769
Shrike Avatar asked Mar 23 '18 15:03

Shrike


1 Answers

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

var g = Graphics.FromImage(new Bitmap(1, 1));
like image 121
dRn Avatar answered Sep 19 '22 09:09

dRn