c# 图片按比例缩放
2013-03-14 20:39
453 查看
public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth) { System.Drawing.Image imgSource = b; System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat; int sW = 0, sH = 0; // 按比例缩放 int sWidth = imgSource.Width; int sHeight = imgSource.Height; if (sHeight > destHeight || sWidth > destWidth) { if ((sWidth * destHeight) > (sHeight * destWidth)) { sW = destWidth; sH = (destWidth * sHeight) / sWidth; } else { sH = destHeight; sW = (sWidth * destHeight) / sHeight; } } else { sW = sWidth; sH = sHeight; } Bitmap outBmp = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage(outBmp); g.Clear(Color.Transparent); // 设置画布的描绘质量 g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel); g.Dispose(); // 以下代码为保存图片时,设置压缩质量 EncoderParameters encoderParams = new EncoderParameters(); long[] quality = new long[1]; quality[0] = 100; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); encoderParams.Param[0] = encoderParam; imgSource.Dispose(); return outBmp; }
相关文章推荐
- 一个可以使得上传的图片大小按照指定的宽度,高度自动按比例进行缩放的函数(C#)
- 2011-09-05[C#中图片按比例缩放]
- 等比例缩放图片(C#)
- C# 中如何是PictureBox 按比例的缩放加载图片
- C# 保持图片的缩放比例
- C#图片按比例缩放的实现代码
- C# 图片等比例不丢真缩放
- C#picturebox或者Dev控件 PictureEdit 按比例的缩放加载图片
- C#picturebox或者Dev控件 PictureEdit 按比例的缩放加载图片
- ASP.NET C# 按原图片大小等比例缩放生成缩略图
- C#图片按比例缩放
- c# 图片按比例缩放
- C#picturebox或者Dev控件 PictureEdit 按比例的缩放加载图片
- 图片等比例缩放 C#
- 在c#中图片原比例缩放
- C# 鼠标滚动事件比例缩放图片
- C# PictureBox 按比例的缩放加载图片
- C# 中如何是PictureBox 按比例的缩放加载图片
- C#图片按比例缩放实例
- [DevExpress]C#picturebox或者Dev控件 PictureEdit 按比例的缩放加载图片