public MainWindow() { InitializeComponent(); ImageDataList.Images.LoadDatabase(); Thumbnails.LoadThumbs(); ImageDataList.Images.OnChange += ImageList_OnChange; }
private void menuItemSaveDB_Click(object sender, RoutedEventArgs e) { ImageDataList.Images.SaveDatabase(); Thumbnails.SaveThumbs(); }
/// <summary> /// Adds an image to the control's image list /// </summary> /// <param name="img">The image to be added</param> /// <param name="arrange">If true, the ArrangePics method is called</param> /// <returns>Returns true if the image was successfully added</returns> public bool AddImage(ImageData img, bool arrange = true) { if (this.images.ContainsKey(img) || !File.Exists(img.path)) { return(false); } Image pic = new Image(); pic.Stretch = System.Windows.Media.Stretch.Uniform; pic.HorizontalAlignment = HorizontalAlignment.Left; pic.VerticalAlignment = VerticalAlignment.Top; pic.Height = this._thumbSize.Height; pic.Width = this._thumbSize.Width; pic.Tag = img.path; StringBuilder tags = new StringBuilder(); foreach (string tag in img.tags) { if (tags.Length > 0) { tags.Append(", "); } tags.Append(tag); } pic.ToolTip = String.Format("Path: {0}\nDimensions: {1}x{2}\nSize: {3} MB\nTags: {4}", img.path, img.width, img.height, (double)img.size / 1024.0 / 1024.0, tags.ToString()); pic.MouseLeftButtonDown += Pic_MouseLeftButtonDown; ContextMenu cMenu = new ContextMenu(); MenuItem itemEdit = new MenuItem(), itemDelete = new MenuItem(); itemEdit.Header = "Edit"; itemEdit.Click += Pic_Edit; itemEdit.Tag = img.path; itemDelete.Header = "Delete"; itemDelete.Click += Pic_Delete; itemDelete.Tag = img.path; cMenu.Items.Add(itemEdit); cMenu.Items.Add(itemDelete); pic.ContextMenu = cMenu; string thumbnail; if (img.thumbnailMD5 == null || !File.Exists(thumbnail = Thumbnails.GetThumbnailPath(img.thumbnailMD5))) { string md5 = Thumbnails.KeyFromPath(img.path); if (md5 == null || !File.Exists(thumbnail = Thumbnails.GetThumbnailPath(md5))) { thumbnail = Thumbnails.GetThumbnailPath(Thumbnails.Add(img.path)); } } BitmapImage image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.UriSource = new Uri(Path.GetFullPath(thumbnail)); image.EndInit(); pic.Source = image; this.images.Add(img, pic); this.panel.Children.Add(pic); if (arrange) { ArrangePics(); } return(true); }
private void buttonAdd_Click(object sender, RoutedEventArgs e) { if (String.IsNullOrWhiteSpace(textBoxPath.Text)) { MessageBox.Show("You must select an image to add.", "No image path", MessageBoxButton.OK, MessageBoxImage.Error); return; } else if (!File.Exists(textBoxPath.Text)) { MessageBox.Show("The specified file does not exist.", "Invalid path", MessageBoxButton.OK, MessageBoxImage.Error); return; } ImageData img = new ImageData(); img.path = textBoxPath.Text; using (var stream = new FileStream(img.path, FileMode.Open)) { try { using (var bmp = System.Drawing.Image.FromStream(stream)) { img.width = bmp.Width; img.height = bmp.Height; } } catch (ArgumentException ex) { MessageBox.Show("The specified file is not an image.", "Invalid file", MessageBoxButton.OK, MessageBoxImage.Error); return; } stream.Seek(0, SeekOrigin.Begin); using (var hasher = new System.Security.Cryptography.SHA1Cng()) { img.sha1Hash = BitConverter.ToString(hasher.ComputeHash(stream)).Replace("-", ""); hasher.Clear(); } stream.Seek(0, SeekOrigin.Begin); img.thumbnailMD5 = Thumbnails.Add(stream, img.path); } if (ImgData == null) { ImageData otherImg = null; for (int i = 0; i < ImageDataList.Images.Count; i++) { otherImg = ImageDataList.Images [i]; if (otherImg == null) // Wat? { continue; } if (string.Equals(otherImg.path, img.path, StringComparison.CurrentCultureIgnoreCase)) { MessageBox.Show("There is already an entry with the specified path in the database.", "Duplicate image", MessageBoxButton.OK, MessageBoxImage.Error); return; } else if (string.Equals(otherImg.sha1Hash, img.sha1Hash, StringComparison.CurrentCultureIgnoreCase)) { MessageBox.Show("The specified image matches the SHA1 hash of another entry in the database.", "Duplicate image", MessageBoxButton.OK, MessageBoxImage.Error); return; } } } img.description = textBoxDescription.Text; img.size = (new FileInfo(img.path)).Length; List <string> tags = new List <string> (), sources = new List <string> (); foreach (string tag in this.listBoxTags.Items) { if (tag.StartsWith("source:")) // If the tag starts with "source:", { sources.Add(tag.Remove(0, 7)); // Remove the "source:" from the tag and add it to the sources list } else // If not, { tags.Add(tag); // Add it to the tags list } } img.sources = sources.ToArray(); sources.Clear(); sources = null; img.tags = tags.ToArray(); tags.Clear(); tags = null; if (ImgData != null) { for (int i = 0; i < ImageDataList.Images.Count; i++) { if (ImageDataList.Images [i] == this.ImgData) { ImageDataList.Images [i] = img; } } } else { ImageDataList.Images.Add(img); } this.Close(); }