⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 photospane.cs

📁 这是一个小型的相片管理器
💻 CS
📖 第 1 页 / 共 4 页
字号:
						_Album.Photos[start] = temp;
					}
					start++;
				}
			}

			// run the entire loop again and call this method if the photos still need arranging
			for (int i = 0; i < _Album.Photos.Count; i++) {
				try { timeI = DateTime.Parse(_Album.Photos[i].TimeTaken); }
				catch {
					MessageBox.Show(LsArrChroErr, _Album.Photos[i].Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
					_Album.SaveAlbum();
					ReloadAlbum();
					return;
				}

				start = i + 1;
				while (start < _Album.Photos.Count) {
					try { timeStart = DateTime.Parse(_Album.Photos[start].TimeTaken); }
					catch {
						MessageBox.Show(LsArrChroErr, _Album.Photos[start].Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
						_Album.SaveAlbum();
						ReloadAlbum();
						return;
					}

					w = timeI.CompareTo(timeStart);

					if (w > 0) {
						RearrangePhotosChro();
						return;
					}
					start++;
				}
			}

			// update the il
			ImageCollection icTemp = new ImageCollection();
			foreach (Photo.Photo p in _Album.Photos)
				icTemp.Add((Image) p.Tag);
			ilThumbs.Images.Clear();
			foreach (Image i in icTemp)
				ilThumbs.Images.Add(i);

			// save the album and update the lv
			_Album.SaveAlbum();
			ReloadAlbum();
		}

		#endregion

		#region AlbumDeleted

		/// <summary>
		/// to be called when the currently loaded album was deleted
		/// clears the album, the lv, the il, and the number of available photos
		/// </summary>
		public void AlbumDeleted() {
			_Album = null;
			lvPhotos.Items.Clear();
			ilThumbs.Images.Clear();
			this.paneTitle.Text = "";
			AlbumLoaded(0);
		}

		#endregion

		#region AddPhotos

		/// <summary>
		/// adds photos that just been added to the album to the listView
		/// </summary>
		public void AddPhotos() {
			int max = _Album.Photos.Count - this.lvPhotos.Items.Count;	// needed for the OnProgressUpdate event

			for (int i = lvPhotos.Items.Count - 1; i < _Album.Photos.Count; i++) {
				// add each new photo to the il and to the lv
				Image img = Bitmap.FromFile(_Album.Photos[i].Thumbnail);
				ilThumbs.Images.Add(img);
				lvPhotos.Items.Add(_Album.Photos[i].Title, i);
				img.Dispose();

				// update the progress information
				OnProgressUpdate(i - max, max, LsLThumb);
			}

			// reset the status bar to its initial state
			OnProgressUpdate(0, 100, "");

			// fire the albumLoaded event
			AlbumLoaded(lvPhotos.Items.Count);
		}

		#endregion

		#region SaveAlbum

		/// <summary>
		/// Saves the currently open album
		/// </summary>
		public DialogResult SaveAlbum(MessageBoxButtons buttons) {
			if (Album != null) {
				try		// try to save the album
				{
					_Album.SaveAlbum();
					return DialogResult.Ignore;		// if everything went fine, return Ignore, since the handling for Ignore and OK is the same
				}
				catch {
					return MessageBox.Show(LsNoAlbumSave, _Album.AlbumPath, buttons, MessageBoxIcon.Error);
				}
			} else
				return DialogResult.Ignore;
		}

		/// <summary>
		/// attempts to save the current album and reacts accordingly if it can't be saved
		/// only to be used from lvPhotos' event AfterLabelEdit
		/// </summary>
		private void AttemptSave(System.Windows.Forms.LabelEditEventArgs e) {
			DialogResult dr = SaveAlbum(MessageBoxButtons.RetryCancel);		// attempt to save the album
			switch (dr) {
				case DialogResult.Cancel: // cancel the edit operation and revert the photo title
				{
						e.CancelEdit = true;
						_Album.Photos[e.Item].Title = lvPhotos.Items[e.Item].Text;
					}
					break;
				case DialogResult.Ignore:	// saving went fine, no need to do anything
					break;
				case DialogResult.Retry: AttemptSave(e);
					break;
				default:
					break;
			}
		}

		/// <summary>
		/// attempts to save the current album and reacts accordingly if it can't be saved
		/// </summary>
		private bool AttemptSave() {
			DialogResult dr = SaveAlbum(MessageBoxButtons.RetryCancel);		// attempt to save the album
			switch (dr) {
				case DialogResult.Cancel: return false;
				case DialogResult.Ignore: return true;	// saving went fine
				case DialogResult.Retry: AttemptSave();
					break;
				default: return true;
			}
			return true;
		}

		#endregion

		#region RecreateThumbnail

		/// <summary>
		/// Recreates the thumbnail of the specified image index
		/// </summary>
		public void RecreateThumbnail(int index) {
			Resizer r = new Resizer();
			r.CreateThumbnail(_Album.Photos[index].Path, _Album.Photos[index].Thumbnail);
			Image img = Bitmap.FromFile(_Album.Photos[index].Thumbnail);
			ilThumbs.Images[index] = new Bitmap(img);
			img.Dispose();

			// refresh the Photos lv
			lvPhotos.Refresh();
		}

		/// <summary>
		/// Recreates the thumbnail of the specified image index, after checking if the photo is actually a member of this album
		/// </summary>
		public void RecreateThumbnail(int index, Photo.Photo p) {
			try {
				if (p == _Album.Photos[index]) {
					Resizer r = new Resizer();
					r.CreateThumbnail(_Album.Photos[index].Path, _Album.Photos[index].Thumbnail);
					Image img = Bitmap.FromFile(_Album.Photos[index].Thumbnail);
					ilThumbs.Images[index] = new Bitmap(img);
					img.Dispose();

					// refresh the Photos lv
					lvPhotos.Refresh();
				}
			}
			catch { }
		}

		#endregion

		#region OpenPhotoEditor

		/// <summary>
		/// opens the photo editor for the currently selected image
		/// </summary>
		public void OpenPhotoEditor() {
			try {
				PhotoEditor pe = new PhotoEditor(_Album.Photos, lvPhotos.SelectedIndices[0], this);
				pe.Show();
			}
			catch { }
		}

		#endregion

		#region UpdateImageInfo

		/// <summary>
		/// updates the specified photo's title
		/// </summary>
		public void UpdateImageInformation(int index, Photo.Photo p) {
			try {
				if (p == _Album.Photos[index])
					lvPhotos.Items[index].Text = p.Title;
			}
			catch { }
		}

		#endregion

		#region StartSlideShow

		/// <summary>
		/// Initiates a slideShow
		/// </summary>
		public void StartSlideShow() {
			SlideShow slideSh;	// create the slideShow object

			// check how many photos are selected and take the appropriate action
			switch (SelPhotos.Count) {
				case 0: slideSh = new SlideShow(_Album.Photos, 0);
					break;
				case 1: slideSh = new SlideShow(_Album.Photos, _Album.Photos.IndexOf(SelPhotos[0]));
					break;
				default:
					ListViewItem item = lvPhotos.GetItemAt(MousePos.X, MousePos.Y);
					int index;
					if (item != null) {
						index = item.Index;
						if (index != 0) {
							int temp = index;
							for (int i = 0; i < index; i++) {
								if (lvPhotos.Items[i].Selected == false)
									temp--;
							}
							index = temp;
						}
					} else
						index = 0;
					slideSh = new SlideShow(SelPhotos, index);
					break;
			}

			// start the show
			slideSh.Show();
		}

		#endregion

		#region accessors

		public Album Album {
			get { return _Album; }
			set {
				_Album = value;
				ParseAlbum();
			}
		}
		public Photos SelectedPhotos {
			get { return SelPhotos; }
		}
		public int PhotoSaverQuality {
			get { return PSaverQ; }
			set { PSaverQ = value; }
		}

		#endregion

		#region Rotation methods

		/// <summary>
		/// Roates the selected image to the left
		/// </summary>
		public void RotateLeft() {
			for (int i = 0; i < SelPhotos.Count; i++) {
				RotateLeft(SelPhotos[i]);

				// recreate thumbnail
				RecreateThumbnail(lvPhotos.Items[lvPhotos.SelectedIndices[i]].ImageIndex);
				OnProgressUpdate(i, SelPhotos.Count, LsRot);
				Application.DoEvents();
			}
			OnProgressUpdate(0, 100, "");

			// refresh the Photos lv
			lvPhotos.Refresh();
		}

		/// <summary>
		/// Rotates the selected image to the right
		/// </summary>
		public void RotateRight() {
			for (int i = 0; i < SelPhotos.Count; i++) {
				RotateRight(SelPhotos[i]);

				// recreate thumbnail
				RecreateThumbnail(lvPhotos.Items[lvPhotos.SelectedIndices[i]].ImageIndex);
				OnProgressUpdate(i, SelPhotos.Count, LsRot);
				Application.DoEvents();
			}
			OnProgressUpdate(0, 100, "");

			// refresh the Photos lv
			lvPhotos.Refresh();
		}

		/// <summary>
		/// Rotates the specified image to the right (used from within the PhotoEditor)
		/// </summary>
		private void RotateRight(Photo.Photo p) {
			// rotate the image
			Resizer r = new Resizer();
			r.Rotate(p.Path, RotateFlipType.Rotate90FlipNone, PSaverQ);
		}

		/// <summary>
		/// Rotates the specified image to the left (used from within the PhotoEditor)
		/// </summary>
		private void RotateLeft(Photo.Photo p) {
			// rotate the image
			Resizer r = new Resizer();
			r.Rotate(p.Path, RotateFlipType.Rotate270FlipNone, PSaverQ);
		}

		#endregion

		#region DeletePhoto

		/// <summary>
		/// Deletes the currently selected photo(s)
		/// </summary>
		public void DeletePhoto() {
			if (MessageBox.Show(LsDeleteQ, LsDel, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
				// attempt to save the album now, to find out if there could be any problems
				if (AttemptSave() == true) {
					int index;
					foreach (Photo.Photo p in SelPhotos) {
						try {
							File.Delete(p.Path);
							File.Delete(p.Thumbnail);
						}
						catch (Exception e) {
							MessageBox.Show(e.Message);
							return;
						}
						Deleting = true;	// make sure that SelPhotos doesn't get modified
						index = _Album.Photos.IndexOf(p);
						_Album.Photos.Remove(p);
						ilThumbs.Images.RemoveAt(index);
						lvPhotos.Items.RemoveAt(index);
						if (index < lvPhotos.Items.Count - 1) {
							for (int i = index; i < lvPhotos.Items.Count; i++)
								lvPhotos.Items[i].ImageIndex--;
							lvPhotos.Items[lvPhotos.Items.Count - 1].ImageIndex++;
						}
					}

					// reset everything SelPhotos related
					Deleting = false;
					SelPhotos.Clear();
					SelectedPhotosChanged(SelPhotos);	// inform the user that SelPhotos changed

					// save the album
					_Album.SaveAlbum();

					// call the PhotosDeleted event
					PhotosDeleted(lvPhotos.Items.Count);
				}
			}
		}

		#endregion

		#region RenamePhoto

		/// <summary>
		/// Begins the renaming of the currently selected photo
		/// </summary>
		public void RenamePhoto() {
			lvPhotos.Items[lvPhotos.SelectedIndices[0]].BeginEdit();
		}

		#endregion

		#region Cut, Copy & Paste

		public void CopyToClipboard() {
			Photos clipb = new Photos();
			foreach (Photo.Photo p in SelPhotos)
				clipb.Add(_Album.Photos[_Album.Photos.IndexOf(p)]);
			if (SelPhotos.Count != 0)
				VPOClipboard.CopyToClipboard(clipb, _Album);
		}

		public void CutToClipboard() {
			Photos clipb = new Photos();
			foreach (Photo.Photo p in SelPhotos)
				clipb.Add(_Album.Photos[_Album.Photos.IndexOf(p)]);
			if (SelPhotos.Count != 0)
				VPOClipboard.CutToClipboard(clipb, _Album);
		}

		public void PasteFromClipboard() {
			if (VPOClipboard.Photos != null & VPOClipboard.Photos.Count > 0) {
				DialogResult dr = SaveAlbum(MessageBoxButtons.RetryCancel);
				switch (dr) {
					case DialogResult.Retry:
						PasteFromClipboard();
						return;
					case DialogResult.Cancel:
						return;
					default:
						break;
				}

				int index = _Album.Photos.Count;
				if (CtrlDown == false)
					index = this.GetDestIndex();
				if (VPOClipboard.ClipboardAction == VPOClipboard.Action.Copy)
					VPOClipboard.CopyToAlbum(_Album, index);
				else
					VPOClipboard.MoveToAlbum(_Album, index);

				ParseAlbum();
			}
		}

		#endregion

		#region helper methods

		/// <summary>
		/// reloads the thumbnails
		/// </summary>
		public void RefreshPhotoView() {
			this.ParseAlbum();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -