【问题标题】:Image format change in AzureAzure 中的图像格式更改
【发布时间】:2020-02-11 18:08:54
【问题描述】:

我想做的事: 在我的应用程序中,我具有上传图像的功能。我想在我的Azure function 中将上传的图像更改为 PNG 格式,而不管用户选择了何种格式。

我的尝试

我尝试了System.Drawing,但由于沙盒限制,这在 Azure 中不起作用。

我尝试了Magick.NET,但它显示内存流已损坏。

我想从你的经验中学习。

谢谢

【问题讨论】:

  • 这个问题有什么更新吗?
  • @GeorgeChen:我们不会继续使用 gsdll32.dll,因为它存在许可问题。在 ImageSharp 中,我们必须将图像写入两次,然后就会出现性能问题。所以,也不要继续这样做。将在此处发布更新。

标签: c# image azure azure-functions


【解决方案1】:

您可以使用与 .netcore 兼容且不依赖 System.Drawing 的 ImageSharp:

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());
}

更多信息:https://github.com/SixLabors/ImageSharp

来自:https://stackoverflow.com/a/58761261/1384539

【讨论】:

    【解决方案2】:

    System.Drawing 有一个沙盒限制,根据我的经验,我使用Magick.NET 来解决这个问题。可以参考我之前的answer

    在那次测试中,我只是将gsdll32.dll 放在 wwwroot 文件夹中,然后它就可以工作了,但是这次我遇到了一个问题,它总是提示无法加载 Magick.NET-Q16-x86.Native.dll 文件然后我上传了运行时\native 文件夹中的 Magick.NET-Q16-x86.Native.dll 文件,这将解决问题。

    下面是我的测试代码。

    [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");
                }
            } 
    

    这是结果和 bin 文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-27
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      相关资源
      最近更新 更多