static async Task ReadText(string imageFilePath) { try { uriBase = endpoint + "vision/v3.1/read/analyze"; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpClient client = new HttpClient(); // Request headers. client.DefaultRequestHeaders.Add( "Ocp-Apim-Subscription-Key", subscriptionKey); string url = uriBase; HttpResponseMessage response; // Two REST API methods are required to extract text. // One method to submit the image for processing, the other method // to retrieve the text found in the image. // operationLocation stores the URI of the second REST API method, // returned by the first REST API method. string operationLocation; // Reads the contents of the specified local image // into a byte array. byte[] byteData = GetImageAsByteArray(imageFilePath); // Adds the byte array as an octet stream to the request body. using (ByteArrayContent content = new ByteArrayContent(byteData)) { // This example uses the "application/octet-stream" content type. // The other content types you can use are "application/json" // and "multipart/form-data". content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); // The first REST API method, Batch Read, starts // the async process to analyze the written text in the image. response = await client.PostAsync(url, content); } // The response header for the Batch Read method contains the URI // of the second method, Read Operation Result, which // returns the results of the process in the response body. // The Batch Read operation does not return anything in the response body. if (response.IsSuccessStatusCode) operationLocation = response.Headers.GetValues("Operation-Location").FirstOrDefault(); else { // Display the JSON error data. string errorString = await response.Content.ReadAsStringAsync(); Console.WriteLine("\n\nResponse:\n{0}\n", JToken.Parse(errorString).ToString()); return; } // If the first REST API method completes successfully, the second // REST API method retrieves the text written in the image. // // Note: The response may not be immediately available. Text // recognition is an asynchronous operation that can take a variable // amount of time depending on the length of the text. // You may need to wait or retry this operation. // // This example checks once per second for ten seconds. string contentString; int i = 0; do { System.Threading.Thread.Sleep(1000); response = await client.GetAsync(operationLocation); contentString = await response.Content.ReadAsStringAsync(); ++i; } while (i < 60 && contentString.IndexOf("\"status\":\"succeeded\"") == -1); if (i == 60 && contentString.IndexOf("\"status\":\"succeeded\"") == -1) { Console.WriteLine("\nTimeout error.\n"); return; } // Display the JSON response. Console.WriteLine("\nResponse:\n\n{0}\n", JToken.Parse(contentString).ToString()); } catch (Exception e) { Console.WriteLine("\n" + e.Message); } }