生成缩略图

方法一:直接读取图片文件

 

/// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="localPath">原图路径</param>
        /// <param name="toWidth">缩略后的宽</param>
        /// <param name="toHeight">高</param>
        /// <returns></returns>
        private Image MakeThumbnail(string localPath, int toWidth, int toHeight)
        {
            if (!File.Exists(localPath))
                return null; // 可以返回一个特定的图片

            Image originalImage = Image.FromFile(localPath);

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            //指定高宽裁减(不变形)               
            if ((double)originalImage.Width / (double)originalImage.Height > (double)toWidth / (double)toHeight)
            {
                oh = originalImage.Height;
                ow = originalImage.Height * toWidth / toHeight;
                y = 0;
                x = (originalImage.Width - ow) / 2;
            }
            else
            {
                ow = originalImage.Width;
                oh = originalImage.Width * toHeight / toWidth;
                x = 0;
                y = (originalImage.Height - oh) / 2;
            }

            //新建一个bmp图片
            Image bitmap = new Bitmap(toWidth, toHeight);

            //新建一个画板
            Graphics g = Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = InterpolationMode.High;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充
            g.Clear(Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new Rectangle(0, 0, toWidth, toHeight),
                new Rectangle(x, y, ow, oh),
                GraphicsUnit.Pixel);

            originalImage.Dispose();
            g.Dispose();
            return bitmap;
        }

 

 

 

方法二:读取文件到内存中

/// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="toWidth">缩略后的宽</param>
        /// <param name="toHeight">高</param>
        /// <param name="originalImage">原始图片</param>
        /// <returns></returns>
        private Image MakeThumbnail(string localPath, int toWidth, int toHeight)
        {
            Image originalImage = ReadPictureStream(localPath);
            if (originalImage == null)
                return null;// 可以返回一个特定的图片

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            //指定高宽裁减(不变形)               
            if ((double)originalImage.Width / (double)originalImage.Height > (double)toWidth / (double)toHeight)
            {
                oh = originalImage.Height;
                ow = originalImage.Height * toWidth / toHeight;
                y = 0;
                x = (originalImage.Width - ow) / 2;
            }
            else
            {
                ow = originalImage.Width;
                oh = originalImage.Width * toHeight / toWidth;
                x = 0;
                y = (originalImage.Height - oh) / 2;
            }

            //新建一个bmp图片
            Image bitmap = new Bitmap(toWidth, toHeight);

            //新建一个画板
            Graphics g = Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = InterpolationMode.High;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充
            g.Clear(Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new Rectangle(0, 0, toWidth, toHeight),
                new Rectangle(x, y, ow, oh),
                GraphicsUnit.Pixel);

            originalImage.Dispose();
            g.Dispose();
            return bitmap;
        }

        // 读取图片存放内存中并返回一个图片对象
        private Image ReadPictureStream(string localPath)
        {
            if (!File.Exists(localPath))
                return null;
            Image image = null;
            FileInfo fileinfo = new FileInfo(localPath);
            using (FileStream imgdatastream = System.IO.File.Open(localPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite))
            {
                int imgdatalen = int.Parse(fileinfo.Length.ToString());   //定义每次读取字节的长度  

                byte[] m_ImgData = new byte[imgdatalen];

                //获取图片的字节数  
                imgdatastream.Read(m_ImgData, 0, imgdatalen);

                //压缩指定路径下的图片并预览  
                using (MemoryStream MemStream = new System.IO.MemoryStream(m_ImgData))
                {
                    image = Image.FromStream(MemStream);
                }
            }
            return image;
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值