使用Azure功能损坏的调整大小的图像

时间:2018-09-06 10:50:08

标签: c# azure azure-functions

我有一个具有一个输入和两个输出的天蓝色功能应用程序。在这种情况下,每当将图像上传到容器:原始图像时,都会触发功能应用程序,该程序将生成两个缩略图图像。

enter image description here

代码:

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 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 static void ResizeImage(int width, int height, bool smartCropping,Stream myBlob, Stream outputBlob)
{    
    string _apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    string _apiUrlBase = "xxxxxxxxxxxxxxxxxxxxxxxxxxx/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) }
};

任何人都可以帮助我解决此问题。

0 个答案:

没有答案