繁体   English   中英

在Azure环境上托管functionapp的情况下,从Dictionary中读取值的问题

[英]Issue in reading values from Dictionary in case of functionapp hosted on Azure environment

我在Azure环境上具有以下功能应用程序设置。 我试图按照链接的输入进行扩展

https://mitra.computa.asia/articles/msdn-smart-image-re-sizing-azure-functions-and-cognitive-services

在这里,无论何时将任何图像上传到容器,我都试图生成两个不同大小的缩略图。

我有一个输入:myBlob和在这种情况下添加的两个不同的输出:Integrate部分下的outputBlobsm,outputBlobmd。

功能应用代码:

using System;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Collections.Generic;


public static void Run(Stream myBlob,string blobname, string blobextension, Stream outputBlobsm,Stream outputBlobmd, TraceWriter log)
{
    bool smartCropping = true;

    log.Info($"C# Blob trigger function Processed blob\n Name:{blobname} \n Extension: {blobextension} extension");

    var sizesm = imageDimensionsTable[ImageSize.Small];
    log.Info($"C# Blob \n Sizesm:{sizesm}");
    log.Info($"C# Blob \n width:{sizesm.item1} \n height: {sizesm.item2}");
    ResizeImage(sizesm.item1, sizesm.item2,smartCropping,myBlob, outputBlobsm);

    var sizemd = imageDimensionsTable[ImageSize.Medium];
    log.Info($"C# Blob \n width:{sizemd.item1} \n height: {sizemd.item2}");
    ResizeImage(sizemd.item1, sizemd.item2,smartCropping,myBlob, outputBlobmd);
}

public void ResizeImage(int width, int height, bool smartCropping,Stream myBlob, Stream outputBlob)
{    
    string _apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxx";
    string _apiUrlBase = "xxxxxxxxxxxxxxxxxxx/generateThumbnail";
  using (var httpClient = new HttpClient())
    {
        httpClient.BaseAddress = new Uri(_apiUrlBase);
        httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _apiKey);
        using (HttpContent content = new StreamContent(myBlob))
        {
            //get response
            content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/octet-stream");
            var uri = $"{_apiUrlBase}?width={width}&height={height}&smartCropping={smartCropping.ToString()}";
            var response = httpClient.PostAsync(uri, content).Result;
            var responseBytes = response.Content.ReadAsByteArrayAsync().Result;

            //write to output thumb
            outputBlob.Write(responseBytes, 0, responseBytes.Length);
        }
    }
}

public enum ImageSize { ExtraSmall, Small, Medium }

private static Dictionary<ImageSize, (int, int)> imageDimensionsTable = new Dictionary<ImageSize, (int, int)>() {
    { ImageSize.ExtraSmall, (320, 200) },
    { ImageSize.Small,      (640, 400) },
    { ImageSize.Medium,     (800, 600) }
};

在编译代码时,出现以下错误:

[Error] run.csx(16,41): error CS1061: '(int, int)' does not contain a definition for 'item1' and no extension method 'item1' accepting a first argument of type '(int, int)' could be found (are you missing a using directive or an assembly reference?)

谁能帮我解决此问题。

C#区分大小写。 如果未指定名称,则新的元组应为Item1,Item2...。

在“小报”下查看示例

请注意,您还可以在声明中为这些int指定名称:

 private static Dictionary<ImageSize, (int Width, int Height)> imageDimensionsTable

这使得

ResizeImage(sizesm.Width, sizesm.Height,smartCropping,myBlob, outputBlobsm);

更具可读性

哦...您可能需要将ResizeImage静态

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM