AsposeSvg:TypeInitializationException:The type initializer for 'Gdip' threw an exception

I’m building an application that runs on both windows and macos.

This application converts svg to bmp.

On Windows (10) it works but on MacOs I get:

TypeInitializationException:The type initializer for ‘Gdip’ threw an exception.

We’re using the latest AsposeSvg and the app is built in .net 6.

See code example:

AsposeSvg.zip (106.3 KB)

@nielsbosma

Would you kindly make sure that libgdiplus package is installed and up-to-date in the system running macOS at your side? In case issue still occurs, please let us know.

Yes it’s installed:

image.png (6.4 KB)

Unhandled exception. System.TypeInitializationException: The type initializer for ‘Gdip’ threw an exception.
—> System.DllNotFoundException: Unable to load DLL ‘libgdiplus’: The specified module could not be found.
at System.Runtime.InteropServices.FunctionWrapper`1.get_Delegate()
at System.Drawing.SafeNativeMethods.Gdip.GdiplusStartup(IntPtr& token, StartupInput& input, StartupOutput& output)
at System.Drawing.SafeNativeMethods.Gdip…cctor()
— End of inner exception stack trace —
at System.Drawing.SafeNativeMethods.Gdip.GdipCreatePath(FillMode brushMode, IntPtr& path)
at System.Drawing.Drawing2D.GraphicsPath…ctor()
at …ctor()
at Aspose.Svg.Rendering.Image.ImageDevice…ctor(ImageRenderingOptions options, String file)
at Program.$(String[] args) in /Users/filestar/repos/Filestar/experimental/AsposeSvg/Program.cs:line 8

@nielsbosma

We have logged an investigation ticket as SVGNET-78 in our issue tracking system for further analysis on this case. We will look into its detail and let you know as soon as it is resolved. Please be patient and spare us some time.

We are sorry for the inconvenience.

Any progress?

@nielsbosma

Regretfully, the issue could not be resolved yet. However, we will surely inform you once we make some progress towards its fix.

We apologize for your inconvenience.

Can you please share status on when you have .net 6 support?

@nielsbosma

We can offer you the next workaround for the problem with the exception:

“you can enable support for non-Windows platforms in .NET 6 by setting the System.Drawing.EnableUnixSupport runtime configuration switch to true in the runtimeconfig.json file:”

{
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}

More details can be found here:

and here:

There is a lot of work and uncertainties with the migration of our product to multiplatform graphics engines like Skia. Of course, we are going to do this, but we are not sure about the time, we suggest this support will not be added until next year.

I’ve tried that but it won’t work in my case.

@nielsbosma

Thanks for sharing the feedback. We have updated the ticket information accordingly and will surely let you know once it is resolved. We apologize for the inconvenience faced.

@nielsbosma

We’ve attempted to replicate the issue using Aspose.SVG.Drawing.SkiaSharp 23.8.1 in a Linux Docker container with preinstalled libgdiplus libraries, and we can confirm that the problem you described does not appear to be reproducible in this environment.

FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
USER root
RUN apt-get update \
    && apt-get install -y --allow-unauthenticated \
        libfontconfig1 \
        libfreetype6 \
        libexpat1 \
        libpng16-16 \
        libc6-dev \
        libgdiplus \
        libx11-dev \
    && rm -rf /var/lib/apt/lists/*
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
RUN sed -i'.bak' 's/$/ contrib/' /etc/apt/sources.list
RUN apt-get update; apt-get install -y ttf-mscorefonts-installer fontconfig
RUN fc-cache -f -v

If you have access to a MAC computer, could you please try to test the code there with Aspose.SVG.Drawing.SkiaSharp 23.8.1 and the preinstalled libgdiplus libraries as outlined in the Dockerfile above? Additionally, it’s possible that you may need to add the NuGet component SkiaSharp.NativeAssets.macOS to the test project.

The thing is that I can’t install libgdiplus need this to work without it.

@nielsbosma

Your response and feedback have been recorded and ticket information has been updated accordingly. We will let you know once we have more updates to share. Please spare us some time.

We are sorry for the inconvenience.

Any progress?

I’ve tried Aspose.SVG.Drawing.SkiaSharp but I’m getting the same error on osx.

@nielsbosma

The ticket is still under the phase of investigation. Its information has been updated as per your provided feedbacks and as soon as we have some updates to share, we will let you know. Please spare us some time.

@nielsbosma

Thank you for your updates and for trying out the suggested solutions. I understand that installing libgdiplus is not an option in your case and the System.Drawing.EnableUnixSupport runtime configuration switch did not resolve the issue. Let’s explore some alternative approaches:

  1. Aspose.SVG.Drawing.SkiaSharp Integration:

It’s crucial to ensure that Aspose.SVG.Drawing.SkiaSharp is correctly initialized in your application. This can be done by adding the following code at the start of your application:

using Aspose.Svg.Drawing.SkiaSharp; Aspose.Svg.Configuration.SetExtension(new SkiaModule());

This code initializes the SkiaSharp-based drawing engine, which is designed to work without libgdiplus.

  1. Testing with the Latest Version:

Please consider testing with the latest version of Aspose.SVG.Drawing.SkiaSharp (24.1.0 as of now). You can find it on NuGet: Aspose.SVG.Drawing.SkiaSharp 24.1.0

  1. Reviewing Your Code:

If possible, could you share a snippet of your code where you’re using Aspose.SVG? This will help us understand if there are any parts of the code that might still be relying on GDI+ inadvertently.

  1. Docker Environment for Testing:

I have successfully run tests for Aspose.SVG on a Debian Docker container without libgdiplus. Here’s the Dockerfile used for testing:

# Use Debian Buster as the base image
FROM debian:buster

# Set the working directory
WORKDIR /app

# Install system dependencies
RUN apt-get update && \
    apt-get install -y wget apt-transport-https software-properties-common

# Add Microsoft package repository
RUN wget -q "https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb" -O packages-microsoft-prod.deb && \
    dpkg -i packages-microsoft-prod.deb && \
    apt-get update

# Install PowerShell
RUN apt-get install -y powershell

# Install .NET SDKs and Runtimes
RUN apt-get install -y dotnet-sdk-5.0 dotnet-sdk-6.0 dotnet-sdk-7.0

RUN apt-get install -y libfontconfig1 libfreetype6 fontconfig fontconfig-config fonts-dejavu-core

# Copy the batch file and any necessary scripts or files into the container
COPY . /app

# Set the entry point to PowerShell
ENTRYPOINT ["pwsh"]
  1. Further Assistance:

If the issue persists, we are here to assist further. Any additional details about the error messages or the context in which the issue occurs would be greatly appreciated.