Search code examples
c#.net-coreazure-functions.net-6.0imagesharp

How to use SixLabors.ImageSharp in a Azurefunction project


I need to use ImageSharp to handle images on my Azure function. But even after adding the reference, I still can't reference it from the c# code. In other solutions related to the library, they mention to add configuration in the startup.cs of an API, which is not my case.

This is my .csproj

    <Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    ...
    <PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" />
    ...
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

And in the c# code, this gets "The type or namespace name 'SixLabors.ImageSharp' could not be found"

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Formats;

Solution

  • Thanks @Sancho Panza for the comment.

    As mentioned by Sancho Panza, the latest version 3.0.1of SixLabors.ImageSharp is only compatible with .NET 6.0 and above.

    • The versions compatible with .NET Core 3.1 are shown below.

    enter image description here

    enter image description here

    If you want to continue with the former version of .NET 3.1, then downgrade the SixLabors.ImageSharp version to 2.1.4.

    Now Iam able to add the namespaces for the below without any issues with the older versions.

    My .csproj file:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <AzureFunctionsVersion>v3</AzureFunctionsVersion>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.1.1" />
        <PackageReference Include="SixLabors.ImageSharp" Version="2.1.4" />
      </ItemGroup>
      <ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
      </ItemGroup>
    </Project>
    

    enter image description here