Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to load DLL 'libSkiaSharp' - Python wrapper based Web API #3040

Open
1 task done
MohanaselvamJ opened this issue Oct 16, 2024 · 6 comments
Open
1 task done

Comments

@MohanaselvamJ
Copy link

MohanaselvamJ commented Oct 16, 2024

Description

We are creating python wrapper based Web API to call C# method via python. While using SkiaSharp related code, we facing errors even loaded the SkiaSharp dlls in python.

Target: .NET Standard 2.0
Python version : 3.9

Any workaround also helpful.

Code

Python:

import clr #import clr from pythonnet
import os

#Get the DLL and input file path in string
dll_path = r'D:\\Python\Python\.NET Standard Wrapper Library\SkiaSharpApp\SkiaSharpApp\bin\Debug\netstandard2.0\SkiaSharpApp.dll'
harbuzzSharp_dll= r'C:\Users\Admin\.nuget\packages\harfbuzzsharp\7.3.0.2\lib\netstandard2.0\HarfBuzzSharp.dll'
skiasharpHarfbuzz_dll= r'C:\Users\ Admin \.nuget\packages\skiasharp.harfbuzz\2.88.8\lib\netstandard2.0\SkiaSharp.HarfBuzz.dll'
skiasharp_dll= r'C:\Users\ Admin \.nuget\packages\skiasharp\2.88.8\lib\netstandard2.0\SkiaSharp.dll'

#Verify the path
if dll_path not in os.sys.path:     
    os.sys.path.append(dll_path)
    os.sys.path.append(harbuzzSharp_dll)
    os.sys.path.append(skiasharpHarfbuzz_dll)
    os.sys.path.append(skiasharp_dll)
    print("Checking path")

#load our dll file(mine is in my C:// folder)
try: 
    clr.AddReference(dll_path)
    clr.AddReference(harbuzzSharp_dll)
    clr.AddReference(skiasharpHarfbuzz_dll)
    clr.AddReference(skiasharp_dll)
    print ("Load success")
except Exception as e:
    print("Fail to load")

from SKiaSharpApp import Test

test = Test() #create  object

print("Open and Save status: "+str(test.SkiaSharpSample()))

C#:

using SkiaSharp;
using System.IO;

namespace SKiaSharpApp
{
    public class Test
    {

        public string SkiaSharpSample()
        {
            //Define the image dimensions
            int width = 800;
            int height = 600;
            string base64String;

            //Create a new bitmap
            using (var bitmap = new SKBitmap(width, height))
            {
                // Create a canvas to draw on the bitmap
                using (var canvas = new SKCanvas(bitmap))
                {
                    // Clear the canvas with a white color
                    canvas.Clear(SKColors.White);

                    // Define the paint for drawing shapes
                    var paint = new SKPaint
                    {
                        Color = SKColors.Blue,
                        IsAntialias = true,
                        Style = SKPaintStyle.Stroke,
                        StrokeWidth = 5
                    };

                    // Draw a rectangle
                    var rect = new SKRect(100, 100, 300, 300);
                    canvas.DrawRect(rect, paint);

                    // Draw a circle
                    paint.Color = SKColors.Red;
                    canvas.DrawCircle(400, 400, 100, paint);

                    // Draw a line
                    paint.Color = SKColors.Green;
                    canvas.DrawLine(500, 100, 700, 300, paint);

                    // Save the bitmap to a file
                    using (var image = SKImage.FromBitmap(bitmap))
                    using (var data = image.Encode(SKEncodedImageFormat.Png, 100))

                    using (var memoryStream = new MemoryStream())
                    {
                        data.SaveTo(memoryStream);


                    }
                }
            }
            return "Success";
        }
    }
}

Replication steps

  1. Create .NET Standard 2.0 library project and compile above C# code.
  2. Create python file and use the dll path from bin folder of C# app in python.
  3. Similarly edit harbuzzSharp_dll and skiasharp_dll path in python as per your .nuget folder path.
  4. Run python
  5. You will receive an error from SkiaSharp

Expected Behavior

Work properly

Actual Behavior

Exception

Version of SkiaSharp

SkiaSharp.HarfBuzz,"2.88.8"

Last Known Good Version of SkiaSharp

6.88.6 (Deprecated)

IDE / Editor

Visual Studio Code (Windows)

Platform / Operating System

Windows

Relevant Log Output

