Add ImageStamp in PDF on Linux got exception: : System.Drawing.Common is not supported on non-Windows platforms

We’re trying to use ImageStamp to add a png image on PDF using .Net 6. It works on Windows, but got exception on Linux: System.Drawing.Common is not supported on non-Windows platforms.

We’re using Aspse.PDF 23.10.0, the program is written in .Net 6. Below is the code

using Aspose.Pdf;
using Aspose.Drawing;

namespace Aspose_Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            byte[] fileBytes = File.ReadAllBytes("1.pdf");
            Image<Rgba32> image = SixLabors.ImageSharp.Image.Load<Rgba32>("Stamp.png");

            using (var stream = new MemoryStream())
            {
                image.SaveAsPng(stream);
                ImageStamp imageStamp = new ImageStamp(stream);
                imageStamp.Background = true;
                imageStamp.XIndent = 100;
                imageStamp.YIndent = 100;
                imageStamp.Height = 300;
                imageStamp.Width = 300;
                imageStamp.Rotate = Rotation.on270;
                imageStamp.Opacity = 0.5;

                Document doc = new Document(new MemoryStream(fileBytes));
                doc.Pages[1].AddStamp(imageStamp);

                MemoryStream ms = new MemoryStream();
                doc.Save("Stamped.pdf");
            }
        }
    }
}

Below is the exception on Linux

>Unhandled exception. System.TypeInitializationException: The type initializer for 'Gdip' threw an exception.
 ---> System.PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.
   at System.Drawing.LibraryResolver.EnsureRegistered()
   at System.Drawing.SafeNativeMethods.Gdip.PlatformInitialize()
   at System.Drawing.SafeNativeMethods.Gdip..cctor()
   --- End of inner exception stack trace ---
   at System.Drawing.SafeNativeMethods.Gdip.GdipLoadImageFromDelegate_linux(StreamGetHeaderDelegate getHeader, StreamGetBytesDelegate getBytes, StreamPutBytesDelegate putBytes, StreamSeekDelegate doSeek, StreamCloseDelegate close, StreamSizeDelegate size, IntPtr& image)
   at System.Drawing.Image.InitializeFromStream(Stream stream)
   at System.Drawing.Image.LoadFromStream(Stream stream, Boolean keepAlive)
   at Aspose.Pdf.ImageStamp..ctor(Stream image)
   at Aspose_Test.Program.Main(String[] args) in F:\Project\CSharp\Aspose Test\Aspose Test\Program.cs:line 18
Aborted (core dumped)

Any solution to fix this problem?

@mailzhengli

Instead of Aspose.PDF for .NET, please try to use Aspose.Pdf.Drawing as it does not have dependency on System.Drawing. You can uninstall existing Aspose.PDF package from your project and install Aspose.Pdf.Drawing from NuGet package manager instead. In case issue still persists, please let us know.

Thanks Asad, using Aspose.Pdf.Drawing fix the problem.

Is Aspose.Pdf.Drawing dll included in the Aspose.PDF package if I buy it?

@mailzhengli

Aspose.Pdf.Drawing is separately available and it is a beta version of base Aspose.PDF for .NET API. It does not have dependency of System.Drawing and we launched it for .NET 6 as well as non-Windows environments. Once we get all satisfactory results, we will simply make it a permanent part of existing Aspose.PDF for .NET. In the meantime, you will not require to purchase any additional license to use it. You will be able to use it with the same license that you get for Aspose.PDF.