public void searchImageFromGoogle(IProgress<string> progress) { //string apiKey = "AIzaSyCKOq5EJwqwfQzmdfCW0VE-IX9fFMIZEUM"; //string searchEngineId = "002524252275919064823:dlgwbkge9f0"; string apiKey = "AIzaSyCOQh0JLePZur6o26W1lwI5mpj5pa3M5oA"; string searchEngineId = "001783529186805515716:jqccojujq9c"; GoogleImage googleImage = new GoogleImage(); string query = tbKeyWord.Text; //start number of result return int count = 0; while (count < 10) { Search images = googleImage.googleImageSearch(apiKey, searchEngineId, query, start); foreach (var item in images.Items) { try { //tbGoogleLink.Text += item.Link + "\n"; progress.Report(item.Link); using (WebClient webClient = new WebClient()) { byte[] data = webClient.DownloadData(item.Link); using (MemoryStream mem = new MemoryStream(data)) { using (var yourImage = Image.FromStream(mem)) { string new_path = Path.Combine(FB_Image.RelativeDirectory, FB_Image.SuccessImageDir,query + "_" + gtemp + ".jpg"); var newImage = googleImage.ScaleImage(yourImage, 960, 720); newImage.Save(new_path, ImageFormat.Jpeg); } } } } catch (Exception e) { Log(e); } gtemp += 1; } MyHelper.DeleteFile(MyHelper.ListFileDelete); start += 10; } }
public void GetProcessImageFromGoogleImage(IProgress<string> progress) { //string apiKey = "AIzaSyCKOq5EJwqwfQzmdfCW0VE-IX9fFMIZEUM"; //string searchEngineId = "002524252275919064823:dlgwbkge9f0"; string apiKey = "AIzaSyCOQh0JLePZur6o26W1lwI5mpj5pa3M5oA"; string searchEngineId = "001783529186805515716:jqccojujq9c"; GoogleImage googleImage = new GoogleImage(); string query = tbKeyWord.Text; //start number of result return int count = 0; int numOfImage = 5; while (count <numOfImage) { Search images = googleImage.googleImageSearch(apiKey, searchEngineId, query, start); foreach (var item in images.Items) { try { //tbGoogleLink.Text += item.Link + "\n"; progress.Report(item.Link); using (WebClient webClient = new WebClient()) { byte[] data = webClient.DownloadData(item.Link); using (MemoryStream mem = new MemoryStream(data)) { using (var yourImage = Image.FromStream(mem)) { string new_path = Path.Combine(FB_Image.BaseDirectory , query + "_" + gtemp + ".jpg"); var newImage = googleImage.ScaleImage(yourImage, 960, 720); newImage.Save(new_path, ImageFormat.Jpeg); MyHelper.ListFileDelete.Add(new_path); string filename = new_path; string testMessage = SplitFileIntoPart(Path.Combine(FB_Image.RelativeDirectory,FB_Image.TestInputDir,"200.txt"),0)[0]; string coverImagePath = TestEncode(null, filename, testMessage, ActiveUser.PrivateAlbumID, true); if (coverImagePath != null) { File.Copy(coverImagePath, Path.Combine(FB_Image.RelativeDirectory,FB_Image.SuccessImageDir, query + "_" + gtemp + ".jpg"), true); count++; progress.Report("success"+Environment.NewLine); } else { progress.Report("Fail"+Environment.NewLine); } } } } if (cancelSearching) { isStart = false; return; } } catch (Exception e) { Log(e); } gtemp += 1; } MyHelper.DeleteFile(MyHelper.ListFileDelete); start += 10; } }