说明
图片压缩在web开发中是经常要用的功能,因此我将最近一个项目中的功能封装成类ImgCmpsFactory以便下次能重复利用。 在上传图片的时候,光从图片后缀名来判断是否是图片是不够的,我们要从文件内部结构来查看是否是图片(具体的图片格式不做介绍),ImgUtil中将实现这个功能。
这两个类,朋友们可以随意改进,但有个要求,改进后的版本要同时要共享出来,顺便通知下俺:)。
代码:
using System;
using System.Drawing;
using System.IO;
namespace WebImagePackage
{
/// <summary>
/// 压缩图象
/// </summary>
public static class ImgCmpsFactory
{
/// <summary>
/// 根据宽度按比例压缩图片
/// </summary>
/// <param name="oldImg"></param>
/// <param name="width"></param>
/// <returns></returns>
public static Bitmap cmpsImgByWidth(Bitmap oldImg, int width)
{
int height = (int)(oldImg.Height * ((double)width / oldImg.Width));
Bitmap newImg = null;
try
{
newImg = getResizeImageByRealSize(oldImg, width, height);
return newImg;
}
catch
{
return null;
}
}
/// <summary>
/// 根据高度按比例压缩图片
/// </summary>
/// <param name="oldImg"></param>
/// <param name="height"></param>
/// <returns></returns>
public static Bitmap cmpsImgByHeight(Bitmap oldImg, int height)
{
int width = (int)(oldImg.Width * ((double)height / oldImg.Height));
Bitmap newImg = null;
try
{
newImg = getResizeImageByRealSize(oldImg, width, height);
return newImg;
}
catch
{
return null;
}
}
//根据宽(象素),高来构建新图片
private static Bitmap getResizeImageByRealSize(Bitmap oldImg, int width, int height)
{
if (oldImg == null)
return null;
Bitmap newImg = null;
try
{
newImg = new Bitmap(width, height);
//构建newImg的绘图对象
Graphics g = Graphics.FromImage(newImg);
//设置变换图象时的插补模式 http://msdn.microsoft.com/zh-tw/library/k0fsyd4e(VS.80).aspx
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置图片的呈现质量
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//以透明色填充
g.Clear(Color.Transparent);
//绘制新图片
g.DrawImage(oldImg, new Rectangle(0, 0, width, height), new Rectangle(0, 0, oldImg.Width, oldImg.Height), GraphicsUnit.Pixel);
return newImg;
}
catch (Exception ex)
{
return null;
}
}
}
/// <summary>
/// 图象处理类,包括判断是否是图片
/// </summary>
public static class ImgUtil
{
/// <summary>
/// 判断指定路径的文件是否是图片
/// </summary>
/// <param name="FileName">图片物理路径</param>
/// <returns>null说明不是图片,否则返回Bitmap对象</returns>
public static Bitmap IsImg(string FileName)
{
if (string.IsNullOrEmpty(FileName))
return null;
Bitmap img=null;
try
{
img = (Bitmap)Bitmap.FromFile(FileName);
return img;
}catch (OutOfMemoryException)
{
return null;
}
}
/// <summary>
/// 判断指定的文件流是否是图片
/// </summary>
/// <param name="FileStream">文件流</param>
/// <returns>null说明不是图片,否则返回Bitmap对象</returns>
public static Bitmap IsImg(Stream FileStream)
{
if (string.IsNullOrEmpty(FileName))
return null;
Bitmap img = null;
try
{
img = (Bitmap)Bitmap.FromStream(FileStream);
return img;
}catch (ArgumentException)
{
return null;
}
}
}
}
图片压缩在web开发中是经常要用的功能,因此我将最近一个项目中的功能封装成类ImgCmpsFactory以便下次能重复利用。 在上传图片的时候,光从图片后缀名来判断是否是图片是不够的,我们要从文件内部结构来查看是否是图片(具体的图片格式不做介绍),ImgUtil中将实现这个功能。
这两个类,朋友们可以随意改进,但有个要求,改进后的版本要同时要共享出来,顺便通知下俺:)。
代码:
using System;
using System.Drawing;
using System.IO;
namespace WebImagePackage
{
/// <summary>
/// 压缩图象
/// </summary>
public static class ImgCmpsFactory
{
/// <summary>
/// 根据宽度按比例压缩图片
/// </summary>
/// <param name="oldImg"></param>
/// <param name="width"></param>
/// <returns></returns>
public static Bitmap cmpsImgByWidth(Bitmap oldImg, int width)
{
int height = (int)(oldImg.Height * ((double)width / oldImg.Width));
Bitmap newImg = null;
try
{
newImg = getResizeImageByRealSize(oldImg, width, height);
return newImg;
}
catch
{
return null;
}
}
/// <summary>
/// 根据高度按比例压缩图片
/// </summary>
/// <param name="oldImg"></param>
/// <param name="height"></param>
/// <returns></returns>
public static Bitmap cmpsImgByHeight(Bitmap oldImg, int height)
{
int width = (int)(oldImg.Width * ((double)height / oldImg.Height));
Bitmap newImg = null;
try
{
newImg = getResizeImageByRealSize(oldImg, width, height);
return newImg;
}
catch
{
return null;
}
}
//根据宽(象素),高来构建新图片
private static Bitmap getResizeImageByRealSize(Bitmap oldImg, int width, int height)
{
if (oldImg == null)
return null;
Bitmap newImg = null;
try
{
newImg = new Bitmap(width, height);
//构建newImg的绘图对象
Graphics g = Graphics.FromImage(newImg);
//设置变换图象时的插补模式 http://msdn.microsoft.com/zh-tw/library/k0fsyd4e(VS.80).aspx
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置图片的呈现质量
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//以透明色填充
g.Clear(Color.Transparent);
//绘制新图片
g.DrawImage(oldImg, new Rectangle(0, 0, width, height), new Rectangle(0, 0, oldImg.Width, oldImg.Height), GraphicsUnit.Pixel);
return newImg;
}
catch (Exception ex)
{
return null;
}
}
}
/// <summary>
/// 图象处理类,包括判断是否是图片
/// </summary>
public static class ImgUtil
{
/// <summary>
/// 判断指定路径的文件是否是图片
/// </summary>
/// <param name="FileName">图片物理路径</param>
/// <returns>null说明不是图片,否则返回Bitmap对象</returns>
public static Bitmap IsImg(string FileName)
{
if (string.IsNullOrEmpty(FileName))
return null;
Bitmap img=null;
try
{
img = (Bitmap)Bitmap.FromFile(FileName);
return img;
}catch (OutOfMemoryException)
{
return null;
}
}
/// <summary>
/// 判断指定的文件流是否是图片
/// </summary>
/// <param name="FileStream">文件流</param>
/// <returns>null说明不是图片,否则返回Bitmap对象</returns>
public static Bitmap IsImg(Stream FileStream)
{
if (string.IsNullOrEmpty(FileName))
return null;
Bitmap img = null;
try
{
img = (Bitmap)Bitmap.FromStream(FileStream);
return img;
}catch (ArgumentException)
{
return null;
}
}
}
}