您当前的位置: 首页 >  c#

寒冰屋

暂无认证

  • 1浏览

    0关注

    2286博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

c# -- 图片缩放方法总结

寒冰屋 发布时间:2018-08-18 00:01:31 ,浏览量:1

方法一(其实也可以扩大,但是估计会失真,没有尝试···)

        ///  
        /// 生成缩略图重载方法1,返回缩略图的Image对象 
        ///  
        /// 缩略图的宽度 
        /// 缩略图的高度 
        /// 缩略图的Image对象 
        private Image GetReducedImage(Image resourceImage, int width, int height)
        {
            try
            {
                Image data = null;
                //用指定的大小和格式初始化Bitmap类的新实例 
                using (Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb))
                {
                    //从指定的Image对象创建新Graphics对象 
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        //清除整个绘图面并以透明背景色填充 
                        //graphics.Clear(Color.Transparent);
                        //在指定位置并且按指定大小绘制原图片对象 
                        graphics.DrawImage(resourceImage, new Rectangle(0, 0, width, height));
                    }
                    data = new Bitmap(bitmap);
                }
                return data;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

方法二(.net自带缩略图方法)

System.Drawing.Image sbmp = System.Drawing.Image.FromFile(filename);

System.Drawing.Image sbmpthum = sbmp.GetThumbnailImage(300, 300, () => { return false; }, IntPtr.Zero);

只有两句(需要方法自行封装),.net自带的,可以具体查看方法定义(F12),这里不做具体说明

关注
打赏
1665926880
查看更多评论
立即登录/注册

微信扫码登录

0.0472s