Azure function throwing "parameter not valid" calling Image.FromStream on large TIF file
Asked Answered
T

1

0

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 is the blob storage Stream being passed directly into the Azure Function.

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:

Parameter is not valid

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

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).

Turku answered 17/5, 2018 at 19:47 Comment(3)
I suspect there might be a bit more to this because even on small TIF files all the image operations are way slower when running in the function host than they are running "normally". Is it wrapping the GDI functions in some sort of odd way?Turku
Have you checked the official tutorial for this? github.com/Azure-Samples/function-image-upload-resize/blob/…Engineering
Thank you but I don't think this is what that tutorial is about.Turku
T
1

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.

I resolved all of my issues by copying the incoming Stream to a MemoryStream using the code provided at https://mcmap.net/q/211078/-how-to-get-a-memorystream-from-a-stream-in-net before performing any Bitmap operations on it.

Turku answered 18/5, 2018 at 17:19 Comment(1)
I have the exact same problem. Really destroying performanceVolotta

© 2022 - 2024 — McMap. All rights reserved.