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

📄 imagehelper.cs

📁 1、用SQL查询器打开install目录下的dooogo.sql运行之后创建数据库dooogo。 2、然后打开web.config修改 <DbProvider type="Club.Fram
💻 CS
字号:
//Copyright (C) 2006 dooogo.com
//Author:benben
//www.aspxclub.com
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;
using System.IO;
namespace Club.Framework
{
	/// <summary>
	/// ImageHelper 的摘要说明。
	/// </summary>
	public class ImageHelper
	{
		private static Hashtable htmimes=new Hashtable();
		internal readonly string AllowExt = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp";
		private bool CheckValidExt(string sExt)
		{   
			bool flag=false;
			string[] aExt = AllowExt.Split('|');
			foreach(string filetype in aExt)
			{
				if(filetype.ToLower()==sExt)
				{
					flag = true;
					break;
				}
			}   
			return flag;
		}
		private string sourcePath;
		/// <summary>
		/// 来源图片路径
		/// </summary>
		public string SourcePath
		{
			get
			{
				return this.sourcePath;
			}
			set
			{
				this.sourcePath = Globals.GetFilePath(value);
			}
		}
		private string savePath;
		/// <summary>
		/// 图片保存路径
		/// </summary>
		public string SavePath
		{
			get
			{
				if(savePath==null)
				{
					this.savePath = this.SourcePath;
				}
				return this.savePath;
			}
			set
			{
				this.savePath=Globals.GetFilePath(value);
			}
		}
		/// <summary>
		/// 保存图片
		/// </summary>
		/// <param name="image">Image 对象</param>
		/// <param name="ici">指定格式的编解码参数</param>
		private void SaveImage(System.Drawing.Image image,ImageCodecInfo ici)
		{
			//设置 原图片 对象的 EncoderParameters 对象
			EncoderParameters parameters = new EncoderParameters(1);
			parameters.Param[0] = new EncoderParameter(Encoder.Quality, ((long) 90));
			image.Save(savePath,ici, parameters);
			parameters.Dispose();
			image.Dispose();
		}
		/// <summary>
		/// 获取图像编码解码器的所有相关信息
		/// </summary>
		/// <param name="mimeType">包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串</param>
		/// <returns>返回图像编码解码器的所有相关信息</returns>
		private static ImageCodecInfo GetCodecInfo(string mimeType)
		{
			ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
			foreach(ImageCodecInfo ici in CodecInfo)
			{
				if(ici.MimeType == mimeType)return ici;
			}
			return null;
		}
		private void GetImgType()
		{
			htmimes[".jpe"]="image/jpeg";
			htmimes[".jpeg"]="image/jpeg";
			htmimes[".jpg"]="image/jpeg";   
			htmimes[".png"]="image/png";   
			htmimes[".tif"]="image/tiff";
			htmimes[".tiff"]="image/tiff";
			htmimes[".bmp"]="image/bmp";
		}
		private string font="宋体";
		/// <summary>
		/// 字体
		/// </summary>
		public string Font
		{
			get
			{
				return font;
			}
			set
			{
				font=value;
			}
		}
		private int fontSize=14;
		/// <summary>
		/// 文字尺寸
		/// </summary>
		public int FontSize
		{
			get
			{
				return fontSize;
			}
			set
			{
				fontSize=value;
			}
		}
		private Color color=Color.White;
		/// <summary>
		/// 文字颜色
		/// </summary>
		public Color Color
		{
			get
			{
				return this.color;
			}
			set
			{
				this.color=value;
			}
		}
		/// <summary>
		/// 设置颜色
		/// </summary>
		/// <param name="alpha">透明度</param>
		/// <param name="color">系统颜色</param>
		public void SetColor(int alpha,Color color)
		{
			this.Color=Color.FromArgb(alpha,color);
		}
		/// <summary>
		/// 设置颜色
		/// </summary>
		/// <param name="red">红色分量值 0-255</param>
		/// <param name="green">绿色分量值 0-255</param>
		/// <param name="blue">蓝色分量值 0-255</param>
		public void SetColor(int red,int green,int blue)
		{
			this.Color = Color.FromArgb(red,green,blue);
		}
		/// <summary>
		/// 设置颜色
		/// </summary>
		/// <param name="colorName">已命名颜色名称</param>
		public void SetColor(string colorName)
		{
			this.Color = Color.FromName(colorName);
		}
		/// <summary>
		/// 制作水印文字
		/// </summary>
		/// <param name="text">显示的文字</param>
		public void MakeWatermarkFont(string text)
		{
			GetImgType();
			string sExt = this.SourcePath.Substring(this.SourcePath.LastIndexOf(".")).ToLower();
			if(!CheckValidExt(sExt))
			{
				throw new ArgumentException("原图片文件格式不正确,支持的格式有[ "+ AllowExt +"]","SourceImagePath");
			}
			System.Drawing.Image image = System.Drawing.Image.FromFile(this.sourcePath);
			Graphics g = Graphics.FromImage(image);
			g.DrawImage(image, 0, 0, image.Width, image.Height);
			Font f = new Font(this.Font, this.FontSize);
			Brush b = new SolidBrush(this.Color);
			g.DrawString(text, f, b, 10,10);
			g.Dispose();
			image.Save(this.SavePath,GetFormat(this.SourcePath));
			image.Dispose();
			//this.SaveImage(image,GetCodecInfo((string)htmimes[sExt]));
		}
		/// <summary>
		/// 制作水印图片
		/// </summary>
		/// <param name="watermarkImagePath">要绘制在上层的图片地址</param>
		public void MakeWatermarkImage(string watermarkImagePath)
		{
			GetImgType();
			string sExt = this.SourcePath.Substring(this.SourcePath.LastIndexOf(".")).ToLower();
			if(!CheckValidExt(sExt))
			{
				throw new ArgumentException("原图片文件格式不正确,支持的格式有[ "+ AllowExt +"]","SourceImagePath");
			}
			System.Drawing.Image image = System.Drawing.Image.FromFile(this.SourcePath);
			System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Globals.GetFilePath(watermarkImagePath));
			Graphics g = Graphics.FromImage(image);
			Rectangle rect=new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height);
			g.DrawImage(copyImage, rect, 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
			g.Dispose();
			image.Save(this.SavePath,GetFormat(this.SourcePath));
			image.Dispose();
		}
		/// <summary>
		/// 检查文件的格式
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public static ImageFormat GetFormat(string name)
		{
			string ext = name.Substring(name.LastIndexOf(".") + 1);
			switch(ext.ToLower())
			{
				case "jpg":
				case "jpeg":
					return ImageFormat.Jpeg;
				case "bmp":
					return ImageFormat.Bmp;
				case "png":
					return ImageFormat.Png;
				case "gif":
					return ImageFormat.Gif;
				default:
					return ImageFormat.Jpeg;
			}
		}
		/// <summary>
		/// 检查文件是否存在和文件名是否合法
		/// </summary>
		/// <param name="filepath"></param>
		/// <returns></returns>
		public static bool ValidateFile(string filepath)
		{
			if (File.Exists(filepath))
			{
				return false;
			}

			return Regex.IsMatch(filepath,
				"(?:[^\\/\\*\\?\\\"\\<\\>\\|\\n\\r\\t]+)\\.(?:jpg|jpeg|gif|png|bmp)",
				RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
				);
		}
		/// <summary>
		/// 返回新图片尺寸
		/// </summary>
		/// <param name="width">原始宽</param>
		/// <param name="height">原始高</param>
		/// <param name="maxWidth">新图片最大宽</param>
		/// <param name="maxHeight">新图片最大高</param>
		/// <returns></returns>
		public static Size ResizeImage(int width, int height, int maxWidth, int maxHeight)
		{
			decimal MAX_WIDTH = (decimal)maxWidth;
			decimal MAX_HEIGHT = (decimal)maxHeight;
			decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT;

			int newWidth, newHeight;

			decimal originalWidth = (decimal)width;
			decimal originalHeight = (decimal)height;
			
			if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT) 
			{
				decimal factor;
				// determine the largest factor 
				if (originalWidth / originalHeight > ASPECT_RATIO) 
				{
					factor = originalWidth / MAX_WIDTH;
					newWidth = Convert.ToInt32(originalWidth / factor);
					newHeight = Convert.ToInt32(originalHeight / factor);
				} 
				else 
				{
					factor = originalHeight / MAX_HEIGHT;
					newWidth = Convert.ToInt32(originalWidth / factor);
					newHeight = Convert.ToInt32(originalHeight / factor);
				}	  
			} 
			else 
			{
				newWidth = width;
				newHeight = height;
			}

			return new Size(newWidth,newHeight);
			
		}
		/// <summary>
		/// 生成缩略图
		/// </summary>
		/// <param name="maxWidth">最大宽度</param>
		/// <param name="maxHeight">最大高度</param>
		public void MakeAlbumImages(int maxWidth,int maxHeight)
		{

			System.Drawing.Image original = System.Drawing.Image.FromFile(this.SourcePath);

			Size _newSize = ResizeImage(original.Width,original.Height,maxWidth,maxHeight);
			System.Drawing.Image displayImage = new Bitmap(original,_newSize);
			original.Dispose();
			try
			{
				displayImage.Save(this.SavePath, GetFormat(this.SourcePath));   
			}
			finally
			{
				displayImage.Dispose();
			}
		}
	}
}

⌨️ 快捷键说明

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