前面2个Class几乎天天用。百度和谷歌搜了下,好像针对图片操作的Class(这里说ImgHelper)曝光的比较少,比如生成小图啊、水印啊、裁剪啊这些功能小弟求这方面的Class,一个函数方法也行,我收集大家的,然后整合一下发到论坛给各位使用
解决方案 »
- 在ASP.net里我用Gridview,在模板里建了个Linkbutton,写了onclientclick的删除提示框,运行时不能删除前两行!
- 怎样判断DropDownList是否选择?
- 如何将数据导出到 Execl 文件,并引发对话框以告诉客户要下载此 Execl 文件 ?(vb)
- 如何实现asp,asp.net不同程序间的单点登录
- 如何把Session清除?
- 为什么NameValueCollection.Item变成只读的了?
- 菜鸟求救。。。客户端按纽传递数据问题。。。大家帮帮忙啊。。。
- 各位老大:怎么扑捉浏览器的resize事件在线等?
- jquery中this的问题
- 各位大侠,有谁碰到这样的问题,帮忙看一下原因
- html里嵌入多个htm页面
- 求问jquery如何把多个区域分别loading.
多收集一些常用的功能,然后看你是传入文件File还是传入文件名来处理,最后发布,保证推荐贴
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;using YDSystem.ZXF.Config;namespace YDSystem.ZXF.Common.Configurable
{
/// <summary>
/// 图像处理帮助类
/// </summary>
public class ImageWorker
{
//会产生graphics异常的PixelFormat,索引颜色
private static PixelFormat[] indexedPixelFormats = {
PixelFormat.Undefined,
PixelFormat.DontCare,
PixelFormat.Format16bppArgb1555,
PixelFormat.Format1bppIndexed,
PixelFormat.Format4bppIndexed,
PixelFormat.Format8bppIndexed
}; #region 私有方法
/// <summary>
/// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中
/// </summary>
/// <param name="imgPixelFormat">原图片的PixelFormat</param>
/// <returns></returns>
private static bool IsIndexedPixelFormat(PixelFormat imgPixelFormat)
{
foreach (PixelFormat pf in indexedPixelFormats)
{
if (pf.Equals(imgPixelFormat)) return true;
}
return false;
} /// <summary>
/// 计算水印的位置
/// </summary>
/// <param name="oWidth">源图片宽度</param>
/// <param name="oHeight">源图片高度</param>
/// <param name="Width">水印图片宽度</param>
/// <param name="Height">鲨鱼图片高度</param>
/// <returns></returns>
private static PointF CountWaterPosition(YDSystem.ZXF.Config.WaterPosition position, int width, int height, int Width, int Height)
{
float padding = 15.0f; //水印与图片边框间的距离 PointF point = new PointF(); //确定水印位置
switch (position)
{
case WaterPosition.Center: //居中
point.X = (float)((width - Width) / 2);
point.Y = (float)((height - Height) / 2);
break;
case WaterPosition.NorthEast: //右上
point.X = (float)(width - Width - padding);
point.Y = padding;
break;
case WaterPosition.East: //右中
point.X = (float)(width - Width - padding);
point.Y = (float)((height - Height) / 2);
break;
case WaterPosition.SouthEast: //右下
point.X = (float)(width - Width - padding);
point.Y = (float)(height - Height - padding);
break;
case WaterPosition.South: //中下
point.X = (float)((width - Width) / 2);
point.Y = (float)(height - Height - padding);
break;
case WaterPosition.SouthWest: //左下
point.X = padding;
point.Y = (float)(height - Height - padding);
break;
case WaterPosition.West: //左中
point.X = padding;
point.Y = (float)((height - Height) / 2);
break;
case WaterPosition.NorthWest: //左上
point.X = padding;
point.Y = padding;
break;
case WaterPosition.North: //中上
point.X = (float)((width - Width) / 2);
point.Y = padding;
break;
}//switch return point;
}
#endregion /// <summary>
/// 按指定的配置添加水印
/// </summary>
/// <param name="image">待添加水印的图片</param>
/// <param name="config">指定的YDSystem.ZXF.Config.WaterConfigInfo水印配置实例</param>
public static void AddWater(ref Image image, YDSystem.ZXF.Config.WaterConfigInfo config)
{
if (image == null) //源图片不存在
{
return;
} int height = image.Height; //原图片高度
int width = image.Width; //原图片宽度 if (IsIndexedPixelFormat(image.PixelFormat)) //索引色图片,必须转化为bitmap进行处理,否则会报异常
{
//创建bitmap对象
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics gr = Graphics.FromImage(bmp);
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.CompositingQuality = CompositingQuality.HighQuality;
//画到bitmap上
gr.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
//复制
image = (Image)bmp.Clone();
//释放资源
gr.Dispose();
bmp.Dispose();
} //创建Graphics对象
Graphics g = Graphics.FromImage(image);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality; PointF point = new PointF(); //水印位置 if (config.Type == WaterType.Text) //水印类型是文字
{
//创建字体
Font font = new Font(config.FontName, config.FontSize, config.FontStyle, GraphicsUnit.Pixel); //测试字符串高度及宽度
SizeF textSize = g.MeasureString(config.Text, font); //保存字符串宽及高
int Width = (int)textSize.Width;
int Height = (int)textSize.Height; //确定水印位置
point = CountWaterPosition(config.Position, width, height, Width, Height); //确定文本颜色
Color color = Color.FromArgb(Convert.ToInt32(config.FontColor.Substring(1), 16)); //删除颜色#666666前面的“#”
SolidBrush brush = new SolidBrush(Color.FromArgb(config.Opacity * 255 / 100, color)); //用白色brush画字符串阴影
g.DrawString(config.Text, font, new SolidBrush(Color.White), point.X + 1, point.Y + 1); //画出字符串
g.DrawString(config.Text, font, brush, point); //释放资源
g.Dispose(); }//if
else //水印类型是图片
{
//获取水印图片
Image Image = null;
ImageAttributes attributes = new ImageAttributes(); try
{
Image = Image.FromFile(Utils.MapPath(config.ImagePath)); //获取水印图片宽和高
int Width = Image.Width;
int Height = Image.Height; //确定水印位置
point = CountWaterPosition(config.Position, width, height, Width, Height); //透明水印
float[][] colorMatrixParam = {
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,config.Opacity * 1.0f / 100,0.0f}, //透明度
new float[] {0.0f,0.0f,0.0f,0.0f,1.0f}
}; ColorMatrix colorMatrix = new ColorMatrix(colorMatrixParam); //重设图像属性
attributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //铬上水印
g.DrawImage(Image, new Rectangle((int)point.X, (int)point.Y, Width, Height), 0, 0, Width, Height, GraphicsUnit.Pixel, attributes);
}
catch (Exception ex)
{
Utils.ShowAdminError("水印图片不存在", ex.Message);
}
finally
{
//释放资源
attributes.Dispose();
Image.Dispose();
g.Dispose();
} }//else
} /// <summary>
/// 按默认的水印配置添加水印
/// </summary>
/// <param name="image">待添加水印的图片实例</param>
public static void AddDefaultWater(ref Image image)
{
AddWater(ref image, GlobalConfigManage.LoadConfig().WaterSettings);
} /// <summary>
/// 按默认的水印配置添加水印
/// </summary>
/// <param name="image">待添加水印的图片实例</param>
public static void AddArticleWater(ref Image image)
{
WaterConfigInfo config = null;
if (GlobalConfigManage.LoadConfig().UseDefaultWaterFirst) //默认配置优先
{
config = GlobalConfigManage.LoadConfig().WaterSettings;
}
else
{
config = ArticleConfigManage.LoadConfig().WaterSettings;
}
AddWater(ref image, config);
}
/// 生成指定大小的缩略图(不变形),保存成jpg的格式。若原图尺寸比要求的缩略图小,无法生成。
/// </summary>
/// <param name="image"></param>
/// <param name="absSavedPath">保存缩略图完整的绝对路径,包括文件名</param>
/// <param name="width">缩略图宽</param>
/// <param name="height">缩略图高</param>
/// <returns></returns>
public static bool MakeThumbnail(Image image, string absSavedPath, int width, int height)
{
#region 原方法,按配置文件中的配置大小生成缩略图
////获取缩略图设置
//int sWidth = GetInt("MaxThumbnailWidth");
//int sHeight = GetInt("MaxThumbnailHeight");
//bool isCutMode = Convert.ToBoolean(GetString("IsCutMode")); ////缩略图尺寸不能超过原图尺寸
//if(sWidth > oWidth)
//{
// sWidth = oWidth;
//}
//if(sHeight > oHeight)
//{
// sHeight = oHeight;
//}
////计算缩略图应有的尺寸
//int width = 0;
//int height = 0; //int x = 0;
//int y = 0;
//if (isCutMode) //裁剪模式
//{
// //裁剪原图片的中部
// x = (oWidth - sWidth) / 2;
// y = (oHeight - sHeight ) / 2;
// //图片尺寸
// width = sWidth;
// height = sHeight;
// //调整原图的裁剪大小
// oWidth = sWidth;
// oHeight = sHeight;
//}
//else //全图模式
//{
// if (oWidth * 1.0 / sWidth > oHeight * 1.0 / sHeight) //宽的比例比高的大,要以sWidth为准
// {
// width = sWidth;
// //按原图比例转换高度
// height = (int)(width * oHeight * 1.0 / oWidth);
// }
// else //高的比例大
// {
// height = sHeight;
// //按原图比例转换宽度
// width = (int)(height * oWidth * 1.0 / oHeight);
// }
//}
#endregion //获取原图尺寸
int oWidth = image.Width;
int oHeight = image.Height; //检查缩略图与原图的尺寸
if (width <= 0 || height <= 0 || oWidth < width || oHeight < height) //尺寸不符,无法生成缩略图
{
return false;
} //先把原图缩小到可获取缩略图的最小尺寸
int minWidth; //最小宽
int minHeight; //最小高 //根据原图与缩略图的宽高比例决定最小图的尺寸 (原图宽/缩略图宽 原图高/缩略图高)
if (oWidth * 1.0 / width > oHeight * 1.0 / height) //宽的比例大,以缩略图的高为基准生成最小图
{
minHeight = height;
minWidth = (int)(oWidth * 1.0 / oHeight * minHeight); //按原比例缩小
}
else //高的比例大,以缩略图的宽为基准生成最小图
{
minWidth = width;
minHeight = (int)(oHeight * 1.0 / oWidth * minWidth);
} Graphics g = null; //根据计算的尺寸生成最小图
Bitmap minBmp = new Bitmap(minWidth, minHeight); g = Graphics.FromImage(minBmp);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置合成模式
g.CompositingQuality = CompositingQuality.HighQuality; g.Clear(Color.Transparent); g.DrawImage(image, new Rectangle(0, 0, minWidth, minHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
int x = 0; //缩略图左上角x坐标
int y = 0; //缩略图左上角y坐标
//计算左上角坐标
if (minWidth > width) //最小图比缩略图宽
{
x = (int)((minWidth - width) * 1.0 / 2);
}
if (minHeight > height)
{
y = (int)((minHeight - height) * 1.0 / 2);
} //创建bitmap
Bitmap bmp = new Bitmap(width, height);
g = Graphics.FromImage(bmp); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置合成模式
g.CompositingQuality = CompositingQuality.HighQuality; //清空画布并以透明背景色填充
g.Clear(Color.Transparent); //裁剪缩略图
g.DrawImage(minBmp, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
try
{
//从配置文件中获取图片品质系数
long quality = GlobalConfigManage.LoadConfig().ImageSettings.Quality; ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
{
ici = codec;
break;
}
}
EncoderParameters ep = new EncoderParameters();
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); //以JPEG格式保存,图像质量较好
bmp.Save(absSavedPath, ici, ep);
}
catch (Exception ex)
{
//向外抛出异常
return false;
throw ex; }
finally
{
minBmp.Dispose();
g.Dispose();
bmp.Dispose();
} return true;
} /// <summary>
/// 生成指定大小的缩略图(不变形),保存成jpg的格式。如果原图比要求的缩略图小,不做任何操作。
/// </summary>
/// <param name="srcPath"></param>
/// <param name="savePath"></param>
public static bool MakeThumbnail(string absSrcPath, string absSavedPath, int width, int height)
{
Image image;
try
{
image = Image.FromFile(absSrcPath);
}
catch (Exception ex)
{
return false;
throw ex;
}
bool result = MakeThumbnail(image, absSavedPath, width, height);
//销毁对象,释放资源
image.Dispose();
return result;
} /// <summary>
/// 等比例缩放图片,保存成jpg的格式,约束最大宽度和最大高度
/// </summary>
/// <param name="image"></param>
/// <param name="savePath"></param>
/// <param name="maxHeight"></param>
/// <param name="maxWidth"></param>
public static void ZoomImage(Image image, string savePath, int maxWidth, int maxHeight)
{
//获取原图尺寸
int oWidth = image.Width;
int oHeight = image.Height; //缩略图尺寸不能超过原图尺寸
if (maxWidth > oWidth)
{
maxWidth = oWidth;
}
if (maxHeight > oHeight)
{
maxHeight = oHeight;
} //计算缩略图应有的尺寸
int width = 0;
int height = 0; if (oWidth * 1.0 / maxWidth > oHeight * 1.0 / maxHeight) //宽的比例比高的大,要以maxWidth为准
{
width = maxWidth;
//按原图比例转换高度
height = (int)(width * oHeight * 1.0 / oWidth);
}
else //高的比例大
{
height = maxHeight;
//按原图比例转换宽度
width = (int)(height * oWidth * 1.0 / oHeight);
} //创建bitmap
Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(Color.Transparent); g.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel); try
{
//从配置文件中获取图片品质系数
long quality = GlobalConfigManage.LoadConfig().ImageSettings.Quality; ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
{
ici = codec;
break;
}
}
EncoderParameters ep = new EncoderParameters();
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); //以JPEG格式保存,图像质量较好
bmp.Save(savePath, ici, ep);
}
catch (Exception ex)
{
//向外抛出异常
throw ex;
}
finally
{
g.Dispose();
bmp.Dispose();
}
} /// <summary>
/// 生成缩略图并保存
/// </summary>
/// <param name="srcPath">源图片的完整物理路径</param>
/// <param name="savePath">目标图片的完整物理路径</param>
/// <param name="maxWidth">最大宽度</param>
/// <param name="maxHeight">最大高度</param>
public static void ZoomImage(string srcPath, string savePath, int maxWidth, int maxHeight)
{
Image image = Image.FromFile(srcPath);
ZoomImage(image, savePath, maxWidth, maxHeight);
//销毁对象,释放资源
image.Dispose();
}
}
}
不懂地方,欢迎追问