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

📄 albumcreator.cs

📁 这是一个小型的相片管理器
💻 CS
字号:


using System;
using System.Collections;
using System.IO;
using System.Windows.Forms;
using VirtualPhotoOrganizer.Photo;
using VirtualPhotoOrganizer.Dialogs;
using TXML;

namespace VirtualPhotoOrganizer.Util
{
	/// <summary>
	/// Used for creating VPO Albums from an array of files, while automatically adding the album path to the root album
	/// </summary>
	internal class AlbumCreator
	{
		// the updateProgress events
		public delegate void MaxUpdate(int newMax);
		public event MaxUpdate UpdateMax;
		public delegate void UpdateProgress(int progress, string message);
		public event UpdateProgress ProgressUpdate;
		public delegate void Finish();
		public event Finish ProcessDone;

		// the strings describing the actions
		private string LsCopy;
		private string LsThumb;
		private string LsDeleting;
		private string LsExists;
		private string LsOverwrite;

		// indicates if we have to cancel
		private bool Cancel;

		// used for dealing with duplicate files
		private PhotoExistsDialog PExDlg;
		private bool PermUseThisAction = false;

		public AlbumCreator() {
			LoadStrings();
			Cancel = false;
		}

		/// <summary>
		/// creates a new album with the specified photos
		/// </summary>
		public void CreateNewAlbum(string name, string destPath, Photos photos, bool deleteOriginals) {
			// create the necessarry objects and dirs
			Album album = new Album(name, destPath);

			// add photos and thumbnails to the album
			if (photos.Count != 0)
				AddPhotosToAlbum(album, photos, deleteOriginals);

			// add the album to the root album
			RootAlbum rAlbum = new RootAlbum();
			rAlbum.Albums.Add(album);
			rAlbum.SaveRootAlbum();
		}

		/// <summary>
		/// adds photos to an existing album
		/// </summary>
		public void AddPhotosToAlbum(Album album, Photos photos, bool deleteOriginals) {
			// create the necessarry objects and dirs
			string destPath = GetDirectoryFromFilePath(album.AlbumPath);
			string thumbsDir = destPath + "\\Thumbnails\\";
			string pName = "";
			int i = 0;	// the progress indicator
			Photos addedPhotos = new Photos();
			ArrayList doNotDel = new ArrayList();
			if (!Directory.Exists(thumbsDir))
				Directory.CreateDirectory(thumbsDir);

			// the default photoExistsAction
			DialogResult drPEx = new DialogResult();
			PermUseThisAction = false;

			// create and show the progress dialog
			Dialogs.ProgressDialog pd = new VirtualPhotoOrganizer.Dialogs.ProgressDialog(this);
			pd.CancelProcess += new VirtualPhotoOrganizer.Dialogs.ProgressDialog.Cancel(pd_CancelProcess);
			pd.Show();

			#region copying
			// copy each photo to the destDir, add it to the album
			UpdateMax(photos.Count);
			FileInfo fi;
			foreach (Photo.Photo p in photos) {
				// create the fileInfo
				fi = new FileInfo(p.Path);
				pName = GetFileNameFromPath(p.Path);
				if (fi.Directory.FullName != destPath)	// only copy the image if the srcPath and the destPath are not the same
				{
					// do the file operations
					if (!File.Exists(destPath + '\\' + pName))	// check if the photo exists
					{
						File.Copy(p.Path, destPath + '\\' + pName, true);
						album.Photos.Add(new Photo.Photo(destPath + '\\' + pName, thumbsDir + "th_" + pName, p.Title, p.Description, fi.LastWriteTime.ToString()));
						addedPhotos.Add(album.Photos[album.Photos.Count - 1]);
					} else {
						// initialize and show the photoExistsDialog
						if (PermUseThisAction == false) {
							if (PExDlg == null)
								PExDlg = new PhotoExistsDialog();

							// show the dialog
							PExDlg.CheckedUseThis += new VirtualPhotoOrganizer.Dialogs.PhotoExistsDialog.UseThisChecked(PExDlg_CheckedUseThis);
							PExDlg.PhotoPath = destPath + '\\' + pName;
							drPEx = PExDlg.ShowDialog(pd);
							PExDlg.CheckedUseThis -= new VirtualPhotoOrganizer.Dialogs.PhotoExistsDialog.UseThisChecked(PExDlg_CheckedUseThis);
						}

						// respond according to user choice
						switch (drPEx) {
							case DialogResult.Yes:	// the user wishes to overwrite the file
								DeletePhotoFromAlbum(album, destPath + '\\' + pName);
								File.Copy(p.Path, destPath + '\\' + pName, true);
								album.Photos.Add(new Photo.Photo(destPath + '\\' + pName, thumbsDir + "th_" + pName, p.Title, p.Description, fi.LastWriteTime.ToString()));
								addedPhotos.Add(album.Photos[album.Photos.Count - 1]);
								break;
							case DialogResult.No:	// the user does not wish to overwrite (remove the current photo from the list)
								doNotDel.Add(i);
								break;
							case DialogResult.Retry:	// the user wants the program to automatically rename the new file, so that it can be added  to the album
								int j = 0;
								while (File.Exists(destPath + '\\' + pName)) {
									j++;
									pName = fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length) + '_' + j.ToString() + fi.Extension;
								}
								File.Copy(p.Path, destPath + '\\' + pName, true);
								album.Photos.Add(new Photo.Photo(destPath + '\\' + pName, thumbsDir + "th_" + pName, p.Title + '_' + j.ToString(), p.Description, fi.LastWriteTime.ToString()));
								addedPhotos.Add(album.Photos[album.Photos.Count - 1]);
								break;
						}
					}
				} else	// if the destPath and the srcPath are the same, just add the photo to the album and add it to the dontDel list
				{
					bool addPhoto = true;
					foreach (Photo.Photo ph in album.Photos) {
						if (ph.Path == p.Path) {
							addPhoto = false;
							break;
						}
					}
					if (addPhoto == true) {
						album.Photos.Add(new Photo.Photo(destPath + '\\' + pName, thumbsDir + "th_" + pName, p.Title, p.Description, fi.LastWriteTime.ToString()));
						addedPhotos.Add(album.Photos[album.Photos.Count - 1]);
					}
					doNotDel.Add(i);
				}

				// update the progress dialog
				i++;
				ProgressUpdate(i, LsCopy);
				if (Cancel == true)	// did the user click cancel?
				{
					FinishPhotoAdd(pd, album);
					return;
				}
			}
			#endregion