PS D:\ Python\Python\API files in Python> python TestSkia.py   
Checking path
Load success
System.DllNotFoundException: Unable to load DLL 'libSkiaSharp': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
   at SkiaSharp.SkiaApi.sk_colortype_get_default_8888()
   at SkiaSharp.SKImageInfo..cctor()

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D \Python\Python\API files in Python\TestSkia.py", line 32, in <module>
    print("Open and Save status: "+str(test.SkiaSharpSample()))
System.TypeInitializationException: The type initializer for 'SkiaSharp.SKImageInfo' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'libSkiaSharp': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
   at SkiaSharp.SkiaApi.sk_colortype_get_default_8888()
   at SkiaSharp.SKImageInfo..cctor()
   --- End of inner exception stack trace ---
   at SkiaSharp.SKBitmap..ctor(Int32 width, Int32 height, Boolean isOpaque)
   at SKiaSharpApp.Test.SkiaSharpSample()

Unhandled Exception: System.TypeInitializationException: The type initializer for 'SkiaSharp.SKObject' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'libSkiaSharp': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
   at SkiaSharp.SkiaApi.sk_version_get_milestone()
   at SkiaSharp.SkiaSharpVersion.get_Native()
   at SkiaSharp.SkiaSharpVersion.CheckNativeLibraryCompatible(Boolean throwIfIncompatible)
   at SkiaSharp.SKObject..cctor()
   --- End of inner exception stack trace ---
   at SkiaSharp.SKObject.get_Handle()
   at SkiaSharp.SKNativeObject.Dispose(Boolean disposing)
   at SkiaSharp.SKBitmap.Dispose(Boolean disposing)
   at SkiaSharp.SKNativeObject.Finalize()

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Proektsoftbg
Copy link

Hi! Any updates on this? I am getting the same.

@mattleibow
Copy link
Contributor

You are loading the managed (.NET) dlls, but have not made sure the native parts are able to be found. You can't reference them directly via the CLR, but if they are in the same folder as the managed dlls, they should be found: https://www.nuget.org/packages/SkiaSharp.NativeAssets.Win32

I think all of this will work if you publish your .NET app and then use that. Publishing will copy all references and dependencies into the output. You can then load the dlls, and they should find the native libraries.

@DharanyaSakthivel-SF4210

@mattleibow , We are still getting the exception after including the native libSkiaSharp.dll.

`PS D:\Word-to-PDF-Python\Word-to-PDF-Python\API files in Python> python SkiaSharpSample.py
Checking path
Fail to load
System.DllNotFoundException: Unable to load DLL 'libSkiaSharp': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at SkiaSharp.SkiaApi.sk_colortype_get_default_8888()
at SkiaSharp.SKImageInfo..cctor()

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "D:\Word-to-PDF-Python\Word-to-PDF-Python\API files in Python\SkiaSharpSample.py", line 36, in
print("Open and Save status: "+str(docIO.SkiaSharpSample()))
System.TypeInitializationException: The type initializer for 'SkiaSharp.SKImageInfo' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'libSkiaSharp': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at SkiaSharp.SkiaApi.sk_colortype_get_default_8888()
at SkiaSharp.SKImageInfo..cctor()
--- End of inner exception stack trace ---
at DocIOLibrary.DocIO.SkiaSharpSample()

Unhandled Exception: System.TypeInitializationException: The type initializer for 'SkiaSharp.SKObject' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'libSkiaSharp': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at SkiaSharp.SkiaApi.sk_version_get_milestone()
at SkiaSharp.SkiaSharpVersion.get_Native()
at SkiaSharp.SkiaSharpVersion.CheckNativeLibraryCompatible(Boolean throwIfIncompatible)
at SkiaSharp.SKObject..cctor()
--- End of inner exception stack trace ---
at SkiaSharp.SKObject.get_Handle()
at SkiaSharp.SKNativeObject.Dispose(Boolean disposing)
at SkiaSharp.SKBitmap.Dispose(Boolean disposing)
at SkiaSharp.SKNativeObject.Finalize()`

@mattleibow
Copy link
Contributor

Are you able to zip up the samples so I can look?

@MohanaselvamJ
Copy link
Author

I have tried below approach, but not works.

  1. Used libSkiaSharp.dll from published folder of .NET app with SkiaSharp.NativeAssets.Win32 NuGet.
  2. Used libSkiaSharp.dll from .nuget folder of SkiaSharp.NativeAssets.Win32 NuGet.

You can find the things we tried and error faced details in attached Word document. Also, the PythonWrapper.zip contains the .NET application and python code.

Could you please try and suggest solution with runnable sample.

@MohanaselvamJ
Copy link
Author

@mattleibow , Any update on this? Does we missed anything in given sample?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Ready For Work
Development

No branches or pull requests

4 participants