无法在Azure函数中使用JpegBitmapEncoder

编程入门 行业动态 更新时间:2024-10-09 02:26:23
本文介绍了无法在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

本文发布于:2023-11-13 03:07:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583252.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Azure   JpegBitmapEncoder

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!