2017-08-25 43 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