Cannot use JpegBitmapEncoder in Azure Function

爷,独闯天下 提交于 2021-01-27 11:57:37

问题


While testing Azure Functions, I wrote the following blob-triggered code:

#r "System.Drawing"
#r "PresentationCore"
#r "WindowsBase"

using System.Drawing.Imaging;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

public static void Run(Stream imageStream, string providerName, string imageKey, string extension, Stream outputStream, TraceWriter log)
{
    log.Info($"Function triggered by blob\n Name:{imageKey} \n Size: {imageStream.Length} Bytes");

    var decoder = BitmapDecoder.Create(imageStream, BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.None);
    BitmapFrame image = decoder.Frames[0];

    double ratio = Math.Min(200 / (double)image.PixelWidth, 200 / (double)image.PixelHeight);
    var target = new TransformedBitmap(image, new ScaleTransform(ratio, ratio, 0, 0));
    image = BitmapFrame.Create(target);

    var encoder = new JpegBitmapEncoder() { QualityLevel = 85 };
    encoder.Frames.Add(image);
    //encoder.Save(outputStream);
}

If I uncomment the last line, I get the following error:

Exception while executing function: Functions.ProcessImageTest. mscorlib: Exception has been thrown by the target of an invocation. PresentationCore: Specified method is not supported.

I don't understand why JpegBitmapEncoder is available if one cannot use the Savemethod...

What am I missing?


回答1:


It's probably due to sandbox resterictions regarding access of win32k.sys \ GDI+ APIs. You can look here for more details about the sandbox https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#win32ksys-user32gdi32-restrictions

I can also verify that, but I'll need the app name (you can share it either directly or indirectly but in general graphics APIs won't work reliably on app service.




回答2:


I eventually found the following solution:

run.csx

#r "System.Drawing"
#r "PresentationCore"
#r "WindowsBase"
#r "Microsoft.WindowsAzure.Storage"

using Microsoft.WindowsAzure.Storage.Blob;
using System.Drawing.Imaging;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

public static void Run(Stream imageStream, string imageName, string extension, CloudBlockBlob outputBlob, TraceWriter log)
{
    log.Info($"Function triggered by blob\n Name:{imageName} \n Size: {imageStream.Length} Bytes");

    var decoder = BitmapDecoder.Create(imageStream, BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.None);
    BitmapFrame image = decoder.Frames[0];

    double ratio = Math.Min(200 / (double)image.PixelWidth, 200 / (double)image.PixelHeight);
    var target = new TransformedBitmap(image, new ScaleTransform(ratio, ratio, 0, 0));
    image = BitmapFrame.Create(target);

    var encoder = new JpegBitmapEncoder() { QualityLevel = 85 };
    encoder.Frames.Add(image);

    using (var outputStream = new MemoryStream())
    {
        encoder.Save(outputStream);
        outputStream.Position = 0;
        outputBlob.Properties.ContentType = "image/jpeg";
        outputBlob.UploadFromStream(outputStream);
    }
}

function.json

{
  "bindings": [
    {
      "name": "imageStream",
      "type": "blobTrigger",
      "direction": "in",
      "path": "input-container/{imageName}.{extension}",
      "connection": "AzureWebJobsDashboard"
    },
    {
      "type": "blob",
      "name": "outputBlob",
      "path": "output-container/{imageName}.jpg",
      "connection": "AzureWebJobsDashboard",
      "direction": "inout"
    }
  ],
  "disabled": false
}


来源:https://stackoverflow.com/questions/45885567/cannot-use-jpegbitmapencoder-in-azure-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!