2

What I am trying to do: In my application, I have the functionality to upload an image. I want to change the image being uploaded to PNG format, irrespective of the format user has selected, in my Azure function.

What I have tried:

I tried System.Drawing but that won't work in Azure because of the Sandbox restrictions.

I tried Magick.NET but it is giving the memory stream as corrupt.

I will like to learn from your experiences on this.

Thanks

NutsAndBolts
  • 341
  • 3
  • 13
  • Any update on this issue? – George Chen Feb 12 '20 at 11:49
  • @GeorgeChen: We are not proceeding with the gsdll32.dll as it has the licensing issue. In ImageSharp, we will have to writ the image twice and will then have a performance issue. So, not going ahead with that also. Will keep the updates posted here. – NutsAndBolts Feb 14 '20 at 03:56

2 Answers2

3

You can use ImageSharp which is compatible to .netcore and there's no dependency on System.Drawing:

private static void ResizeAndSavePhoto(Image<Rgba32> img, string path, int squareSize)
{
    img.Mutate(x =>
        x.Resize(new ResizeOptions
        {
            Size = new Size(squareSize, squareSize),
            Mode = ResizeMode.Pad
        }).BackgroundColor(new Rgba32(255, 255, 255, 0)));

    // The following demonstrates how to force png encoding with a path.
    img.Save(Path.ChangeExtension(path, ".jpg"))

    img.Save(path, new PngEncoder());
}

More info: https://github.com/SixLabors/ImageSharp

from: https://stackoverflow.com/a/58761261/1384539

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
1

There is a sandbox limit about System.Drawing, in my experience I used the Magick.NET to solve this problem. You could refer to my previous answer.

In that test I just put gsdll32.dll in the wwwroot folder then it will work, however this time I got a problem it always prompts could not load Magick.NET-Q16-x86.Native.dll file then I upload the Magick.NET-Q16-x86.Native.dll file from the runtime\native folder and this will solve the problem.

Below is my test code.

[FunctionName("Function1")]
        public static void Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,ExecutionContext context,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            MagickNET.SetGhostscriptDirectory(context.FunctionAppDirectory);

            using (var img = new MagickImage(context.FunctionAppDirectory + "\\test.jpg"))
            {

                img.Write(context.FunctionAppDirectory + "\\test.png");
            }
        } 

Here is the result and the bin folder.

enter image description here

enter image description here

George Chen
  • 13,703
  • 2
  • 11
  • 26