如何在C#中实现图片缩放
2024-07-21 02:18:39
供稿:网友
//************************************************************//
//下面给出三个简单的方法,后面两个方法是扩展,估计有时用得着
//************************************************************//
/// <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;
}
}