Unable to use any Image library in the AWS Lambda function project in .NET Core

0

I am developing an Aws S3 Lambda function in .NET core. What I am trying to do now is to resize the image and save it as a new file when an image file is uploaded to the s3 Bucket. For resizing the images in the .NET core, I tried using all the options mentioned in this link. All the options are working locally on my Windows Laptop. But when I install any of packages or dll files into my project and use it in the AWS Lambda function, I am getting error. For all the libraries. It seems like that, AWS lambda is using Linux server and the libries might need some extra configurations.

For example, when I use CoreCompat.System.Drawing in the code and create a bitmap like this.

var resized = new Bitmap(100, 100);

I got this error when I test it on the AWS console.

{
  "errorType": "AggregateException",
  "errorMessage": "One or more errors occurred. (The type initializer for 'System.Drawing.GDIPlus' threw an exception.)",
  "stackTrace": [
    "at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)",
    "at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
  ],
  "cause": {
    "errorType": "TypeInitializationException",
    "errorMessage": "The type initializer for 'System.Drawing.GDIPlus' threw an exception.",
    "stackTrace": [
      "at System.Drawing.GDIPlus.GdipCreateBitmapFromScan0(Int32 width, Int32 height, Int32 stride, PixelFormat format, IntPtr scan0, IntPtr& bmp)",
      "at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)",
      "at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)",
      "at AwsS3Lambda.Function.<FunctionHandler>d__14.MoveNext() in C:\\Users\\Acer\\Desktop\\AwsS3Lambda\\AwsS3Lambda\\AwsS3Lambda\\Function.cs:line 117"
    ],
    "cause": {
      "errorType": "DllNotFoundException",
      "errorMessage": "Unable to load DLL 'gdiplus': The specified module or one of its dependencies could not be found.\n (Exception from HRESULT: 0x8007007E)",
      "stackTrace": [
        "at System.Drawing.GDIPlus.GdiplusStartup(UInt64& token, GdiplusStartupInput& input, GdiplusStartupOutput& output)",
        "at System.Drawing.GDIPlus..cctor()"
      ]
    }
  },
  "causes": [
    {
      "errorType": "TypeInitializationException",
      "errorMessage": "The type initializer for 'System.Drawing.GDIPlus' threw an exception.",
      "stackTrace": [
        "at System.Drawing.GDIPlus.GdipCreateBitmapFromScan0(Int32 width, Int32 height, Int32 stride, PixelFormat format, IntPtr scan0, IntPtr& bmp)",
        "at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)",
        "at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)",
        "at AwsS3Lambda.Function.<FunctionHandler>d__14.MoveNext() in C:\\Users\\Acer\\Desktop\\AwsS3Lambda\\AwsS3Lambda\\AwsS3Lambda\\Function.cs:line 117"
      ],
      "cause": {
        "errorType": "DllNotFoundException",
        "errorMessage": "Unable to load DLL 'gdiplus': The specified module or one of its dependencies could not be found.\n (Exception from HRESULT: 0x8007007E)",
        "stackTrace": [
          "at System.Drawing.GDIPlus.GdiplusStartup(UInt64& token, GdiplusStartupInput& input, GdiplusStartupOutput& output)",
          "at System.Drawing.GDIPlus..cctor()"
        ]
      }
    }
  ]
}

I know CoreCompat.System.Drawing has cross-platform issue. So, I tried using the SkiaSharp which is compactible for cross-platform.

Once I call this line of code.

using(var inputStream = new SKManagedStream(responseStream))
                  {

                  }

I got this error.

