|
| 1 | +//Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +//Licensed under the MIT License. |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Text; |
| 6 | +using System.Net; |
| 7 | + |
| 8 | +namespace ConsoleAppPost |
| 9 | +{ |
| 10 | + // This sample uploads an image file for insights obtained from the Bing Image Search endpoint. |
| 11 | + // For more information, see https://docs.microsoft.com/en-us/azure/cognitive-services/bing-image-search/tutorial-image-post |
| 12 | + class Program |
| 13 | + { |
| 14 | + // Replace the accessKey string value with your valid access key. |
| 15 | + const string accessKey = "YOUR-ACCESS-KEY"; |
| 16 | + |
| 17 | + // The endpoint URI. |
| 18 | + const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/images/details"; |
| 19 | + |
| 20 | + // The image to upload. Replace with your file and path. |
| 21 | + const string imageFile = @"C:\Users\USER\PATH\IMAGE.jpg"; |
| 22 | + |
| 23 | + // Used to return image results including relevant headers |
| 24 | + struct SearchResult |
| 25 | + { |
| 26 | + public String jsonResult; |
| 27 | + public Dictionary<String, String> relevantHeaders; |
| 28 | + } |
| 29 | + |
| 30 | + static void Main() |
| 31 | + { |
| 32 | + Console.OutputEncoding = System.Text.Encoding.UTF8; |
| 33 | + |
| 34 | + if (accessKey.Length == 32) |
| 35 | + { |
| 36 | + //Console.WriteLine("Searching images for: " + searchTerm); |
| 37 | + |
| 38 | + SearchResult result = BingImageSearch(uriBase); |
| 39 | + |
| 40 | + Console.WriteLine("\nRelevant HTTP Headers:\n"); |
| 41 | + foreach (var header in result.relevantHeaders) |
| 42 | + Console.WriteLine(header.Key + ": " + header.Value); |
| 43 | + |
| 44 | + Console.WriteLine("\nJSON Response:\n"); |
| 45 | + Console.WriteLine(JsonPrettyPrint(result.jsonResult)); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + Console.WriteLine("Invalid Bing Search API subscription key!"); |
| 50 | + Console.WriteLine("Please paste yours into the source code."); |
| 51 | + } |
| 52 | + |
| 53 | + Console.Write("\nPress Enter to exit "); |
| 54 | + Console.ReadLine(); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Performs a Bing Image search and return the results as a SearchResult. |
| 59 | + /// </summary> |
| 60 | + static SearchResult BingImageSearch(string uriStr) |
| 61 | + { |
| 62 | + WebClient client = new WebClient(); |
| 63 | + client.Headers["Ocp-Apim-Subscription-Key"] = accessKey; |
| 64 | + //client.Headers["Content-Type"] = "multipart/form-data"; |
| 65 | + |
| 66 | + byte[] resp = client.UploadFile(uriBase + "?modules=All", imageFile); //RecognizedEntities |
| 67 | + var json = System.Text.Encoding.Default.GetString(resp); |
| 68 | + |
| 69 | + // Create result object for return |
| 70 | + var searchResult = new SearchResult() |
| 71 | + { |
| 72 | + jsonResult = json, |
| 73 | + relevantHeaders = new Dictionary<String, String>() |
| 74 | + }; |
| 75 | + |
| 76 | + return searchResult; |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Formats the given JSON string by adding line breaks and indents. |
| 81 | + /// </summary> |
| 82 | + /// <param name="json">The raw JSON string to format.</param> |
| 83 | + /// <returns>The formatted JSON string.</returns> |
| 84 | + static string JsonPrettyPrint(string json) |
| 85 | + { |
| 86 | + if (string.IsNullOrEmpty(json)) |
| 87 | + return string.Empty; |
| 88 | + |
| 89 | + json = json.Replace(Environment.NewLine, "").Replace("\t", ""); |
| 90 | + |
| 91 | + StringBuilder sb = new StringBuilder(); |
| 92 | + bool quote = false; |
| 93 | + bool ignore = false; |
| 94 | + char last = ' '; |
| 95 | + int offset = 0; |
| 96 | + int indentLength = 2; |
| 97 | + |
| 98 | + foreach (char ch in json) |
| 99 | + { |
| 100 | + switch (ch) |
| 101 | + { |
| 102 | + case '"': |
| 103 | + if (!ignore) quote = !quote; |
| 104 | + break; |
| 105 | + case '\\': |
| 106 | + if (quote && last != '\\') ignore = true; |
| 107 | + break; |
| 108 | + } |
| 109 | + |
| 110 | + if (quote) |
| 111 | + { |
| 112 | + sb.Append(ch); |
| 113 | + if (last == '\\' && ignore) ignore = false; |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + switch (ch) |
| 118 | + { |
| 119 | + case '{': |
| 120 | + case '[': |
| 121 | + sb.Append(ch); |
| 122 | + sb.Append(Environment.NewLine); |
| 123 | + sb.Append(new string(' ', ++offset * indentLength)); |
| 124 | + break; |
| 125 | + case '}': |
| 126 | + case ']': |
| 127 | + sb.Append(Environment.NewLine); |
| 128 | + sb.Append(new string(' ', --offset * indentLength)); |
| 129 | + sb.Append(ch); |
| 130 | + break; |
| 131 | + case ',': |
| 132 | + sb.Append(ch); |
| 133 | + sb.Append(Environment.NewLine); |
| 134 | + sb.Append(new string(' ', offset * indentLength)); |
| 135 | + break; |
| 136 | + case ':': |
| 137 | + sb.Append(ch); |
| 138 | + sb.Append(' '); |
| 139 | + break; |
| 140 | + default: |
| 141 | + if (quote || ch != ' ') sb.Append(ch); |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + last = ch; |
| 146 | + } |
| 147 | + |
| 148 | + return sb.ToString().Trim(); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments