How to send image in C# to a Flask Server that is decoded by OpenCV?

Here is part of my Flask API in Python:

image_data = flask.request.get_data() # image_data's data type string image_vector = numpy.frombuffer(image_data, dtype=numpy.uint8)  image = cv2.imdecode(image_vector, cv2.IMREAD_COLOR)   

How would I send a image that I encoded like below, in C#:

        ResultString = "Loading...";         var surface = SKSurface.Create(new SKImageInfo((int)canvasView.CanvasSize.Width,                                         (int)canvasView.CanvasSize.Height));         var canvas = surface.Canvas;         canvas.Clear();          foreach (SKPath path in completedPaths)             canvas.DrawPath(path, paint);          foreach (SKPath path in inProgressPaths.Values)             canvas.DrawPath(path, paint);          canvas.Flush();          var snap = surface.Snapshot();         var pngImage = snap.Encode(SKEncodedImageFormat.Png, 100);         AnalyerResults analyerResults = mathclient.AnalyzeWork(pngImage);         try { ResultString = analyerResults.message; } catch { ResultString = "Error..."; }  

How would I send the image to in C# to be able to be received and decoded like shown in part of my API? I already tried:

HttpClient client = await GetClient(); var result = await client.PostAsync(Url + "analyzer", new ByteArrayContent(pngImage.ToArray())); return JsonConvert.DeserializeObject<AnalyerResults>(await result.Content.ReadAsStringAsync()); 

I also tried:

var client = new RestClient(Url + "analyzer"); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "image/png"); request.AddParameter("image/png", pngImage, ParameterType.RequestBody); IRestResponse response = client.Execute(request); return JsonConvert.DeserializeObject<AnalyerResults>(response.Content); 

However in both the content returned null. This question is related to How to Replicate this Postman Request which has a Binary Content Body and contains a .PNG File in C#?.

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.