{
  "errorType": "AggregateException",
  "errorMessage": "One or more errors occurred. (The type initializer for 'SkiaSharp.SKAbstractManagedStream' threw an exception.)",
  "stackTrace": [
    "at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)",
    "at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
  ],
  "cause": {
    "errorType": "TypeInitializationException",
    "errorMessage": "The type initializer for 'SkiaSharp.SKAbstractManagedStream' threw an exception.",
    "stackTrace": [
      "at SkiaSharp.SKAbstractManagedStream..ctor(Boolean owns)",
      "at SkiaSharp.SKManagedStream..ctor(Stream managedStream, Boolean disposeManagedStream, Boolean owns)",
      "at SkiaSharp.SKManagedStream..ctor(Stream managedStream, Boolean disposeManagedStream)",
      "at SkiaSharp.SKManagedStream..ctor(Stream managedStream)",
      "at AwsS3Lambda.Function.<FunctionHandler>d__14.MoveNext() in C:\\Users\\Acer\\Desktop\\AwsS3Lambda\\AwsS3Lambda\\AwsS3Lambda\\Function.cs:line 113"
    ],
    "cause": {
      "errorType": "DllNotFoundException",
      "errorMessage": "Unable to load DLL 'libSkiaSharp': The specified module or one of its dependencies could not be found.\n (Exception from HRESULT: 0x8007007E)",
      "stackTrace": [
        "at SkiaSharp.SkiaApi.sk_managedstream_set_delegates(IntPtr pRead, IntPtr pPeek, IntPtr pIsAtEnd, IntPtr pHasPosition, IntPtr pHasLength, IntPtr pRewind, IntPtr pGetPosition, IntPtr pSeek, IntPtr pMove, IntPtr pGetLength, IntPtr pCreateNew, IntPtr pDestroy)",
        "at SkiaSharp.SKAbstractManagedStream..cctor()"
      ]
    }
  },
  "causes": [
    {
      "errorType": "TypeInitializationException",
      "errorMessage": "The type initializer for 'SkiaSharp.SKAbstractManagedStream' threw an exception.",
      "stackTrace": [
        "at SkiaSharp.SKAbstractManagedStream..ctor(Boolean owns)",
        "at SkiaSharp.SKManagedStream..ctor(Stream managedStream, Boolean disposeManagedStream, Boolean owns)",
        "at SkiaSharp.SKManagedStream..ctor(Stream managedStream, Boolean disposeManagedStream)",
        "at SkiaSharp.SKManagedStream..ctor(Stream managedStream)",
        "at AwsS3Lambda.Function.<FunctionHandler>d__14.MoveNext() in C:\\Users\\Acer\\Desktop\\AwsS3Lambda\\AwsS3Lambda\\AwsS3Lambda\\Function.cs:line 113"
      ],
      "cause": {
        "errorType": "DllNotFoundException",
        "errorMessage": "Unable to load DLL 'libSkiaSharp': The specified module or one of its dependencies could not be found.\n (Exception from HRESULT: 0x8007007E)",
        "stackTrace": [
          "at SkiaSharp.SkiaApi.sk_managedstream_set_delegates(IntPtr pRead, IntPtr pPeek, IntPtr pIsAtEnd, IntPtr pHasPosition, IntPtr pHasLength, IntPtr pRewind, IntPtr pGetPosition, IntPtr pSeek, IntPtr pMove, IntPtr pGetLength, IntPtr pCreateNew, IntPtr pDestroy)",
          "at SkiaSharp.SKAbstractManagedStream..cctor()"
        ]
      }
    }
  ]
}

I tried all the libraries mentioned in the link. All the libraries are working with .NET core locally on my laptop when I create new project (Console) app and test it. But all the libraries are not working when I use it in AWS Lambda function project because all the libraries throwing respective error when I test it on AWS console. How can I resize the image in the AWS Lambda function in .NET Core?

amazon-web-services
amazon-s3
.net-core
aws-lambda
skiasharp
asked on Stack Overflow May 29, 2018 by Wai Yan Hein • edited Aug 1, 2018 by halfer

1 Answer

0

.NET Core ships with System.Drawing.Common, which is the cross-platform implementation of System.Drawing.

It uses libgdiplus, and you'll need to make sure that you've installed libgdiplus on the server on which your code will run. The libgdiplus project is located at github.com/mono/libgdiplus and ships with most Linux distros.

The same goes for SkiaSharp and its native counterpart.

I don't know much about Amazon Lambda functions, but if you don't have control over which packages are installed on the server on which your code runs, you can also try a fully managed drawing library such as ImageSharp.

answered on Stack Overflow May 31, 2018 by Frederik Carlier

User contributions licensed under CC BY-SA 3.0