C# PictureBox控件 通过鼠标滚轮对图片进行缩放(还需优化)


在控件中没有直接的鼠标滚轮事件,所以我们要手动添加鼠标滚轮事件。

窗口设置

在这里插入图片描述

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;

namespace TestPictureBox
{
    public partial class Form1 : Form
    {
        HTuple window = new HTuple();

        public Form1()
        {
            InitializeComponent();
            MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
            HOperatorSet.OpenWindow(0,0,pictureBox1.Width,pictureBox1.Height,pictureBox1.Handle,"","",out window);
            HDevWindowStack.Push(window);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string imagepath = @"D:\Pictures\Camera Roll\01.jpg";
            HOperatorSet.ReadImage(out HObject image,imagepath);
            HOperatorSet.GetImageSize(image,out HTuple image_width,out HTuple image_height);
            HOperatorSet.SetPart(window,0,0,image_height-1,image_width-1);
            HOperatorSet.DispObj(image,window);
        }
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)//鼠标滚轮事件
        {
            //double step = 1.2;//缩放倍率
            
            //if (e.Delta > 0)
            //{
            //    if (pictureBox1.Height >= Screen.PrimaryScreen.Bounds.Height * 100)
            //        return;
            //    pictureBox1.Height = (int)(pictureBox1.Height * step);
            //    pictureBox1.Width = (int)(pictureBox1.Width * step);

            //    int px = Cursor.Position.X - pictureBox1.Location.X;
            //    int py = Cursor.Position.Y - pictureBox1.Location.Y;
            //    int px_add = (int)(px * (step - 1.0));
            //    int py_add = (int)(py * (step - 1.0));
            //    pictureBox1.Location = new Point(pictureBox1.Location.X - px_add, pictureBox1.Location.Y - py_add);
            //    Application.DoEvents();
            //}
            //else
            //{
            //    if (pictureBox1.Height <= Screen.PrimaryScreen.Bounds.Height)
            //        return;
            //    pictureBox1.Height = (int)(pictureBox1.Height / step);
            //    pictureBox1.Width = (int)(pictureBox1.Width / step);

            //    int px = Cursor.Position.X - pictureBox1.Location.X;
            //    int py = Cursor.Position.Y - pictureBox1.Location.Y;
            //    int px_add = (int)(px * (1.0 - 1.0 / step));
            //    int py_add = (int)(py * (1.0 - 1.0 / step));
            //    pictureBox1.Location = new Point(pictureBox1.Location.X + px_add, pictureBox1.Location.Y + py_add);
            //    Application.DoEvents();
            //}
            int x = e.Location.X;
            int y = e.Location.Y;
            int ow = pictureBox1.Width;
            int oh = pictureBox1.Height;
            int VX, VY; //因缩放产生的位移矢量
            int zoomStep = 20;
            if (e.Delta > 0) //放大
            {
                //第①步
                pictureBox1.Width += zoomStep;
                pictureBox1.Height += zoomStep;
                //第②步
                PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
                Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
                //第③步
                pictureBox1.Width = rect.Width;
                pictureBox1.Height = rect.Height;
            }
            if (e.Delta < 0) //缩小
            {
                //防止一直缩成负值
                if (pictureBox1.Width < pictureBox1.Width / 10)
                    return;

                pictureBox1.Width -= zoomStep;
                pictureBox1.Height -= zoomStep;
                PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |  BindingFlags.NonPublic);
                Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
                pictureBox1.Width = rect.Width;
                pictureBox1.Height = rect.Height;
            }
            //第④步,求因缩放产生的位移,进行补偿,实现锚点缩放的效果
            VX = (int)((double)x * (ow - pictureBox1.Width) / ow);
            VY = (int)((double)y * (oh - pictureBox1.Height) / oh);
            pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);
        }
    }
}

原图效果

在这里插入图片描述

缩小效果

在这里插入图片描述

放大效果

在这里插入图片描述

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值