无法在Azure功能中使用JpegBitmapEncoder

时间:2017-08-25 16:13:21

标签: c# azure azure-functions

在测试Azure Functions时,我编写了以下blob触发的代码:

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

如果我取消注释最后一行,我会收到以下错误:

  

执行函数时出现异常:Functions.ProcessImageTest。   mscorlib:调用目标抛出了异常。   PresentationCore:不支持指定的方法。

如果无法使用JpegBitmapEncoder方法,我不明白为什么Save可用...

我错过了什么?

2 个答案:

答案 0 :(得分:2)

这可能是由于有关win32k.sys \ {{}} API访问的沙箱重新审核所致。 您可以在此处查看有关沙箱https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#win32ksys-user32gdi32-restrictions

的更多详细信息

我也可以验证,但我需要应用名称(您可以直接或indirectly分享,但一般来说,图形API无法在应用服务上可靠地运行。

答案 1 :(得分:2)

我最终找到了以下解决方案:

#nelmioCorsBundle configuration IN CONFIG.YML nelmio_cors: defaults: allow_credentials: true allow_origin: '*' allow_headers: ['accept', 'content-type', 'authorization', 'x-http-method-override'] allow_methods: ['POST', 'PUT', 'PATCH', 'GET', 'DELETE'] max_age: 3600 paths: '^/': allow_origin: ['http://localhost:4201'] allow_headers: ['Authorization', 'X-Requested-With', 'Content-Type', 'Accept', 'Origin', 'X-Custom-Auth'] allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'OPTIONS'] max_age: 3600 hosts: [] origin_regex: false hosts: ['^\.']

firewalls:
        login:
            pattern: ^/api/login
            form_login:
            provider: fos_userbundle
            login_path: /api/login
            check_path: /api/login_check
            username_parameter: username
            password_parameter: password
            success_handler: lexik_jwt_authentication.handler.authentication_success
            failure_handler: lexik_jwt_authentication.handler.authentication_failure
            require_previous_session: false
        logout:       true
        anonymous:    true

    api:
        pattern:   ^/api
        anonymous: false
        provider: fos_userbundle
        lexik_jwt:  #par defaut check token in Authorization Header prefixer par Bearer
            authorization_header: # check token in Authorization Header
                    enabled: true
                    prefix:  Bearer
                    name:    Authorization
            cookie:               # check token in a cookie
                    enabled: false
                    name:    BEARER
            query_parameter:      # check token in query string parameter
                    enabled: true
                    name:    bearer
            throw_exceptions:        true     # When an authentication failure occurs, return a 401 response immediately
            create_entry_point:      false      # When no authentication details are provided, create a default entry point that returns a 401 response
            authentication_provider: lexik_jwt_authentication.security.authentication.provider
            authentication_listener: lexik_jwt_authentication.security.authentication.listener

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