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

📄 photoinfo.cs

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


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using TXML;

namespace VirtualPhotoOrganizer.Photo
{
	/// <summary>
	/// Grants the easy access to a photo's properties
	/// </summary>
	internal class PhotoInfo
	{
		// the photo's properties
		private string _PhotoPath;
		private string _PhotoName;
		private string _Thumbnail;
		private string _PhotoTitle;
		private string _Description;
		private string _TimeTaken;
		private string _ImageType;
		private float _FileSize = 0;
		private string _FileSizeStr;
		private int _ImageWidth = 0;
		private int _ImageHeight = 0;
		private float _HorizontalResolution = 0;
		private float _VerticalResolution = 0;
		//private short _BitDeph = 0;

		/// <summary>
		/// ctor used when reading image data
		/// </summary>
		public PhotoInfo(Album album, int photoIndex) {
			LoadPhoto(album, photoIndex);
		}

		/// <summary>
		/// Loads the specified photo's data into the vars
		/// </summary>
		public void LoadPhoto(Album album, int photoIndex) {
			// reset a few vars to their default values
			_ImageWidth = 0;
			_ImageHeight = 0;
			_HorizontalResolution = 0;
			_VerticalResolution = 0;

			FileInfo fi = new FileInfo(album.Photos[photoIndex].Path);

			// fill the property vars
			_PhotoPath = album.Photos[photoIndex].Path;
			_PhotoName = fi.Name;
			_Thumbnail = album.Photos[photoIndex].Thumbnail;
			_PhotoTitle = album.Photos[photoIndex].Title;
			_Description = album.Photos[photoIndex].Description;
			_TimeTaken = album.Photos[photoIndex].TimeTaken;
			_ImageType = GetImageType(fi);
			_FileSize = fi.Length;
			_FileSizeStr = GetFileSizeString(_FileSize);
		}

		#region Image property accessors

		public string PhotoPath {
			get { return _PhotoPath; }
		}
		public string PhotoName {
			get { return _PhotoName; }
		}
		public string Thumbnail {
			get { return _Thumbnail; }
		}
		public string PhotoTitle {
			get { return _PhotoTitle; }
		}
		public string Description {
			get { return _Description; }
		}
		public string TimeTaken {
			get { return _TimeTaken; }
		}
		public string ImageType {
			get { return _ImageType; }
		}
		public float FileSize {
			get { return _FileSize; }
		}
		public string FileSizeStr {
			get { return _FileSizeStr; }
		}
		public int ImageWidth {
			get {
				if (_ImageWidth == 0)
					SetBitmapData();
				return _ImageWidth;
			}
		}
		public int ImageHeight {
			get {
				if (_ImageHeight == 0)
					SetBitmapData();
				return _ImageHeight;
			}
		}
		public float HorizontalResolution {
			get {
				if (_HorizontalResolution == 0)
					SetBitmapData();
				return _HorizontalResolution;
			}
		}
		public float VerticalResolution {
			get {
				if (_VerticalResolution == 0)
					SetBitmapData();
				return _VerticalResolution;
			}
		}
		/*public short BitDeph
		{
			get
			{
				if(_BitDeph == 0)
					SetBitmapData();
				return _BitDeph;
			}
		}*/

		#endregion

		#region helper-methods

		private string GetImageType(FileInfo fi) {
			switch (fi.Extension.ToLower()) {
				case ".jpg": return "JPEG-Compliant (JPG)";
				case ".tif": return "Tagged Image File Format (TIF)";
				case ".png": return "Portable Network Graphics (PNG)";
				case ".bmp": return "Windows Bitmap (BMP)";
				case ".gif": return "CompuServe Graphics Interchange (GIF)";
				case ".jpeg": return "JPEG-Compliant (JPEG)";
				case ".tiff": return "Tagged Image File Format (TIFF)";
				default: return "Unknown";
			}
		}

		private string GetFileSizeString(float size) {
			const int kilo = 1024;
			const int mega = 1048576;
			float convertedSize = 0;
			string returnValue = "";

			if (size >= mega)	// is the size greater than a mb
			{
				convertedSize = (float) Math.Round(size / mega, 2);
				returnValue = convertedSize.ToString() + " MB";
			} else {
				if (size >= kilo)	// is the size greater than a kb
				{
					convertedSize = (float) Math.Round(size / kilo, 2);
					returnValue = convertedSize.ToString() + " KB";
				} else
					returnValue = size.ToString() + " B";	// return the size in bytes
			}

			return returnValue;
		}

		/// <summary>
		/// fills the property vars that rely on a bitmap
		/// </summary>
		private void SetBitmapData() {
			Bitmap b = new Bitmap(Bitmap.FromFile(_PhotoPath));

			_ImageWidth = b.Width;
			_ImageHeight = b.Height;
			_HorizontalResolution = b.HorizontalResolution;
			_VerticalResolution = b.VerticalResolution;

			b.Dispose();
		}

		/// <summary>
		/// Removes the filename from a given path and returns only the directories
		/// </summary>
		private 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 "";
		}

		#endregion
	}
}

⌨️ 快捷键说明

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