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

//************************************************************//
//下面给出三个简单的方法,后面两个方法是扩展,估计有时用得着
//************************************************************//
/// <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;
}
}

转载于:https://www.cnblogs.com/codeloves/archive/2013/05/20/3088168.html

Microsoft Visual Studio 2010做的C#图片缩放实例,主要代码: Image imageSource = bitmapSource; ImageFormat sourceFormat = imageSource.RawFormat; int iSourceWidth = imageSource.Width; int iSourceHeight = imageSource.Height; decimal decTargetWidth = iSourceWidth*decTimes; decTargetWidth = Math.Round(decTargetWidth, 0); string sTargetWidth = decTargetWidth.ToString( ); int iTargetWidth = Int32.Parse(sTargetWidth); decimal decTargetHeight = iSourceHeight * decTimes; decTargetHeight = Math.Round(decTargetHeight, 0); string sTargetHeight = decTargetHeight.ToString(); int iTargetHeight = Int32.Parse(sTargetHeight); Bitmap bitmapTarget = new Bitmap(iTargetWidth, iTargetHeight); Graphics gTarget = Graphics.FromImage(bitmapTarget); gTarget.Clear(Color.Transparent); // 设置画布的描绘质量 gTarget.CompositingQuality = CompositingQuality.HighQuality; gTarget.SmoothingMode = SmoothingMode.HighQuality; gTarget.InterpolationMode = InterpolationMode.HighQualityBicubic; gTarget.DrawImage(imageSource, new Rectangle(0, 0, iTargetWidth, iTargetHeight), 0, 0, imageSource.Width, imageSource.Height, GraphicsUnit.Pixel); gTarget.Dispose(); // 以下代码为保存图片时,设置压缩质量 EncoderParameters encoderParams = new EncoderParameters(); long[] lQualityS = new long[1]; lQualityS[0] = 100; EncoderParameter EncoderParameterVar = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, lQualityS); encoderParams.Param[0] = EncoderParameterVar; imageSource.Dispose();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值