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

📄 photoeditor.cs

📁 这是一个小型的相片管理器
💻 CS
📖 第 1 页 / 共 5 页
字号:


using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using VirtualPhotoOrganizer.Photo;
using VirtualPhotoOrganizer.Util;

namespace VirtualPhotoOrganizer
{
	/// <summary>
	/// Zusammenfassung f黵 PhotoEditor.
	/// </summary>
	internal class PhotoEditor : System.Windows.Forms.Form
	{
		// needed for locating our current photo
		private Photos Photos;
		private int CurrIndex;

		// indicates if we're currently resetting
		private bool Resetting = false;

		// the VPOBitmap that contains the current photo
		private VPOBitmap Image;

		// our gdi+ objects
		private SolidBrush BClear;

		// indicates if we're currently zooming
		private bool Zooming = false;

		// indicates the current zoom level
		private int ZoomLevel = 1;

		// language strings
		private string LsNumOnly;
		private string LsImgChanged;
		private string LsImgChangedMsg;
		private string LsPixels;
		private string LsNoSave;
		private string LsZFactor;
		private string LsScaled;

		// the AR vars
		private double ScaleW;
		private double ScaleH;

		// idicates if title or desc have been changed
		private bool TextChanged = false;

		// indicates if the image was changed
		private bool ImageChanged = false;

		// we need this for thumbnail management
		Controls.PhotosPane PhotosP;

		// the path we'll save the image in
		private string Path;

		// the title and description buffers
		private string PTitleBuf;
		private string PDescBuf;
		private string PTimeTaken;

		private System.Windows.Forms.Panel pControls;
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabGen;
		private System.Windows.Forms.TabPage tabProp;
		private System.Windows.Forms.TabPage tabResize;
		private System.Windows.Forms.TabPage tabCrop;
		private System.Windows.Forms.RichTextBox rtbDesc;
		private System.Windows.Forms.TextBox tbTitle;
		private System.Windows.Forms.Label lbTitle;
		private System.Windows.Forms.TrackBar tbBright;
		private System.Windows.Forms.TrackBar tbCont;
		private System.Windows.Forms.TrackBar tbGamma;
		private System.Windows.Forms.Label lbLBright;
		private System.Windows.Forms.Label lbLCont;
		private System.Windows.Forms.Label lbLGamma;
		private System.Windows.Forms.Label lbLSat;
		private System.Windows.Forms.NumericUpDown numBright;
		private System.Windows.Forms.NumericUpDown numCont;
		private System.Windows.Forms.NumericUpDown numGamma;
		private System.Windows.Forms.NumericUpDown numSat;
		private System.Windows.Forms.TrackBar tbSat;
		private System.Windows.Forms.Label lbLOrigWidth;
		private System.Windows.Forms.Label lbLOrigHeight;
		private System.Windows.Forms.Label lbWidth;
		private System.Windows.Forms.Label lbHeight;
		private System.Windows.Forms.Label lbLNewWidth;
		private System.Windows.Forms.CheckBox cbMaintainAR;
		private System.Windows.Forms.TextBox tbNWidth;
		private System.Windows.Forms.TextBox tbNHeight;
		private System.Windows.Forms.CheckBox cbChooseFilter;
		private System.Windows.Forms.Button bResize;
		private System.Windows.Forms.ComboBox cobFilt;
		private System.Windows.Forms.Panel pButtons;
		private System.Windows.Forms.Button bNext;
		private System.Windows.Forms.Button bPrev;
		private System.Windows.Forms.Button bZIn;
		private System.Windows.Forms.Button bZOut;
		private System.Windows.Forms.Button bReset;
		private System.Windows.Forms.Button bSave;
		private System.Windows.Forms.Button bRotRight;
		private System.Windows.Forms.Button bRotLeft;
		private System.Windows.Forms.Button bSaveAs;
		private System.Windows.Forms.Label lbLNewHeight;
		private System.Windows.Forms.Label lbDimensions;
		private System.Windows.Forms.Label lbLOrig;
		private System.Windows.Forms.Label lbLSel;
		private System.Windows.Forms.Label lbOrigDim;
		private System.Windows.Forms.Label lbSelDim;
		private System.Windows.Forms.Button bCrop;
		private System.Windows.Forms.HScrollBar hSb;
		private System.Windows.Forms.VScrollBar vSb;
		private System.Windows.Forms.NumericUpDown numBottom;
		private System.Windows.Forms.NumericUpDown numLeft;
		private System.Windows.Forms.NumericUpDown numRight;
		private System.Windows.Forms.NumericUpDown numTop;
		private System.Windows.Forms.Label lbTop;
		private System.Windows.Forms.Label lbBottom;
		private System.Windows.Forms.Label lbLeft;
		private System.Windows.Forms.Label lbRight;
		private System.Windows.Forms.Label lbCArea;
		private System.Windows.Forms.Label lbTime;
		private System.Windows.Forms.TextBox tbTime;

		/// <summary>
		/// Erforderliche Designervariable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public PhotoEditor(Photos photos, int index, Controls.PhotosPane pp) {
			//
			// Erforderlich f黵 die Windows Form-Designerunterst黷zung
			//
			InitializeComponent();

			PhotosP = pp;
			Photos = photos;
			CurrIndex = index;
//			LoadLanguageStrings();
			BClear = new SolidBrush(this.BackColor);
			OpenPhoto();
		}

		/// <summary>
		/// Die verwendeten Ressourcen bereinigen.
		/// </summary>
		protected override void Dispose(bool disposing) {
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region OpenPhoto

		/// <summary>
		/// used for opening a photo from the album
		/// </summary>
		private void OpenPhoto() {
			// dispose the old bitmap
			if (Image != null)
				Image.Dispose();

			// initialize a new photo and load it into the VPOBitmap
			bReset.Enabled = false;
			Image = new VPOBitmap(Photos[CurrIndex].Path);

			// initialize the AR vars
			ScaleW = (double) Image.Image.Height / (double) Image.Image.Width;
			ScaleH = (double) Image.Image.Width / (double) Image.Image.Height;

			// reset the ui
			ResetUi();

			// load the photo data
			LoadPhotoData(Photos[CurrIndex]);

			// draw the photo into the pb
			DrawPhoto();
		}

		/// <summary>
		/// used for opening a photo that is not part of an album
		/// </summary>
		private void OpenPhoto(Photo.Photo p) {
			// dispose the old bitmap
			if (Image != null)
				Image.Dispose();

			// initialize a new photo and load it into the VPOBitmap
			bReset.Enabled = false;
			Image = new VPOBitmap(p.Path);

			// initialize the AR vars
			ScaleW = (double) Image.Image.Height / (double) Image.Image.Width;
			ScaleH = (double) Image.Image.Width / (double) Image.Image.Height;

			// reset the ui
			ResetUi();

			// load the photo data
			LoadPhotoData(p);

			// draw the photo into the pb
			DrawPhoto();

			bReset.Enabled = false;
		}

		#endregion

		#region LoadPhotoData

		private void LoadPhotoData(Photo.Photo p) {
			Resetting = true;

			// load the text information
			this.Text = p.Path;
			tbTitle.Text = p.Title;
			rtbDesc.Text = p.Description;
			tbTime.Text = p.TimeTaken;
			PTitleBuf = p.Title;
			PDescBuf = p.Description;
			PTimeTaken = p.TimeTaken;
			Path = p.Path;

			// load the resize info
			lbWidth.Text = Image.Image.Width.ToString();
			lbHeight.Text = Image.Image.Height.ToString();
			tbNWidth.Text = Image.Image.Width.ToString();
			tbNHeight.Text = Image.Image.Height.ToString();

			// load the cropping info
			lbOrigDim.Text = Image.Image.Width.ToString() + " x " + Image.Image.Height + ' ' + LsPixels;
			lbSelDim.Text = lbOrigDim.Text;
			numTop.Maximum = Image.Image.Height;
			numBottom.Maximum = Image.Image.Height;
			numLeft.Maximum = Image.Image.Width;
			numRight.Maximum = Image.Image.Width;

			Resetting = false;

			// adjust the scrollbars
			AdjustSBars();
		}

		/// <summary>
		/// loads the photo data from the currently opened image (to be used for Cropping only)
		/// </summary>
		private void LoadPhotoData() {
			Resetting = true;

			// load the text info
			this.Text = Path;

			// load the resize info
			lbWidth.Text = Image.Image.Width.ToString();
			lbHeight.Text = Image.Image.Height.ToString();
			tbNWidth.Text = Image.Image.Width.ToString();
			tbNHeight.Text = Image.Image.Height.ToString();

			// load the cropping info
			lbOrigDim.Text = Image.Image.Width.ToString() + " x " + Image.Image.Height + ' ' + LsPixels;
			lbSelDim.Text = lbOrigDim.Text;
			numTop.Maximum = Image.Image.Height;
			numBottom.Maximum = Image.Image.Height;
			numLeft.Maximum = Image.Image.Width;
			numRight.Maximum = Image.Image.Width;

			Resetting = false;

			// adjust the scrollbars
			AdjustSBars();
		}

		#endregion

		#region DrawPhoto

		/// <summary>
		/// Draws the current photo into the pictureBox
		/// </summary>
		private void DrawPhoto() {
			// initialize the necessarry objects/vars
			Rectangle r = new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height - pControls.Height);
			int clientHeight = this.ClientRectangle.Height - pControls.Height;
			int clientWidth = this.ClientRectangle.Width;

			if (clientWidth != 0 && clientHeight != 0) {
				Graphics g = Graphics.FromHwnd(this.Handle);
				Bitmap b = new Bitmap(clientWidth, clientHeight);
				Graphics gb = Graphics.FromImage(b);

				// clear the screen area
				gb.FillRectangle(BClear, r);

				if (Zooming == false)	// if we're not supposed to zoom, scale the image to fit the client area
				{
					if (Image.Image.Height > clientHeight || Image.Image.Width > clientWidth) {	// if the photo is bigger than the screen
						// calculate the size of the destRectangle
						double ratio = Image.Image.Height / Image.Image.Width;
						if (ratio < (clientHeight / clientWidth)) {
							// width is constant
							r.Width = clientWidth;
							r.Height = (Image.Image.Height * clientWidth) / Image.Image.Width;
							r.Offset(0, (clientHeight - r.Height) / 2);
						} else {
							// height is constant
							r.Height = clientHeight;
							r.Width = (Image.Image.Width * clientHeight) / Image.Image.Height;
							r.Offset((clientWidth - r.Width) / 2, 0);
						}
					} else {
						// just calculate the offset
						r.Width = Image.Image.Width;
						r.Height = Image.Image.Height;
						r.Offset((clientWidth - r.Width) / 2, (clientHeight - r.Height) / 2);
					}

					// draw the image into the bitmap
					gb.DrawImage(Image.Image, r);
				} else	// draw the zoomed version of the image
				{
					if (Image.Width * ZoomLevel < clientWidth && Image.Height * ZoomLevel < clientHeight) {	// if the zoomed image is still not bigger than the clientRectangle just set the width and height and calculate the offset
						r.Width = Image.Width * ZoomLevel;
						r.Height = Image.Height * ZoomLevel;
						r.Offset((clientWidth - r.Width) / 2, (clientHeight - r.Height) / 2);

						// draw the image into the bitmap
						gb.DrawImage(Image.Image, r);
					} else {	// draw only the selected portion of the zoomed image
						// update the client rectangle size
						clientWidth -= vSb.Width;
						clientHeight -= hSb.Height;

						// do the zooming
						Rectangle area = GetZoomedRect();
						gb.DrawImage(Image.Image, r, area, GraphicsUnit.Pixel);
					}
				}

				// draw the bitmap onto the form
				g.DrawImage(b, 0, 0, clientWidth, clientHeight);

				// dispose the gdi+ objects
				g.Dispose();
				gb.Dispose();
				b.Dispose();
			}
		}

		#endregion

		#region Zooming

		/// <summary>
		/// Takes care of all the calculations related to zooming
		/// </summary>
		private Rectangle GetZoomedRect() {
			// the client rectangle size
			int clientHeight = this.ClientRectangle.Height - pControls.Height - hSb.Height;
			int clientWidth = this.ClientRectangle.Width - vSb.Width;

			// the zoomed image size
			int zWidth = Image.Width * ZoomLevel;
			int zHeight = Image.Height * ZoomLevel;

			// our rectangle object
			Rectangle r = new Rectangle(0, 0, clientWidth, clientHeight);

⌨️ 快捷键说明

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