public async Task <BingVisualSearchResponse> FindSimilarProductsAsync(string imageUrl, BoundingRectangle queryRectangle) { var cab = Math.Max(queryRectangle.BottomLeft.Y, queryRectangle.BottomRight.Y); var cal = Math.Min(queryRectangle.TopLeft.X, queryRectangle.BottomLeft.X); var car = Math.Max(queryRectangle.TopRight.X, queryRectangle.BottomRight.X); var cat = Math.Min(queryRectangle.TopLeft.Y, queryRectangle.TopRight.Y); var content = new MultipartFormDataContent("--------------------------498758971529224930840173"); var image = await ImageUtils.GetImageAsync(imageUrl); content.Add(new ByteArrayContent(image) { Headers = { ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "image", FileName = "image", }, ContentType = new MediaTypeHeaderValue("image/jpeg"), }, }); BingVisualSearchResponse result = null; content.Headers.Add("Ocp-Apim-Subscription-Key", _bingSearchKey); using (var response = await Client.PostAsync($"{ServiceUri}/visualsearch?mkt=en-us&safeSearch=Strict&cab={cab}&cal={cal}&car={car}&cat={cat}", content)) { var json = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject <BingVisualSearchResponse>(json); } return(result); }
public async Task <BingVisualSearchResponse> FindSimilarProductsAsync(string imageUrl) { BingVisualSearchResponse result = null; try { var content = new MultipartFormDataContent("--------------------------498758971529224930840173"); var image = await ImageUtils.GetImageAsync(imageUrl); content.Add(new ByteArrayContent(image) { Headers = { ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "image", FileName = "image", }, ContentType = new MediaTypeHeaderValue("image/jpeg"), }, }); content.Headers.Add("Ocp-Apim-Subscription-Key", _bingSearchKey); using (var response = await Client.PostAsync($"{ServiceUri}/visualsearch?mkt=en-us&safeSearch=Strict", content)) { response.EnsureSuccessStatusCode(); var json = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject <BingVisualSearchResponse>(json); } } catch (Exception ex) { Console.WriteLine("Encountered exception. " + ex.Message); } return(result); }