从httpRequest主体生成位图

时间:2019-05-27 15:42:37

标签: bitmap azure-functions

我正在通过邮递员发送图片

enter image description here我想将图像用作函数中的位图。

我正在尝试

  public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("IsFundusCalled");

            string name = req.Query["image"];
            var imageData = ReadFully(req.Body);
            Bitmap image;
            using (var ms = new MemoryStream(imageData))
            {
                image = new Bitmap(ms);
            }
            MemoryStream memoryStream = new MemoryStream();
            image.Save(memoryStream, ImageFormat.Jpeg);

            HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
            httpResponseMessage.Content = new ByteArrayContent(memoryStream.ToArray());
            httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
            httpResponseMessage.StatusCode = HttpStatusCode.OK;

            return httpResponseMessage;
        }

        //from https://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream
        public static byte[] ReadFully(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

但是我得到了错误: “ System.Private.CoreLib:执行功能时异常:IsFundus。ZKWeb.System.Drawing:找到空引用或无效值[GDI +状态:InvalidParameter]。” 来自:image.Save(memoryStream, ImageFormat.Jpeg);

1 个答案:

答案 0 :(得分:1)

与其将图像作为表单数据上传,还不如将其作为二进制文件上传到邮递员中,它应该可以正常工作。到目前为止,您的功能代码看起来还不错。 (不过,我会在Function标头中删除“ get”,因为只有POST才对您有意义)。