本文介绍了Azure函数抛出“参数无效”;在大型TIF文件上调用Image.FromStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Azure函数中处理多页TIF。该功能由Blob存储中的更改触发。当触发器运行时,它将调用:

I'm trying to process multi-page TIFs in an Azure Function. The function is triggered by changes in blob storage. When the trigger runs, it calls this:

function loadFile(Stream mpTif);
     Bitmap pageOnes = (Bitmap)Image.FromStream(mpTif);

mpTif 是要传递的Blob存储流直接在Azure函数中。

mpTif is the blob storage Stream being passed directly into the Azure Function.

我的函数在小型多页TIF文件上工作正常,但是当我在blob存储中放入一个很大的文件时,在<$上失败c $ c> Image.FromStream 并显示错误:

My function works fine on small multi-page TIF files but when I put a very large one in the blob storage, it fails on Image.FromStream with the error:

我正在使用本地函数主机在自己的计算机上运行此命令。奇怪的是,我有一个控制台应用程序,它使用完全相同的代码运行,但使用MemoryStream调用它:

I am running this on my own machine using the local function host. The strange thing is that I have a console application which runs using the exact same code but calls it using a MemoryStream instead:

MemoryStream data = new MemoryStream(File.ReadAllBytes("big.tif"));
loadFile(data);

这很好。我是否在Azure Functions中达到某种内存限制?我很可能要花很长时间才能找到该错误,这使我认为这是OOM。这个TIF文件非常大(80Mb和10,000页)。

This works fine. Am I hitting some sort of memory limit in Azure Functions? It takes suspiciously long before I hit that error, which makes me think it's an OOM thing. This TIF file is very large (80Mb and 10,000 pages).

推荐答案

我已经了解了这一点-事实证明,位图在Blob Streams上运行这些操作一点也不高兴。性能非常糟糕(可能慢了100倍),并且对大文件的操作因我上面提供的错误而失败。

I got to the bottom of this - it turns out Bitmap operations are not at all happy running off the blob Streams. The performance is terrible (perhaps 100 times slower) and operations on large files just fail with the error I provided above.

我通过复制传入的Stream解决了所有问题。在执行任何位图操作之前,使用提供的代码将其添加到MemoryStream

I resolved all of my issues by copying the incoming Stream to a MemoryStream using the code provided at https://stackoverflow.com/a/3212765/498949 before performing any Bitmap operations on it.

这篇关于Azure函数抛出“参数无效”;在大型TIF文件上调用Image.FromStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:54