一个图片缩放的类

做开发时经常需要把图片缩放,特献上一个图片缩放的类!

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

///


/// Photo 的摘要说明
///

public class Photo
{
    public Photo()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    //************************************************************//
    //下面给出三个简单的方法,后面两个方法是扩展,估计有时用得着
    //************************************************************//
    ///


    /// 缩小图片
    ///

    /// 源图文件名(包括路径)
    /// 缩小后保存为文件名(包括路径)
    /// 缩小至宽度
    /// 缩小至高度
    public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
    {

        System.Drawing.Bitmap objPic, objNewPic;
        try
        {
            objPic = new System.Drawing.Bitmap(strOldPic);
            objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
            objNewPic.Save(strNewPic);

        }
        catch (Exception exp) { throw exp; }
        finally
        {
            objPic = null;
            objNewPic = null;
        }
    }

    ///


    /// 按比例缩小图片,自动计算高度
    ///

    /// 源图文件名(包括路径)
    /// 缩小后保存为文件名(包括路径)
    /// 缩小至宽度
    public void SmallPicHeight(string strOldPic, string strNewPic, int intWidth)
    {

        System.Drawing.Bitmap objPic, objNewPic;
        try
        {
            objPic = new System.Drawing.Bitmap(strOldPic);
            int intHeight = (intWidth / objPic.Width) * objPic.Height;
            objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
            objNewPic.Save(strNewPic);

        }
        catch (Exception exp) { throw exp; }
        finally
        {
            objPic = null;
            objNewPic = null;
        }
    }


    ///


    /// 按比例缩小图片,自动计算宽度
    ///

    /// 源图文件名(包括路径)
    /// 缩小后保存为文件名(包括路径)
    /// 缩小至高度
    public void SmallPicWidth(string strOldPic, string strNewPic, int intHeight)
    {

        System.Drawing.Bitmap objPic, objNewPic;
        try
        {
            objPic = new System.Drawing.Bitmap(strOldPic);
            int intWidth = (intHeight / objPic.Height) * objPic.Width;
            objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
            objNewPic.Save(strNewPic);

        }
        catch (Exception exp) { throw exp; }
        finally
        {
            objPic = null;
            objNewPic = null;
        }
    }

你可能感兴趣的:(C#技术,.net技术,exception,string,null,system,class,扩展)