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

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

1<summary>   
2/// 缩小图片   
3/// </summary>

///

1<param name="strOldPic"/>

源图文件名(包括路径)
///

1<param name="strNewPic"/>

缩小后保存为文件名(包括路径)
///

1<param name="intWidth"/>

缩小至宽度
///

1<param name="intHeight"/>

缩小至高度
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;
}
}

///

1<summary>   
2/// 按比例缩小图片,自动计算高度   
3/// </summary>

///

1<param name="strOldPic"/>

源图文件名(包括路径)
///

1<param name="strNewPic"/>

缩小后保存为文件名(包括路径)
///

1<param name="intWidth"/>

缩小至宽度
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;
}
}

///

1<summary>   
2/// 按比例缩小图片,自动计算宽度   
3/// </summary>

///

1<param name="strOldPic"/>

源图文件名(包括路径)
///

1<param name="strNewPic"/>

缩小后保存为文件名(包括路径)
///

1<param name="intHeight"/>

缩小至高度
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(希望和大家一起来交流)

Published At
Categories with Web编程
Tagged with
comments powered by Disqus