How to Install Latest libgdiplus library on Docker(dotnetcore)

I recently ran into a issue with Aspose.PDF where it fails when converting PDF to Image. It was giving a System.OutOfMemoryException and in error logs i found that the error comes from the System.Drawing.Common which uses the libgdiplus as the main provider of the cross-platform implementation of System.Drawing.Common on the native side. When we deploy the same code to Azure App service Environment(Windows) this error never occurred and error appears only for Linux.

some of the error logs as below:

** (process:24425): WARNING **: Path conversion requested 8582688 bytes (1224 x 1753). Maximum size is 8388608 bytes.
System.OutOfMemoryException: at System.Drawing.SafeNativeMethods+Gdip.CheckStatus (System.Drawing.Common, Version=5.0.0.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51) at System.Drawing.Graphics.get_ClipBounds (System.Drawing.Common, Version=5.0.0.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51) at #=zPJj50tEShreZcXmmZPJN5_dN8VOB10cgOQW$e0o=.#=zB3VnxjIIPIEv (Aspose.PDF, Version=22.3.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56)

I already installed libgdiplus using apt-get in aspnetcore 5.0 image but after closer look at the version installed I found that the Debian is still on version 4.2 of libgdiplus which is very old.

below is the output from dpkg -l libgdiplus

After couple of unsuccessful attempt to install the latest using mono download link and following there installation guide which retrieve deb from the "deb https://download.mono-project.com/repo/debian stable-buster main"  (which is still on version 4.2) I found a solution which mentioned here in mono docs.

Now my libgdiplus installation step in docker file looks like below which is successfully running on aspnetcore 5 container.

ENV MONO_VERSION 6.12.0.122

RUN echo "deb https://download.mono-project.com/repo/debian stable-buster/snapshots/$MONO_VERSION main" > /etc/apt/sources.list.d/mono-official-stable.list \
  && apt-get update \
  && apt-get install -y mono-runtime \
  && rm -rf /var/lib/apt/lists/* /tmp/*

With that one I would be able to get the newer version of libgdiplus as below and It has resolved the  System.OutOfMemoryException comes from System.Drawing.Common.

You can find the complete docker file in this Github repo.