原比例生成缩略图实例
public Image GetThumb(int w,int h,Image source) { try { double zoom=1; if (source.Size.Height >= source.Size.Width) { if (source.Size.Height > 70) { zoom = source.Size.Height / 70; w = Convert.ToInt32(source.Size.Width / (zoom)); } else { w = source.Size.Width; h = source.Size.Height; } } else { if (source.Size.Width > 70) { zoom = source.Size.Width/70; h = Convert.ToInt32(source.Size.Height / (zoom)); } else { w = source.Size.Width; h = source.Size.Height; } } Bitmap bmp = new Bitmap(70, 70); Bitmap oldBmp = new Bitmap(source); // 加载原图片 Bitmap newbmp = new Bitmap(w, h); // 创建新图片 Graphics grap = Graphics.FromImage(newbmp); // 绑定画板 // 原图片的开始绘制位置,及宽和高 (控制Rectangle的组成参数,便可实现对图片的剪切) Rectangle oldRect = new Rectangle(0, 0, oldBmp.Width, oldBmp.Height); // 绘制在新画板中的位置,及宽和高 (在这里是完全填充) Rectangle newRect = new Rectangle(0, 0, w, h); // 指定新图片的画面质量 grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; //grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //grap.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; //grap.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; // 把原图片指定位置的图像绘制到新画板中 grap.DrawImage(oldBmp, newRect, oldRect, GraphicsUnit.Pixel); Graphics g = Graphics.FromImage(bmp); Rectangle minRec = new Rectangle(0, 0, newbmp.Width, newbmp.Height); // 绘制在新画板中的位置,及宽和高 (在这里是完全填充) Rectangle panleRec = new Rectangle((70-newbmp.Width)/2, (70-newbmp.Height)/2, 70, 70); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; //g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(newbmp, panleRec, minRec, GraphicsUnit.Pixel); //float startpw = (70 - w) / 2 / newbmp.HorizontalResolution * dpi_x; //float startph = (70 - h) / 2 / newbmp.VerticalResolution * dpi_y; //float pw = w / newbmp.HorizontalResolution * dpi_x; //float ph = h / newbmp.VerticalResolution * dpi_y; //g.DrawImage(newbmp, startpw, startph, pw, ph); g.Dispose(); grap.Dispose(); source.Dispose(); return bmp; } catch (Exception ex) { ErrHandle.ErrHandler(ex.ToString(), ErrType.Info); return null; } }
本文来自博客园,作者:NLazyo,转载请注明原文链接:https://www.cnblogs.com/bile/p/3304764.html