hey-red / ImageSharp.Heif

HEIF/AVIF decoder for ImageSharp
MIT License
7 stars 3 forks source link

Unable to load DLL 'libheif' or one of its dependencies: #2

Closed michaelakin closed 1 year ago

michaelakin commented 1 year ago

Hi,

I was referred from ImageSharp to this package. I am trying to load a heic file with your package and I am getting the following error:

Unable to load DLL 'libheif' or one of its dependencies: The specified module could not be found. (0x8007007E) Stack trace:

  at LibHeifSharp.Interop.LibHeifNative.heif_get_version_number()
   at LibHeifSharp.LibHeifInfo.GetLibHeifVersionNumber()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at LibHeifSharp.LibHeifVersion.ThrowIfNotSupported()
   at LibHeifSharp.HeifContext..ctor(Stream stream, Boolean leaveOpen)
   at HeyRed.ImageSharp.Heif.HeifDecoderCore.Decode[TPixel](Stream stream, CancellationToken cancellationToken)
   at HeyRed.ImageSharp.Heif.HeifDecoder.Decode[TPixel](HeifDecoderOptions options, Stream stream, CancellationToken cancellationToken)
   at HeyRed.ImageSharp.Heif.HeifDecoder.Decode(HeifDecoderOptions options, Stream stream, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.SpecializedImageDecoder`1.Decode(DecoderOptions options, Stream stream, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.ImageDecoder.<>c__DisplayClass1_0.<Decode>b__0(Stream s)
   at SixLabors.ImageSharp.Formats.ImageDecoder.WithSeekableStream[T](DecoderOptions options, Stream stream, Func`2 action)
   at SixLabors.ImageSharp.Formats.ImageDecoder.Decode(DecoderOptions options, Stream stream)
   at SixLabors.ImageSharp.Image.Load(DecoderOptions options, String path)
   at Program.<Main>$(String[] args) in D:\Code\ffng\ConsoleApp1\Program.cs:line 27

Here is my sample program:

using HeyRed.ImageSharp.Heif.Formats.Avif;
using HeyRed.ImageSharp.Heif.Formats.Heif;
using SixLabors.ImageSharp.Formats;

var files = Directory.GetFiles(@"C:\Users\myuser\Downloads\");
var types = new List<string>() { ".jpg", ".jpeg", ".png", ".heic" };

var options = new DecoderOptions()
{
    Configuration = new Configuration(
        new AvifConfigurationModule(),
        new HeifConfigurationModule()
        )
};

foreach (var file in files.Where(f => f.Contains("7B44F_bol.heic")))
{
    var ext = Path.GetExtension(file);
    if (types.Contains(ext))
    {
        Console.WriteLine(file);
        try
        {
            var image = Image.Load(options , file);
            Console.WriteLine(image.Size.ToString());
        }
        catch (Exception ex)
        {
            Console.WriteLine($"{file} error {ex.Message}");
        }
    }
}

Console.ReadLine();

Here are the nuget packages installed:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="HeyRed.ImageSharp.Heif" Version="2.1.1" />
    <PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" />
  </ItemGroup>

</Project>
michaelakin commented 1 year ago

Here is the file in question. I renamed it back to .jpeg file extension so that it would load here.

7B44F_bol

michaelakin commented 1 year ago

I may have fixed it, I also installed this package: Install-Package LibHeif.Native

And now it seems to be working.

Thanks.

AndrewFreemantle commented 3 months ago

For those on an Apple Silicon Mac:

  1. Install libheic with brew: brew install libheic
  2. Find out where it's installed with: brew info libheic
  3. Link it to your .net installation folder - on my machine (at the time of writing) my command was:
ln -s /opt/homebrew/Cellar/libheif/1.17.6_1/lib/libheif.dylib \
  /Users/andrew/.dotnet/shared/Microsoft.NETCore.App/8.0.6/libheif