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

Chris Rae
  • 5,627
  • 2
  • 36
  • 51
  • 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? – Chris Rae May 17 '18 at 23:25
  • Have you checked the official tutorial for this? https://github.com/Azure-Samples/function-image-upload-resize/blob/master/imageresizerfunc/run.csx – kamil-mrzyglod May 18 '18 at 08:05
  • Thank you but I don't think this is what that tutorial is about. – Chris Rae May 18 '18 at 16:49

1 Answers1

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://stackoverflow.com/a/3212765/498949 before performing any Bitmap operations on it.

Chris Rae
  • 5,627
  • 2
  • 36
  • 51