			#region creating thumbnails
			// create a thumbnail of each photo
			Resizer resizer = new Resizer();
			i = 0;
			foreach (Photo.Photo p in addedPhotos) {
				// do the file operations
				resizer.CreateThumbnail(p.Path, p.Thumbnail);

				// update the progress dialog
				i++;
				ProgressUpdate(i, LsThumb);
				if (Cancel == true)	// did the user click cancel?
				{
					FinishPhotoAdd(pd, album);
					return;
				}
			}
			#endregion

			#region deleting originals
			// delete the originals if requested
			bool dontDel = false;
			if (deleteOriginals == true) {
				i = 0;
				foreach (Photo.Photo p in photos) {
					// check if the photo was selected to not be copied
					for (int x = 0; x < doNotDel.Count; x++) {
						if ((int) doNotDel[x] == i) {
							dontDel = true;
							break;
						} else
							dontDel = false;
					}
					// delete the file
					if (dontDel == false) {
						try {
							File.Delete(p.Path);
						}
						catch (Exception e) {
							MessageBox.Show(e.Message);
						}
					}



					// update the progress dialog
					i++;
					ProgressUpdate(i, LsDeleting);
					if (Cancel == true)	// did the user click cancel?
					{
						FinishPhotoAdd(pd, album);
						return;
					}
				}
			}
			#endregion

			FinishPhotoAdd(pd, album);
		}

		private void FinishPhotoAdd(Dialogs.ProgressDialog pd, Album album) {
			// save the album
			album.SaveAlbum();

			// close the progress dialog
			ProcessDone();

			// hook out of cancel event
			pd.CancelProcess -= new VirtualPhotoOrganizer.Dialogs.ProgressDialog.Cancel(pd_CancelProcess);

			// reset Cancel
			Cancel = false;
		}

		#region helper methods

		/// <summary>
		/// loads the language strings
		/// </summary>
		private void LoadStrings() {
			try {
				// load strings from file
				TXmlReader reader = XmlHandler.OpenLangFile();

				LsCopy = reader.GetString("Progress", "Copy", "Copying Photos...");
				LsThumb = reader.GetString("Progress", "Thumb", "Creating Thumbnails...");
				LsDeleting = reader.GetString("Progress", "Deleting", "Deleting Originals...");
				LsExists = reader.GetString("Progress", "Exists", "The following picture already exists in the destination path:");
				LsOverwrite = reader.GetString("Progress", "Overwrite", "Overwrite?");

				reader.Close();
			}
			catch {
				// use defaults
				LsCopy = "Copying Photos...";
				LsThumb = "Creating Thumbnails...";
				LsDeleting = "Deleting Originals...";
				LsExists = "The following picture already exists in the destination path:";
				LsOverwrite = "Overwrite?";
			}
		}

		/// <summary>
		/// Removes the directories from a given path and returnes only the file name
		/// </summary>
		private string GetFileNameFromPath(string path) {
			FileInfo fi = new FileInfo(path);
			return fi.Name;
		}

		/// <summary>
		/// Removes the filename from a given path and returns only the directories
		/// </summary>
		public static string GetDirectoryFromFilePath(string path) {
			int i = 0;
			for (i = path.Length - 1; i > 0; i--) {
				if (path[i] == '\\')
					break;
			}

			if (i != 0)
				return path.Substring(0, i);
			else
				return Directory.GetCurrentDirectory();
		}

		/// <summary>
		/// removes the specified photo from the specified album
		/// </summary>
		private void DeletePhotoFromAlbum(Album a, string photoPath) {
			int i = 0;
			foreach (Photo.Photo p in a.Photos) {
				if (p.Path == photoPath) {
					a.Photos.RemoveAt(i);
					break;
				}
				i++;
			}
		}


		#endregion

		private void pd_CancelProcess() {
			Cancel = true;
		}

		private void PExDlg_CheckedUseThis(bool isChecked) {
			PermUseThisAction = isChecked;
		}
	}
}

⌨️ 快捷键说明

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