public void FindAndDownloadMatchingImages(string searchString, string rootFolder, string prefix, int maxImagesToDownload) { // How many images> BingImageSearchResponse initResults = GetImages(searchString, 1, 0).Result; int estTotalImages = initResults.TotalEstimatedMatches; var msg = String.Format("Est. #Results=" + estTotalImages); int numResultsAtATime = 100; bool keepGettingImages = true; var offSet = 0; var imageFoundCount = 0; var downloadCount = 0; while (keepGettingImages) { BingImageSearchResponse searchResults = GetImages(searchString, numResultsAtATime, offSet).Result; offSet += numResultsAtATime; imageFoundCount += numResultsAtATime; InformationEvent(this, new InformationEventArgs(msg)); var destinationFolder = GetDestinationFolder(rootFolder, searchString); var values = searchResults.Value; foreach (ImageInfo info in values) { downloadCount++; if (downloadCount > maxImagesToDownload) { keepGettingImages = false; break; } var sourceUrl = info.ContentUrl; string fileName = DestinationFileName(info, prefix, destinationFolder, downloadCount); var fdleArgs = new FileDownloadingEventArgs(searchString, sourceUrl, fileName, downloadCount); FileDownloading(this, fdleArgs); DownloadFile(sourceUrl, fileName); } if (imageFoundCount > estTotalImages) { keepGettingImages = false; } } var summaryMsg = String.Format( "Est. #Results={0}\n#Results returned={1}\n", estTotalImages, downloadCount); InformationEvent(this, new InformationEventArgs(summaryMsg)); }
public void DownLoadFiles(ImageInfo[] imageInfos, int start, int maxDownloads, string destinationFolder, string prefix, string searchString) { var count = 0; foreach (ImageInfo info in imageInfos) { count++; var sourceUrl = info.ContentUrl; //var destinationFolder = @"c:\test\Cars\"; string fileName = DestinationFileName(info, prefix, destinationFolder, count); var fdleArgs = new FileDownloadingEventArgs(searchString, sourceUrl, fileName, count); FileDownloading(this, fdleArgs); DownloadFile(sourceUrl, fileName); } }