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

📄 album.cs

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


using System;
using System.IO;
using TXML;

namespace VirtualPhotoOrganizer.Photo
{
	/// <summary>
	/// used for album file access
	/// </summary>
	internal class Album
	{
		private Albums _SubAlbums;	// the subalbums
		private Photos _Photos;	// our Photo collection
		private string _AlbumName;
		private string _AlbumPath;
		private string AlbumDir;
		// the node names
		private const string ALBUM_NAME_N = "AlbumName";
		private const string PHOTO = "Photo";
		private const string SUBALBUM = "SubAlbum";
		private const string PATH = "Path";
		private const string THUMB = "Thumbnail";
		private const string TITLE = "Title";
		private const string DESCRIP = "Description";
		private const string TIMETAKEN = "TimeTaken";

		/// <summary>
		/// the ctor used for creating a new album file
		/// </summary>
		public Album(string albumName, string albumPath) {
			_Photos = new Photos();
			_SubAlbums = new Albums();
			_AlbumName = albumName;
			_AlbumPath = albumPath;
			AlbumDir = GetAlbumDir(_AlbumPath);
		}

		/// <summary>
		/// the ctor used for opening an existing album file
		/// </summary>
		public Album(string albumPath) {
			_Photos = new Photos();
			_SubAlbums = new Albums();
			_AlbumPath = albumPath;
			AlbumDir = GetAlbumDir(_AlbumPath);
			LoadAlbumData();
		}

		private void LoadAlbumData() {
			TXmlReader reader = new TXmlReader(_AlbumPath);

			_AlbumName = reader.GetString(ALBUM_NAME_N, _AlbumName);

			string[] nodes = reader.GetNodes();

			// the temp property vars
			string path;
			string thumb;
			string title;
			string descrip;
			string timeTaken;

			int ip = 0;	// serves as photo index
			int ia = 0; // serves as subalbum index

			// retrieve all photos' properties
			foreach (string s in nodes) {
				switch (s) {
					case PHOTO:
						ip++;
						try {
							path = AlbumDir + reader.GetString(PHOTO, ip, PATH, 1, "");
							thumb = AlbumDir + reader.GetString(PHOTO, ip, THUMB, 1, "");
							title = reader.GetString(PHOTO, ip, TITLE, 1, "");
							descrip = reader.GetString(PHOTO, ip, DESCRIP, 1, "");
							timeTaken = reader.GetString(PHOTO, ip, TIMETAKEN, 1, "");
							_Photos.Add(new Photo(path, thumb, title, descrip, timeTaken));
						}
						catch { }
						break;
					case SUBALBUM:
						ia++;
						try {
							path = reader.GetString(SUBALBUM, ia, "");
							_SubAlbums.Add(new Album(path));
						}
						catch { }
						break;
					default:
						break;
				}

			}

			reader.Close();
		}

		public void SaveAlbum() {
			// if the file already exists delete it
			if (File.Exists(_AlbumPath))
				File.Delete(_AlbumPath);

			TXmlWriter writer = new TXmlWriter(_AlbumPath, "VPO_Album", 4);

			writer.WriteString("VPO_Album", ALBUM_NAME_N, _AlbumName);

			// save each subalbum's path to file
			int i = 1;
			foreach (Album a in _SubAlbums)
				writer.WriteString("VPO_ALBUM", 1, SUBALBUM, i++, a.AlbumPath);

			// save each photo's data to file
			i = 1;
			foreach (Photo p in _Photos) {
				writer.AddEmptyNode(PHOTO);
				writer.WriteString(PHOTO, i, PATH, 1, GetPhotoName(p.Path));
				writer.WriteString(PHOTO, i, THUMB, 1, "Thumbnails\\" + GetPhotoName(p.Thumbnail));
				writer.WriteString(PHOTO, i, TITLE, 1, p.Title);
				writer.WriteString(PHOTO, i, DESCRIP, 1, p.Description);
				writer.WriteString(PHOTO, i, TIMETAKEN, 1, p.TimeTaken);
				i++;
			}

			writer.Close();
		}

		/// <summary>
		/// removes the specified element
		/// </summary>
		public void DeletePhoto(int index) {
			_Photos.RemoveAt(index);
		}

		/// <summary>
		/// returns the album's root dir
		/// </summary>
		private string GetAlbumDir(string albumPath) {
			FileInfo fi = new FileInfo(albumPath);
			return fi.Directory.FullName + '\\';
		}

		/// <summary>
		/// returns the file name of the photo
		/// </summary>
		private string GetPhotoName(string path) {
			FileInfo fi = new FileInfo(path);
			return fi.Name;
		}

		#region accessors

		public string AlbumPath {
			get { return _AlbumPath; }
			set {
				_AlbumPath = value;
				AlbumDir = GetAlbumDir(_AlbumPath);
			}
		}
		public Albums SubAlbums {
			get { return _SubAlbums; }
		}
		public string AlbumName {
			get { return _AlbumName; }
			set { _AlbumName = value; }
		}
		public Photos Photos {
			get { return _Photos; }
		}

		#endregion
	}
}

⌨️ 快捷键说明

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