picturebox控件中的图片如何放大缩小

qq976542596 2015-05-12 01:13:20
picturebox控件中的图片如何放大缩小
...全文
1638 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
-小蕾- 2015-05-12
  • 打赏
  • 举报
回复
第一种方法:

/// <summary>
        /// 图片等比例缩放
        /// </summary>
        /// <param name="b"></param>
        /// <param name="destHeight"></param>
        /// <param name="destWidth"></param>
        /// <returns></returns>
        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;
        }
第二种方法:

/// <summary>  
        ///  Resize图片   
        /// </summary>  
        /// <param name="bmp">原始Bitmap </param>  
        /// <param name="newW">新的宽度</param>  
        /// <param name="newH">新的高度</param>  
        /// <returns>处理以后的图片</returns>  
        public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH)
        {
            try
            {
                Bitmap b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);
                // 插值算法的质量   
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                g.Dispose();
                return b;
            }
            catch
            {
                return null;
            }
        }  
a1295962107 2015-05-12
  • 打赏
  • 举报
回复
学习收藏了。
  • 打赏
  • 举报
回复
DrawImage你F12就知道了啊,好几个方法的
qq976542596 2015-05-12
  • 打赏
  • 举报
回复
还有其他方法吗?
  • 打赏
  • 举报
回复
设置起点,然后矩形的两个位置,你F12看DrawImage对应的参数描述就有了解了
qq976542596 2015-05-12
  • 打赏
  • 举报
回复
引用 2 楼 starfd 的回复:
等比放大缩小么?第一部分代码就是等比例 http://blog.csdn.net/starfd/article/details/44850449

             Point point = new Point(0, 0);  
            Point point2 = new Point(width, 0);  
            Point point3 = new Point(0, height);  
           Point[] destPoints = new Point[] { point, point2, point3 };  
           Rectangle rect = GetImageRectangle(fromImage);  
           graphics.DrawImage(fromImage, destPoints, rect, GraphicsUnit.Pixel);  
这里什么意思?
  • 打赏
  • 举报
回复
等比放大缩小么?第一部分代码就是等比例 http://blog.csdn.net/starfd/article/details/44850449
本拉灯 2015-05-12
  • 打赏
  • 举报
回复
这个要自定义控件,重截PictureBox

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