无法在 Azure 函数中使用 JpegBitmapEncoder
Cannot use JpegBitmapEncoder in Azure Function
在测试 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);
}
如果我取消注释最后一行,我会收到以下错误:
Exception while executing function: Functions.ProcessImageTest.
mscorlib: Exception has been thrown by the target of an invocation.
PresentationCore: Specified method is not supported.
我不明白如果不能使用 Save
方法,为什么 JpegBitmapEncoder
可用...
我错过了什么?
这可能是由于有关访问 win32k.sys
\ GDI+
API 的沙盒限制。
您可以在此处查看有关沙箱的更多详细信息 https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#win32ksys-user32gdi32-restrictions
我也可以验证这一点,但我需要应用名称(您可以直接共享它或 indirectly,但通常图形 API 无法在应用服务上可靠地工作。
我最终找到了以下解决方案:
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
}
在测试 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);
}
如果我取消注释最后一行,我会收到以下错误:
Exception while executing function: Functions.ProcessImageTest. mscorlib: Exception has been thrown by the target of an invocation. PresentationCore: Specified method is not supported.
我不明白如果不能使用 Save
方法,为什么 JpegBitmapEncoder
可用...
我错过了什么?
这可能是由于有关访问 win32k.sys
\ GDI+
API 的沙盒限制。
您可以在此处查看有关沙箱的更多详细信息 https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#win32ksys-user32gdi32-restrictions
我也可以验证这一点,但我需要应用名称(您可以直接共享它或 indirectly,但通常图形 API 无法在应用服务上可靠地工作。
我最终找到了以下解决方案:
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
}