0

I'm sending an image via Postman

enter image description hereI want to use the image as an Bitmap in my function.

I am trying

  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();
            }
        }

but i am getting the error: "System.Private.CoreLib: Exception while executing function: IsFundus. ZKWeb.System.Drawing: A null reference or invalid value was found [GDI+ status: InvalidParameter]." from : image.Save(memoryStream, ImageFormat.Jpeg);

Community
  • 1
  • 1
Jake_2
  • 78
  • 15

1 Answers1

1

Instead of uploading your image as Form-data, upload it as binary in postman and it should work just fine. Your Function code looks ok so far. (I would remove the "get" though, in the Function header, as only POST really makes sense for your case)

silent
  • 14,494
  • 4
  • 46
  • 86