We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

System.PlatformNotSupportedException: System.Drawing.Common is not supported on this platform while converting image to grayscale

I’m using Aspose.Imaging within a linux container to convert PNG to grayscale which gives me the following exception/Stacktrace:

anzen_server | IndexWorker: Unexpected exception while indexing Document with Id ‘199411ca-46b1-4371-a504-6e7daee299bc’: System.PlatformNotSupportedException: System.Drawing.Common is not supported on this platform.
anzen_server | at System.Drawing.SystemFonts.get_DefaultFont()
anzen_server | at Aspose.Imaging.ImageOptionsBase…ctor()
anzen_server | at Aspose.Imaging.ImageOptions.PngOptions…ctor()
anzen_server | at Aspose.Imaging.FileFormats.Png.PngImage.SaveData(Stream stream)
anzen_server | at Aspose.Imaging.DataStreamSupporter.Save(Stream stream)
anzen_server | at appGenerics.Anzen.Infrastructure.Converters.AsposeHelper.ImageToGrayscale(Stream input) in /src/anzen_server/Infrastructure/Converters/AsposeHelper.cs:line 31

Seems that Linux is not fully supported here. What can I do?

Here comes the lines I use to convert the image:

        using (Image image = Image.Load(input))
        {
            var tempFileName = Path.GetTempFileName();
            try
            {
                using (var grayScaledStream = new FileStream(tempFileName, FileMode.Open, FileAccess.ReadWrite))
                {
                    // Cast the image to RasterCachedImage and Check if image is cached                

                    RasterCachedImage rasterCachedImage = (RasterCachedImage)image;
                    if (!rasterCachedImage.IsCached)
                    {
                        // Cache image if not already cached
                        rasterCachedImage.CacheData();
                    }

                    // Binarize image with predefined fixed threshold and Save the resultant image                
                    rasterCachedImage.Grayscale();
                    rasterCachedImage.Save(grayScaledStream);
                    input.Position = 0;
                    grayScaledStream.CopyTo(input);
                    input.SetLength(grayScaledStream.Length);
                }
            }
            finally
            {
                File.Delete(tempFileName);
            }

Exception occurs in rasterCachedImage.Save(grayScaledStream);

@HauptlorenzAG, If you are using .Net 6.0 please see the link below: https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only

I’m using .NET 7.

The Aspose.PDF team wrote a new version which has no dependency to System.Drawing anymore und runs now on Linux.

Also on your side unter https://products.aspose.com/imaging/net/ is written:

Aspose.Imaging for .NET is a flexible, stable and powerful API, capable of processing the most commonly used formats along with some special formats such as DjVu, DICOM, WebP & DNG. Moreover, it extends the native support for image formats & processing functions for .NET and .NET Core. Aspose.Imaging is cross-platform library, it is Windows and Linux compatible.

  1. Are there any plans for Aspose.Imaging as well?
  2. How can I turn an Image to grayscale on Linux using Aspose?

@HauptlorenzAG, Currently only .NET6.0 is supported, .Net7.0 support is planned.
Alternatively, you can try the beta version: https://www.nuget.org/packages/Aspose.Imaging/22.10.0-beta
This version does not need system.drawing, but there is no .Net7.0 support.

I’ve downgraded to 22.10.0-beta and receive now the following exception:

anzen_server | IndexWorker: Unexpected exception while indexing Document with Id ‘199411ca-46b1-4371-a504-6e7daee299bc’: System.ArgumentException: Font ‘FreeSans’ cannot be found
anzen_server | at System.Drawing.FontFamily.:slight_smile:(String :slight_smile:, FontCollection :heart:)
anzen_server | at System.Drawing.FontFamily…ctor(String name)
anzen_server | at System.Drawing.Font.:slight_smile:(String :slight_smile:, Single :heart:, FontStyle :clubs:, GraphicsUnit, Byte :spades:, Boolean ♫)
anzen_server | at System.Drawing.Font…ctor(String familyName, Single emSize)
anzen_server | at System.Drawing.SystemFonts.get_DefaultFont()
anzen_server | at Aspose.Imaging.ImageOptionsBase…ctor()
anzen_server | at Aspose.Imaging.ImageOptions.PngOptions…ctor()
anzen_server | at Aspose.Imaging.FileFormats.Png.PngImage.SaveData(Stream stream)
anzen_server | at Aspose.Imaging.DataStreamSupporter.Save(Stream stream)

Is there any font package I must install into my container?

Additional question: why do you need fonts for turning a rasterized PNG into a gray scaled PNG?

@HauptlorenzAG, You need to install the font package: https://askubuntu.com/questions/24983/how-do-i-reinstall-default-fonts
Fonts are needed to initialize the library.

I’ve added msttcorefonts to the container but still same message. What is “FreeSans”? I have this font not on my windows machine also.

@HauptlorenzAG, sorry, you need to add this package: apt-get install fonts-freefont-ttf

Works now, thanks!