private void SearchInDatabase(Dictionary<Descriptor, object> query, Descriptor desc) { if (query != null) { try { PhotosEntities photoEntities = new PhotosEntities(); foreach (var photoId in _PhotoIdList) { bool comply = false; Photo photo = photoEntities.PhotoSet.Single(p => p.PhotoID == photoId); Gallery.ImageListViewItem ilvi = new Gallery.ImageListViewItem(photo.ImagePath); //we need to clear the filename form the text property, and set it to picture title ilvi.Text = "Photo title: " + photo.Title + Environment.NewLine; ilvi.Tag = new ImageInfo { PhotoID = photo.PhotoID }; foreach (Descriptor descriptor in Enum.GetValues(typeof(Descriptor))) { if (descriptor != Descriptor.NONE && (desc & descriptor) == descriptor) { double distance = DescriptorTools.CalculateDescriptorDistance(query[descriptor], photo.GetByIndex(descriptor.ToString()).BDeserialize(), descriptor); //TODO: distance needs to be set per descriptor if (distance >= 0) { comply = true; //string tmpFileName = TempManager.GetTempFileName(); string tmpFileName = Path.Combine(_FolderPath, photo.PhotoID + ".jpg"); ((ImageInfo)ilvi.Tag).SetDescriptorDistance(descriptor, distance); GalleryEntryBuilder(new FileInfo(tmpFileName), ref ilvi, descriptor.ToString() + ": " + distance.ToString("F")); } } } // after all descriptors touched the file it goes to the gallery if it complies with the query parameters if (comply) { ilvGallery.Items.Add(ilvi); } } } catch (UnauthorizedAccessException) { // again.. so what.. go go go! } } }
private void FlickrPhotoSearch(object searchOptions) { PhotoCollection photoCollection = _Flickr.PhotosSearch((PhotoSearchOptions)searchOptions); _PhotoIdList.Clear(); WebClient client = new WebClient(); PhotosEntities photoEntities = new PhotosEntities(); foreach (FlickrNet.Photo photo in photoCollection) { _PhotoIdList.Add(photo.PhotoId); string tempFile = Path.Combine(_FolderPath, photo.PhotoId + ".jpg"); // check if the photo was not indexed before if (photoEntities.PhotoSet.Count(p => p.PhotoID == photo.PhotoId) < 1) { if (!File.Exists(tempFile)) client.DownloadFile(photo.MediumUrl, tempFile); // insert record to db FileInfo tmpFileInfo = new FileInfo(tempFile); // it happened one and got the app crashed .. so just in case if (tmpFileInfo.Length == 0) continue; Photo dbPhotoEntry = new Photo { PhotoID = photo.PhotoId, Title = photo.Title, UrlThumbnail = photo.ThumbnailUrl, UrlMedium = photo.MediumUrl, UrlLarge = photo.LargeUrl, ImagePath = tempFile, SCD = DescriptorTools.CalculateDescriptor(tmpFileInfo, Descriptor.SCD).BSerialize(), CLD = DescriptorTools.CalculateDescriptor(tmpFileInfo, Descriptor.CLD).BSerialize(), EHD = DescriptorTools.CalculateDescriptor(tmpFileInfo, Descriptor.EHD).BSerialize(), CEDD = DescriptorTools.CalculateDescriptor(tmpFileInfo, Descriptor.CEDD).BSerialize(), FCTH = DescriptorTools.CalculateDescriptor(tmpFileInfo, Descriptor.FCTH).BSerialize() }; photoEntities.PhotoSet.AddObject(dbPhotoEntry); } else { if (!File.Exists(tempFile)) client.DownloadFile(photo.MediumUrl, tempFile); } PopulateGallery(tempFile, photo); if (!_IsIndexing) { photoEntities.SaveChanges(); return; } } if (btnSearchFlickr.InvokeRequired) { this.Invoke((Action)(() => btnSearchFlickr.Text = "Index Flickr")); } else { btnSearchFlickr.Text = "Index Flickr"; } _IsIndexing = false; photoEntities.SaveChanges(); }