asp.net 图片压缩和判别类

 说明
    图片压缩在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;
            }

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值