m0_59286135 2021-12-21 13:21 采纳率: 87.5%
浏览 259
已结题

C#怎么用滚轮实现picturebox里的图片的放大和缩小

比如滚轮向前推是缩小,向后推是放大,类似这样的。
是picturebox里图片的放大的缩小,不是控件放大和缩小

  • 写回答

2条回答 默认 最新

  • Ival 2021-12-21 14:31
    关注

    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

        private Image sourceImage = null;
        private int imageWidth = 0;
        private int imageHeight = 0;
        private float zoom = 1;
        private const float reduce = 0.1f;
        private const float enlarge = 1f;
    
        private void Form1_Load(object sender, EventArgs e)
        {
            sourceImage = ZoomImage.Properties.Resources._123456;
            imageWidth = sourceImage.Width;
            imageHeight = sourceImage.Height;
            pictureBox1.MouseWheel += pictureBox1_MouseWheel;
        }
    
        private 
        void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                if (zoom > 1)
                {
                    zoom -= enlarge;
                }
                else
                {
                    zoom -= reduce;
                }
                if (zoom >= 10)
                {
                    zoom = 10;
                }
                if (zoom <= 0)
                {
                    zoom = 0.1f;
                }
            }
            else
            {
                if (zoom > 1)
                {
                    zoom += enlarge;
                }
                else
                {
                    zoom += reduce;
                }
                if (zoom >= 10)
                {
                    zoom = 10;
                }
                if (zoom <= 0)
                {
                    zoom = 0.1f;
                }
            }
    
            pictureBox1.Image = ZoomImageMethod((Image)ZoomImage.Properties.Resources._123456, Convert.ToInt32(Math.Ceiling(imageWidth * zoom)), Convert.ToInt32(Math.Ceiling(imageHeight * zoom)));
        }
    
        private Image ZoomImageMethod(Image bitmap, int destHeight, int destWidth)
        {
            try
            {
                System.Drawing.Image sourImage = bitmap;
                int width = 0, height = 0;
                //按比例缩放             
                int sourWidth = sourImage.Width;
                int sourHeight = sourImage.Height;
                if (sourHeight > destHeight || sourWidth > destWidth)
                {
                    if (sourHeight / (destHeight * 1.0) > sourWidth / (destWidth * 1.0))
                    {
                        height = destHeight;
                        width = Convert.ToInt32(Math.Floor(sourWidth * destHeight / (sourHeight * 1.0)));
                    }
                    else
                    {
                        width = destWidth;
                        height = Convert.ToInt32(Math.Floor(sourHeight * destWidth / (sourWidth * 1.0)));
                    }
                }
                else
                {
                    width = sourWidth;
                    height = sourHeight;
                }
                Bitmap destBitmap = new Bitmap(destWidth, destHeight);
                Graphics g = Graphics.FromImage(destBitmap);
                g.Clear(Color.White);
                //设置画布的描绘质量           
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(sourImage, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
                g.Dispose();
                //设置压缩质量       
                System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
                long[] quality = new long[1];
                quality[0] = 100;
                System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                encoderParams.Param[0] = encoderParam;
                sourImage.Dispose();
                return destBitmap;
            }
            catch
            {
                return bitmap;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月28日
  • 已采纳回答 9月20日
  • 创建了问题 12月21日

悬赏问题

  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加