无法在Azure函数中使用JpegBitmapEncoder [英] Cannot use JpegBitmapEncoder in Azure Function

查看:56
本文介绍了无法在Azure函数中使用JpegBitmapEncoder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

执行功能时发生异常:Functions.ProcessImageTest.mscorlib:调用的目标已引发异常.PresentationCore:不支持指定的方法.

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

我不明白为什么如果不能使用 Save 方法...

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

我想念什么?

推荐答案

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

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
}

这篇关于无法在Azure函数中使用JpegBitmapEncoder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