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

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

//************************************************************//

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

//************************************************************//

/// <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;

}

}

//************************************************************//

//盛天 2004-11-5

//QQ:37878073(希望和大家一起来交流)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航