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

📄 photoeditor.cs

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

			// the amount of pixels we can pan in each direction from the center of the client rectangle
			int hRange = clientWidth / 2;
			int vRange = clientHeight / 2;

			// get the area of the image that is supposed to be drawn
			int x;	// x cooridinate of the left upper rectangle point
			int y;	// y cooridinate of the left upper rectangle point
			x = (hSb.Value - hRange) / ZoomLevel;
			y = (vSb.Value - vRange) / ZoomLevel;

			// set the coordinates
			r.Width = (int) Math.Round(Image.Width / ((float) zWidth / (float) clientWidth), 0);
			r.Height = (int) Math.Round(Image.Height / ((float) zHeight / (float) clientHeight), 0);
			r.Offset(x, y);

			// return rectangle
			return r;

		}

		/// <summary>
		/// Adjusts the scrollBars according to the current zoomLevel
		/// </summary>
		private void AdjustSBars() {
			// the zoomed image size
			int zWidth = Image.Width * ZoomLevel;
			int zHeight = Image.Height * ZoomLevel;

			if (Zooming == true) {
				if (zWidth > this.ClientRectangle.Width || zHeight > this.ClientRectangle.Height - pButtons.Height) {
					// show the scrollBars and adjust their lengths according to the zoom factor
					hSb.Visible = true;
					vSb.Visible = true;

					// the client rectangle size (includes scrollBars)
					int clientHeight = this.ClientRectangle.Height - pControls.Height - hSb.Height;
					int clientWidth = this.ClientRectangle.Width - vSb.Width;

					// the amount of pixels we can pan in each direction from the center of the client rectangle
					int hRange = clientWidth / 2;
					int vRange = clientHeight / 2;

					hSb.Minimum = hRange;
					vSb.Minimum = vRange;
					hSb.Maximum = zWidth - hRange;
					vSb.Maximum = zHeight - vRange;
				} else {	// hide the scrollBars
					hSb.Visible = false;
					vSb.Visible = false;
				}

				// adjust the zooming info in the window title
				this.Text = LsZFactor + ' ' + ZoomLevel.ToString() + "00% - " + Path;
			} else {	// hide the scrollBars
				hSb.Visible = false;
				vSb.Visible = false;

				// adjust the zooming info in the window title
				this.Text = LsZFactor + ' ' + LsScaled + " - " + Path;
			}
		}

		#endregion

		#region loadLangStrings()

		private void LoadLanguageStrings() {
			const string MN = "PhotoEditor";

			string lsSave;
			string lsSaveAs;
			string lsReset;
			string lsTitle;
			string lsGeneral;
			string lsProp;
			string lsResize;
			string lsCrop;
			string lsBright;
			string lsCont;
			string lsSat;
			string lsGamma;
			string lsGraysc;
			string lsSepia;
			string lsOrigWidth;
			string lsOrigHeight;
			string lsNewWidth;
			string lsNewHeight;
			string lsAR;
			string lsResFilter;
			string lsDim;
			string lsOrigDim;
			string lsSelDim;
			string lsCropImg;
			string lsCropArea;
			string lsTop;
			string lsBottom;
			string lsLeft;
			string lsRight;
			string lsTime;

			try {
				TXML.TXmlReader reader = XmlHandler.OpenLangFile();

				lsSave = reader.GetString(MN, "Save", "Save");
				lsSaveAs = reader.GetString(MN, "SaveAs", "Save As...");
				lsReset = reader.GetString(MN, "Reset", "Reset");
				lsTitle = reader.GetString(MN, "Title", "Title:");
				lsGeneral = reader.GetString(MN, "General", "General");
				lsProp = reader.GetString(MN, "PhotoProp", "Photo Properties");
				lsResize = reader.GetString(MN, "Resize", "Resize");
				lsCrop = reader.GetString(MN, "Crop", "Crop");
				lsBright = reader.GetString(MN, "Brightness", "Brightness");
				lsCont = reader.GetString(MN, "Contrast", "Contrast");
				lsSat = reader.GetString(MN, "Saturation", "Saturation");
				lsGamma = reader.GetString(MN, "Gamma", "Gamma");
				lsGraysc = reader.GetString(MN, "Grayscale", "Grayscale");
				lsSepia = reader.GetString(MN, "Sepia", "Sepia");
				lsOrigWidth = reader.GetString(MN, "OriginalWidth", "Original Width:");
				lsOrigHeight = reader.GetString(MN, "OriginalHeight", "Original Height:");
				lsNewWidth = reader.GetString(MN, "NewWidth", "New Width:");
				lsNewHeight = reader.GetString(MN, "NewHeight", "New Height:");
				lsAR = reader.GetString(MN, "MaintainAR", "Maintain Aspect Ratio");
				lsResFilter = reader.GetString(MN, "ResizingFilter", "I want to choose the resize filter myself");
				lsDim = reader.GetString(MN, "Dimensions", "Dimensions:");
				lsOrigDim = reader.GetString(MN, "OrigDim", "Original:");
				lsSelDim = reader.GetString(MN, "SelDim", "Selection:");
				lsCropImg = reader.GetString(MN, "CropImg", "Crop Image");
				lsCropArea = reader.GetString(MN, "CropArea", "Crop Area:");
				lsTop = reader.GetString(MN, "Top", "Top");
				lsBottom = reader.GetString(MN, "Bottom", "Bottom");
				lsLeft = reader.GetString(MN, "Left", "Left");
				lsRight = reader.GetString(MN, "Right", "Right");
				LsNumOnly = reader.GetString(MN, "NumOnly", "Not a valid numeric value!");
				LsImgChanged = reader.GetString(MN, "ImgChanged", "Image was changed!");
				LsImgChangedMsg = reader.GetString(MN, "ImgChangedMsg", "Do you wish to save the changes?");
				LsPixels = reader.GetString("PhotoInfoDialog", "Pixels", "Pixels");
				LsNoSave = reader.GetString("PhotosPane", "NoAlbumSave", "The file could not be saved. Please make sure that the disk is neither full, nor write-protected!");
				LsZFactor = reader.GetString(MN, "ZoomFactor", "Zoom factor:");
				LsScaled = reader.GetString(MN, "Scaled", "Scaled Image");
				lsTime = reader.GetString("PhotoInfoDialog", "Taken", "Time taken:");

				reader.Close();
			}
			catch {
				lsSave = "Save";
				lsSaveAs = "Save As...";
				lsReset = "Reset";
				lsTitle = "Title:";
				lsGeneral = "General";
				lsProp = "Photo Properties";
				lsResize = "Resize";
				lsCrop = "Crop";
				lsBright = "Brightness";
				lsCont = "Contrast";
				lsSat = "Saturation";
				lsGamma = "Gamma";
				lsGraysc = "Grayscale";
				lsSepia = "Sepia";
				lsOrigWidth = "Original Width:";
				lsOrigHeight = "Original Height:";
				lsNewWidth = "New Width:";
				lsNewHeight = "New Height:";
				lsAR = "Maintain Aspect Ratio";
				lsResFilter = "I want to choose the resize filter myself";
				lsDim = "Dimensions:";
				lsOrigDim = "Original:";
				lsSelDim = "Selection:";
				lsCropImg = "Crop Image";
				lsCropArea = "Crop Area:";
				lsTop = "Top";
				lsBottom = "Bottom";
				lsLeft = "Left";
				lsRight = "Right";
				LsNumOnly = "Not a valid numeric value!";
				LsImgChanged = "Image was changed!";
				LsImgChangedMsg = "Do you wish to save the changes?";
				LsPixels = "Pixels";
				LsNoSave = "The file could not be saved. Please make sure that the disk is neither full, nor write-protected!";
				LsZFactor = "Zoom factor:";
				LsScaled = "Scaled Image";
				lsTime = "Time taken:";
			}

			// assign the language strings to their controls
			bSave.Text = lsSave;
			bSaveAs.Text = lsSaveAs;
			bReset.Text = lsReset;
			lbTitle.Text = lsTitle;
			tabGen.Text = lsGeneral;
			tabProp.Text = lsProp;
			tabResize.Text = lsResize;
			tabCrop.Text = lsCrop;
			lbLBright.Text = lsBright;
			lbLCont.Text = lsCont;
			lbLSat.Text = lsSat;
			lbLGamma.Text = lsGamma;
//			bGraysc.Text = lsGraysc;
//			bSepia.Text = lsSepia;
			lbLOrigWidth.Text = lsOrigWidth;
			lbLOrigHeight.Text = lsOrigHeight;
			lbLNewWidth.Text = lsNewWidth;
			lbLNewHeight.Text = lsNewHeight;
			cbMaintainAR.Text = lsAR;
			cbChooseFilter.Text = lsResFilter;
			lbDimensions.Text = lsDim;
			lbLOrig.Text = lsOrigDim;
			lbLSel.Text = lsSelDim;
			bCrop.Text = lsCropImg;
			lbCArea.Text = lsCropArea;
			lbTop.Text = lsTop;
			lbBottom.Text = lsBottom;
			lbLeft.Text = lsLeft;
			lbRight.Text = lsRight;
			lbTime.Text = lsTime;
		}

		#endregion

		#region ResetUi

		/// <summary>
		/// Resets the ui
		/// </summary>
		private void ResetUi() {
			Resetting = true;	// make sure the components know that we're resetting

			tbBright.Value = 0;
			tbCont.Value = 0;
			tbGamma.Value = 0;
			tbSat.Value = 0;
			numBright.Value = 0;
			numCont.Value = 0;
			numGamma.Value = 0;
			numSat.Value = 0;
			cobFilt.SelectedIndex = 0;
			Path = "";
			ZoomLevel = 1;
			numTop.Value = 0;
			numBottom.Value = 0;
			numLeft.Value = 0;
			numRight.Value = 0;
			tbTitle.Text = Photos[CurrIndex].Title;
			rtbDesc.Text = Photos[CurrIndex].Description;
			tbTime.Text = Photos[CurrIndex].TimeTaken;

			Resetting = false;
			TextChanged = false;
			ImageChanged = false;
			Zooming = false;
		}

		#endregion

		#region FindRightInterpolationMode

		/// <summary>
		/// returns the apropriate interpolationMode based on the new size
		/// </summary>
		private InterpolationMode FindRightInterpolationMode(int newWidth, int newHeight) {
			if (newWidth > Image.Width || newHeight > Image.Height)	// if newWidth or newHeight is greater than original value use bilinear interpolation
				return InterpolationMode.Bilinear;
			else	// use bicubic interpolation
				return InterpolationMode.Bicubic;
		}

		#endregion

		#region Cropping

		private void PreviewCropping() {
			Image.PreviewCrop((int) numTop.Value, (int) numBottom.Value, (int) numLeft.Value, (int) numRight.Value);
			int newWidth = Image.Image.Width - (int) numLeft.Value - (int) numRight.Value;
			int newHeight = Image.Image.Height - (int) numTop.Value - (int) numBottom.Value;
			lbSelDim.Text = newWidth.ToString() + " x " + newHeight.ToString() + ' ' + LsPixels;
			DrawPhoto();
		}

		private void DoCropping() {
			// create a rectangle object according to the specified crop area
			Rectangle r = new Rectangle((int) numLeft.Value, (int) numTop.Value, (int) (Image.Image.Width - numLeft.Value - numRight.Value), (int) (Image.Image.Height - numTop.Value - numBottom.Value));

			// crop and redraw the image
			Image.Crop(r);
			ImageChanged = true;
			DrawPhoto();

			// reset the cropping ui
			//numTop.Value = 0;
			//numBottom.Value = 0;
			//numLeft.Value = 0;
			//numRight.Value = 0;
			//LoadPhotoData();
		}

		#endregion

		#region ClosingDR

		/// <summary>
		/// returns the DialogResult for the closing question (asks the user if he wished to discard the changes)
		/// </summary>
		private DialogResult GetClosingDR() {
			DialogResult dr = MessageBox.Show(LsImgChangedMsg, LsImgChanged, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
			return dr;
		}

		#endregion

		#region SaveImage

		/// <summary>
		/// saves the current image, returns true if save was successfull and false if it wasn't
		/// </summary>
		private bool SaveImage() {
			if (TextChanged == true)	// did the text change?
			{
				if (Path == Photos[CurrIndex].Path || Path == "")	// is the photo an album member?
				{	// then update title and description
					Photos[CurrIndex].Title = tbTitle.Text;
					Photos[CurrIndex].Description = rtbDesc.Text;
					Photos[CurrIndex].TimeTaken = tbTime.Text;

					// let's update the album
					PhotosP.UpdateImageInformation(CurrIndex, Photos[CurrIndex]);
					if (AttemptAlbumSave() == DialogResult.Abort) {	// if the save didn't work restore the original image data
						Photos[CurrIndex].Title = PTitleBuf;
						Photos[CurrIndex].Description = PDescBuf;
						Photos[CurrIndex].TimeTaken = PTimeTaken;
						return false;
					}
				}
				TextChanged = false;
			}

			if (ImageChanged == true)	// did the image change?
			{
				if (Path == "")	// if the path has not yet been set
				{
					Path = Photos[CurrIndex].Path;
					System.IO.FileInfo fi = new System.IO.FileInfo(Path);
					if (fi.Extension.ToLower() != ".jpg" && fi.Extension.ToLower() != ".jpeg")	// check if the current iamge is a JPEG
					{
						Path = Photos[CurrIndex].Path + ".jpg";	// if not make the path a JPEG
						fi.Delete();						// delete the original file
					}
					Photos[CurrIndex].Path = Path;	// set the Photo's path to the save path
				}

				// let's save the image
				if (AttemptImageSave() == false)
					return false;

				// recreate the thumbnail for the image
				PhotosP.RecreateThumbnail(CurrIndex, Photos[CurrIndex]);

				// let's reset the ui and reload the image
				OpenPhoto();
			}

			bReset.Enabled = false;

			return true;
		}

		/// <summary>
		/// attempts to save the album and takes the appropriate action if an error occurs
		/// </summary>
		private DialogResult AttemptAlbumSave() {
			DialogResult dr = PhotosP.SaveAlbum(MessageBoxButtons.AbortRetryIgnore);
			switch (dr) {
				case DialogResult.Abort: return DialogResult.Abort;
				case DialogResult.Ignore: return DialogResult.Ignore;
				case DialogResult.Retry: AttemptAlbumSave();
					break;
				default: return dr;
			}

⌨️ 快捷键说明

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