您的位置:首页 > 编程语言 > C#

如何在C#中实现图片缩放

2012-03-08 11:27 453 查看
/// <summary>

/// 缩小图片

/// </summary>

/// <param name="strOldPic">源图文件名(包括路径)</param>

/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>

/// <param name="intWidth">缩小至宽度</param>

/// <param name="intHeight">缩小至高度</param>

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;

}

}

/// <summary>

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

/// </summary>

/// <param name="strOldPic">源图文件名(包括路径)</param>

/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>

/// <param name="intWidth">缩小至宽度</param>

public void SmallPic(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;

}

}

/// <summary>

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

/// </summary>

/// <param name="strOldPic">源图文件名(包括路径)</param>

/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>

/// <param name="intHeight">缩小至高度</param>

public void SmallPic(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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: