/// <summary> /// Asynchronously uploads the specified image to Google Images, /// and returns the URL of the results page. /// </summary> /// <param name="imagePath">Path to the image file</param> /// <param name="includeFileName">Whether to send the image file name to Google</param> /// <param name="resizeOnUpload">Whether to resize large images</param> /// <param name="cancelToken">Allows for cancellation of the upload</param> /// <returns>String containing the URL of the results page</returns> public static async Task <string> Search(string imagePath, bool includeFileName, bool resizeOnUpload, CancellationToken cancelToken) { // Load the image, resizing it if necessary byte[] data = LoadImageData(imagePath, resizeOnUpload); // Prevent auto redirect (we want to open the // redirect destination directly in the browser) var handler = new HttpClientHandler(); handler.AllowAutoRedirect = false; using (var client = new HttpClient(handler)) { var form = new MultipartFormDataContentCompat(); form.Add(new StringContent(BinaryToBase64Compat(data)), "image_content"); if (includeFileName) { form.Add(new StringContent(Path.GetFileName(imagePath)), "filename"); } var response = await client.PostAsync("https://images.google.com/searchbyimage/upload", form, cancelToken); if (response.StatusCode != HttpStatusCode.Redirect) { throw new IOException("Expected redirect to results page, got " + (int)response.StatusCode); } string resultUrl = response.Headers.Location.ToString(); return(resultUrl); } }
public async Task GoogleImageSearch(string imagePath, bool includeFileName, CancellationToken cancelToken) { if (memStream == null) { return; } byte[] data = memStream.ToArray(); // Prevent auto redirect (we want to open the // redirect destination directly in the browser) var handler = new HttpClientHandler(); handler.AllowAutoRedirect = false; using (var client = new HttpClient(handler)) { var form = new MultipartFormDataContentCompat(); form.Add(new StringContent(BinaryToBase64Compat(data)), "image_content"); if (includeFileName) { form.Add(new StringContent(Path.GetFileName(imagePath)), "filename"); } var response = await client.PostAsync("https://images.google.com/searchbyimage/upload", form, cancelToken); if (cancelToken.IsCancellationRequested) { return; } if (response.StatusCode != HttpStatusCode.Redirect) { MessageBox.Show("Expected redirect to results page, got " + (int)response.StatusCode); return; } string resultUrl = response.Headers.Location.ToString(); TryOpenBrowser(resultUrl); } }