惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - Simmy.卧龙先生

Office老是弹出配置对话框,安装之类的进度的处理方法 右键点击任务栏程序没有锁定菜单 CMD命令:不是内部或者外部命令也不是可运行的程序或批处理文件 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 通道闸机 博文阅读密码验证 - 博客园 Activex、OLE、COM、OCX、DLL之间区别、联系[转] 顶级人生规划[转] 添加并删除Marker [转]C# winform与Javascript的相互调用 Token国内地铁使用城市 【人体识别系统研究】网摘 英语历年真题下载 博文阅读密码验证 - 博客园 为什么啄木鸟不会得脑震荡? 如何隐藏Excel中单元格公式且其他单元格可修改 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园
C#图像显示实现拖拽、锚点缩放功能【转】
Simmy.卧龙先生 · 2015-09-16 · via 博客园 - Simmy.卧龙先生

1.图像拖拽

核心步骤:

①新建Point类型全局变量mouseDownPoint,记录拖拽过程中鼠标位置;

②MouseDown事件记录Cursor位置;

③MouseMove事件计算移动矢量,并更新pictureBox1.Location。

代码:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint.X = Cursor.Position.X;   //记录鼠标左键按下时位置
                mouseDownPoint.Y = Cursor.Position.Y;                
                isMove = true;
                pictureBox1.Focus();    //鼠标滚轮事件(缩放时)需要picturebox有焦点
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMove = false;                
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox1.Focus();    //鼠标在picturebox上时才有焦点,此时可以缩放
            if (isMove)
            {
                int x, y;           //新的pictureBox1.Location(x,y)
                int moveX, moveY;   //X方向,Y方向移动大小。
                moveX = Cursor.Position.X - mouseDownPoint.X;
                moveY = Cursor.Position.Y - mouseDownPoint.Y;
                x = pictureBox1.Location.X + moveX;
                y = pictureBox1.Location.Y + moveY;                
                pictureBox1.Location = new Point(x, y);
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;                
            }
        }
        
        private void panel2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint.X = Cursor.Position.X;   //记录鼠标左键按下时位置
                mouseDownPoint.Y = Cursor.Position.Y;
                isMove = true;
            }
        }

        private void panel2_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMove = false;
            }
        }

        private void panel2_MouseMove(object sender, MouseEventArgs e)
        {
            panel2.Focus();  //鼠标不在picturebox上时焦点给别的控件,此时无法缩放            
            if (isMove)
            {
                int x, y;           //新的pictureBox1.Location(x,y)
                int moveX, moveY;   //X方向,Y方向移动大小。
                moveX = Cursor.Position.X - mouseDownPoint.X;
                moveY = Cursor.Position.Y - mouseDownPoint.Y;
                x = pictureBox1.Location.X + moveX;
                y = pictureBox1.Location.Y + moveY;
                pictureBox1.Location = new Point(x, y);
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
            }
        }

2.图像缩放

核心思想:利用picturebox的zoom模式,根据图像显示大小更改picturebox大小,记录鼠标位置补偿缩放位移,实现锚点缩放,即以鼠标位置为中心进行缩放。

代码:

//实现锚点缩放(以鼠标所指位置为中心缩放);
        //步骤:
        //①先改picturebox长宽,长宽改变量一样;
        //②获取缩放后picturebox中实际显示图像的长宽,这里长宽是不一样的;
        //③将picturebox的长宽设置为显示图像的长宽;
        //④补偿picturebox因缩放产生的位移,实现锚点缩放。
        //  注释:为啥要②③步?由于zoom模式的机制,把picturebox背景设为黑就知道为啥了。
        //这里需要获取zoom模式下picturebox所显示图像的大小信息,添加 using System.Reflection;
        //pictureBox1_MouseWheel事件没找到。。。手动添加,别忘在Form1.Designer.cs的“Windows 窗体设计器生成的代码”里加入:        
        //this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel)。
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            int x = e.Location.X;
            int y = e.Location.Y;
            int ow = pictureBox1.Width;
            int oh = pictureBox1.Height;            
            int VX, VY;     //因缩放产生的位移矢量
            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 < myBmp.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);
        }