2017-08-25 78 views
2

在测试Azure的功能,我写了下面的blob触发代码:在Azure的功能不能使用JpegBitmapEncoder

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

如果我去掉最后一行,我得到以下错误:

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

我不明白为什么JpegBitmapEncoder可用,如果一个人不能使用Save方法...

我在想什么?

回答

0

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

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 
} 
1

这可能是由于有关访问win32k.sys \ GDI+ API的沙盒阻止。 你可以看看这里了解沙箱https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#win32ksys-user32gdi32-restrictions

我还可以验证更多的细节,但我需要的应用程序名称(你可以分享它直接或indirectly但总体图形API将无法可靠地工作,上。应用服务

+0

非常感谢。如果你确实可以确认问题来自哪里,那将是非常好的。这里是我的应用程序的详细信息:''2017-08-28T07:39:15.489函数启动(Id = 4d286e48-d80c-4b7f-b3fc-2c1cf30734e4)''。地区:西欧。 – Rodolphe

+0

另外,你会建议一个简单的blob触发的图像调整器,就像我想要实现的一样? – Rodolphe