随笔 - 272  文章 - 7  评论 - 27  阅读 - 83万

原比例生成缩略图实例

复制代码
        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;
            }
            
        } 
复制代码

 

posted on   NLazyo  阅读(370)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示