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();
1