コード例 #1
0
        /// <summary>
        /// Submits an image for descriptive analysis.
        /// </summary>
        /// <param name="imageByteArray">The image to analyze as a byte array.</param>
        /// <param name="language">The language to use in the returned description.</param>
        /// <returns>An Azure ImageAnalysis object or null.</returns>
        private async Task <ImageAnalysis> MakeDescriptiveAnalysisRequestToAzure(byte[] imageByteArray, string language)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ConnectionDetails.SubscriptionKey);
                using (var content = new ByteArrayContent(imageByteArray))
                {
                    try
                    {
                        var uri = ConnectionDetails.GetAnalyzeUrl(language);
                        content.Headers.ContentType =
                            new MediaTypeHeaderValue("application/octet-stream");

                        var response = await client.PostAsync(uri, content);

                        response.EnsureSuccessStatusCode();


                        var json = await response.Content.ReadAsStringAsync();

                        return(JsonConvert.DeserializeObject <ImageAnalysis>(json));
                    }
                    catch (HttpRequestException ex)
                    {
                        Console.WriteLine(ex);
                        throw;
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Submits an image for descriptive analysis.
        /// </summary>
        /// <param name="imageByteArray">The image to analyze as a byte array.</param>
        /// <param name="language">The language to use in the returned description.</param>
        /// <returns>An Azure ImageAnalysis object or null.</returns>
        private ImageAnalysis MakeDescriptiveAnalysisRequestToAzure(byte[] imageByteArray, string language)
        {
            ImageAnalysis   output   = null;
            HttpWebResponse response = null;

            try
            {
                var request = WebRequest.CreateHttp(new Uri(ConnectionDetails.GetAnalyzeUrl(language)));
                request.ContentType = "application/octet-stream";
                request.Headers.Add("Ocp-Apim-Subscription-Key", ConnectionDetails.SubscriptionKey);
                request.Method        = "POST";
                request.ContentLength = imageByteArray.Length;
                var requestStream = request.GetRequestStream();
                requestStream.Write(imageByteArray, 0, imageByteArray.Length);
                requestStream.Close();


                response = (HttpWebResponse)request.GetResponse();


                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new HttpRequestException("did not return success");
                }

                // ReSharper disable once AssignNullToNotNullAttribute
                var reader = new StreamReader(response.GetResponseStream());
                output = JsonConvert.DeserializeObject <ImageAnalysis>(reader.ReadToEnd());
            }
            catch (HttpRequestException ex)
            {
                Console.WriteLine(ex);
                throw;
            }
            finally
            {
                response?.Close();
            }

            return(output);
        }