【问题标题】:Compress images in azure blob with ImageMagick使用 ImageMagick 压缩 azure blob 中的图像
【发布时间】:2018-09-11 09:55:16
【问题描述】:

我正在编写一个 azure 函数来压缩上传到 blob 存储的图像并将压缩后的图像保存到另一个 blob。我为此使用Magick.NET nuget 包。

[FunctionName("Function1")]
    public static void Run(
        [BlobTrigger("test/{name}", Connection = "")]Stream image,
        [Blob("test-output/{name}", FileAccess.ReadWrite)]CloudBlockBlob output,
        string name,
        TraceWriter log)
    {
        log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {image.Length} Bytes");
        using (MemoryStream memoryStream = new MemoryStream())
        {
            image.CopyTo(memoryStream);
            ImageOptimizer imageOptimizer = new ImageOptimizer();
            imageOptimizer.Compress(memoryStream);
            output.UploadFromStream(memoryStream);
        }
    }

但是下面一行抛出异常,

关于如何解决此问题的任何建议?

【问题讨论】:

    标签: c# .net azure imagemagick azure-functions


    【解决方案1】:

    复制后需要回退MemoryStream

    image.CopyTo(memoryStream);
    memoryStream.Position = 0;
    

    否则,该位置将在数据的末尾,对其进行任何读取操作都将返回 0 数据。

    【讨论】:

      猜你喜欢
      • 2015-12-01
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 2020-12-10
      • 2017-04-05
      • 1970-01-01
      • 2020-04-15
      相关资源
      最近更新 更多