c# - Azure Functions 使用两个输出 blob 调整 blob 大小

标签 c# azure azure-functions outputstream

我有一个 Azure 函数,可以调整图像大小,然后保存到另一个容器文件夹。

我的代码是;

private static readonly Dictionary<ImageSize, Tuple<int, int>> ImageDimensionsTable = new Dictionary<ImageSize, Tuple<int, int>>()
{
    { ImageSize.ExtraSmall, Tuple.Create(320, 200) },
    { ImageSize.Small,      Tuple.Create(640, 400) },
    { ImageSize.Medium,     Tuple.Create(800, 600) }
};

private enum ImageSize
{
    /// <summary>
    /// The extra small
    /// </summary>
    ExtraSmall,

    /// <summary>
    /// The small
    /// </summary>
    Small,

    /// <summary>
    /// The medium
    /// </summary>
    Medium
}

[FunctionName("Function1")]
public static void Run(
    [BlobTrigger("vehicle-images/{folder}/{name}", Connection = "")]Stream image,
    string name,
    [Blob("watermarked-vehicle-images/{folder}/s-{name}", FileAccess.ReadWrite, Connection = "")]CloudBlockBlob imageSmall,
    [Blob("watermarked-vehicle-images/{folder}/m-{name}", FileAccess.ReadWrite, Connection = "")]CloudBlockBlob imageMedium,
    TraceWriter log)
{
    // Resize and save Small Image
    var size = ImageDimensionsTable[ImageSize.Small];
    var outputStream = new MemoryStream();
    var instructions = new Instructions
    {
        Width = size.Item1,
        Height = size.Item2,
        Mode = FitMode.Max,
        OutputFormat = OutputFormat.Jpeg,
        JpegQuality = 85
    };

    ImageBuilder.Current.Build(new ImageJob(image, outputStream, instructions));
    outputStream.Seek(0, SeekOrigin.Begin);

    imageSmall.Properties.ContentType = "image/jpeg";
    imageSmall.UploadFromStream(outputStream);

    // Resize and save Medium Image.
    size = ImageDimensionsTable[ImageSize.Medium];    

    var outputStream2 = new MemoryStream();
    var instructions2 = new Instructions
    {
        Width = size.Item1,
        Height = size.Item2,
        Mode = FitMode.Max,
        OutputFormat = OutputFormat.Jpeg,
        JpegQuality = 85
    };

    ImageBuilder.Current.Build(new ImageJob(image, outputStream2, instructions2));
    outputStream2.Seek(0, SeekOrigin.Begin);

    imageMedium.Properties.ContentType = "image/jpeg";
    imageMedium.UploadFromStream(outputStream2);

}

因此,当前正在执行的操作是触发图像上传,执行将大小调整为小尺寸的第一步,但是当它到达将大小调整为中尺寸的代码时,它会给出错误错误:my-vehicle-image.jpg 参数无效。

所以我怀疑我的原始Stream image正在被关闭?或处置,以便当我尝试使用 ImageBuilder.Current.Build(new ImageJob(image, outputStream2, instructions2)); 这是无效的?

希望这个问题很容易解决。我的替代方案是使用 2 个函数,其中一个函数调整小函数的大小,另一个函数调整大函数的大小,这也是可行的。

最佳答案

好吧,发现 ImageJob 有一个接受 bool disposeSource 的重载,所以我已将代码更新为;

ImageBuilder.Current.Build(new ImageJob(image, outputStream, instructions, false, false));

这样,当我第二次想使用image时,它仍然可用。

关于c# - Azure Functions 使用两个输出 blob 调整 blob 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52967303/

相关文章:

c# - 优雅地处理任务取消

azure - 如何使用 ARM 模板更新 Azure 函数中的运行时规模监控设置

azure - 是否可以使用 Azure Active Directory 身份验证在本地调试 Azure Functions?

c# - 我什么时候应该为 ApartmentState.STA 设置一个线程?

c# - 路由 : How to hide action name in url?

php - 当我向 Azure 中的 Web.config 添加重写规则时,为什么会收到 500 错误?

Azure - web.config 中的 customErrors ="off"未在 Azure 应用程序(云服务)中显示详细错误

c# - Newtonsoft.Json 引用提示 Azure Functions

c# - 我如何使用 for 或 while 循环以及在主体外部声明的 int?

使用企业 Active Directory 的 Azure