NutsAndBolts
NutsAndBolts

Reputation: 429

Image format change in Azure

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

Upvotes: 2

Views: 485

Answers (2)

George Chen
George Chen

Reputation: 14334

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

Upvotes: 1

Thiago Custodio
Thiago Custodio

Reputation: 18387

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

Upvotes: 3

Related Questions