如何在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;
}
}
 
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、付费专栏及课程。

余额充值