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

📄 imagehandling.cs

📁 本系统是在asp版《在线文件管理器》的基础上设计制作
💻 CS
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using CommunityServer.Components;

namespace CommunityServer.Galleries.Components
{

	#region GalleryImageSettings

	public class GalleryImageSettings
	{
		public int		Width;
		public int		Height;
		public int		Quality;
		public int		Brightness;
		public bool		MaintainRatio;
		public bool		OriginalSize;
		public bool		UpScale;

		public GalleryImageSettings()
		{
			MaintainRatio = true;
			OriginalSize = false;
			UpScale = false;
			Brightness = -1;
		}

		public GalleryImageSettings(int width, int height, int quality, bool maintainRatio, bool originalSize, bool upScale)
		{
			Width = width;
			Height = height;
			Quality = quality;
			MaintainRatio = maintainRatio;
			OriginalSize = originalSize;
			UpScale = upScale;
			Brightness = -1;
		}

		public GalleryImageSettings(int width, int height, int quality, bool maintainRatio) : this()
		{
			Width = width;
			Height = height;
			Quality = quality;
			MaintainRatio = maintainRatio;
		}
	}

	#endregion

	public class ImageHandling
	{

		public static void GetMaintainedRatio(Picture picture, GalleryImageSettings settings)
		{
			// See if we want to maintain the image ratio
			if(settings.MaintainRatio == true)
			{
				double heightRatio = (double)picture.Height / picture.Width;
				double widthRatio = (double)picture.Width / picture.Height;

				int desiredHeight = settings.Height;
				int desiredWidth = settings.Width;


				settings.Height = desiredHeight;
				if(widthRatio > 0)
					settings.Width = Convert.ToInt32(settings.Height * widthRatio);

				if (settings.Width > desiredWidth)
				{
					settings.Width = desiredWidth;
					settings.Height = Convert.ToInt32(settings.Width * heightRatio);
				}
			}

			// In some instances, we might not want to scale it larger
			if((settings.UpScale == false) && (settings.Height > picture.Height || settings.Width > picture.Width))
			{
				// TODO: Not perfect
				settings.Height = picture.Height;
				settings.Width = picture.Width;
				settings.OriginalSize = true;
				return;
			}
		}


		public static void ScalePicture(string filename, Picture picture, GalleryImageSettings settings)
		{
			// See the image doesn't already exist
			if(!File.Exists(filename))
			{
				// Get the image
				PostAttachment pictureData = Pictures.GetPictureData(picture.PostID);

				if((pictureData.Content == null) || (pictureData.Content.Length == 0))
					return;
				MemoryStream stream = new MemoryStream();
				stream.Write(pictureData.Content, 0, pictureData.Content.Length);
				stream.Position = 0;
				Bitmap image = (Bitmap)Bitmap.FromStream(stream, true);
				stream.Close();

				// We got here, so we do want to scale it.
				Graphics graph;
				Bitmap bitmap = new Bitmap(settings.Width, settings.Height);
				graph = Graphics.FromImage(bitmap);
				graph.InterpolationMode = InterpolationMode.HighQualityBicubic;

				// Set the brightness
				if(settings.Brightness != -1)
				{
					float brightness = (settings.Brightness - 50) / 100.0f;
					ImageAttributes attrs = new ImageAttributes();
					ColorMatrix colorMatrix = new ColorMatrix(new float[][] {
																				new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
																				new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
																				new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
																				new float[] { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f },
																				new float[] { brightness, brightness, brightness, 0.0f, 1.0f }
																			} );
					attrs.SetColorMatrix(colorMatrix);

					Bitmap bitmapB = new Bitmap(settings.Width, settings.Height);
					Graphics graphB = Graphics.FromImage(bitmapB);
					graphB.InterpolationMode = InterpolationMode.HighQualityBicubic;
					graphB.DrawImage(image, 0, 0, settings.Width, settings.Height);
					graph.DrawImage(bitmapB, new Rectangle(0, 0, bitmapB.Width, bitmapB.Height), 0, 0, bitmapB.Width, bitmapB.Height, GraphicsUnit.Pixel, attrs);
					graphB.Dispose();
					bitmapB.Dispose();
				}
				else
					graph.DrawImage(image, 0, 0, settings.Width, settings.Height);

				// specify codec
				ImageCodecInfo codec = GetEncoderInfo("image/jpeg");

				// set image quality
				EncoderParameters eps = new EncoderParameters(1);
				eps = new EncoderParameters();
				eps.Param[0] = new EncoderParameter(Encoder.Quality, (long)settings.Quality);

				// Save to the cache
				try { bitmap.Save(filename, codec, eps); }
				catch { (new CSException(CSExceptionType.AccessDenied, "Permission to save cache file denied (" + filename + ")")).Log(); }

				bitmap.Dispose();
				graph.Dispose();
				eps.Dispose();
				image.Dispose();
			}
		}


		/// <summary>
		/// Gets the encoder information for the specified mimetype.  Used in imagescaling
		/// </summary>
		/// <param name="mimeType">The mimetype of the picture.</param>
		/// <returns>System.Drawing.Imaging.ImageCodecInfo</returns>
		public static ImageCodecInfo GetEncoderInfo(string mimeType)
		{
			ImageCodecInfo [] myEncoders =
				ImageCodecInfo.GetImageEncoders();

			foreach (ImageCodecInfo myEncoder in myEncoders)
				if (myEncoder.MimeType == mimeType)
					return myEncoder;
			return null;
		}

	}
}

⌨️ 快捷键说明

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