当前位置: 首页 > news >正文

PictureBox内的图片拖动功能

当 PictureBox内的图片太大,超过PictureBox边框时可以用下面的方法来实现,

 

通过重绘来实现 :
 

Code
bool wselected = false;
 Point p = new Point();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
 {
 wselected = true;
 p.X = e.X;
 p.Y = e.Y;
 }
int driftX = 0, driftY = 0;
int mx = 0, my = 0;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
 {
if (wselected)
 {
 driftX = p.X - e.X;
 driftY = p.Y - e.Y;

 mx = mx - driftX;
 my = my - driftY;

 Bitmap bm = new Bitmap(this.pictureBox1.Image);

 Graphics g = pictureBox1.CreateGraphics();
 g.Clear(pictureBox1.BackColor);
 g.DrawImage(bm, mx, my);

 p.X = e.X;
 p.Y = e.Y;

 bm.Dispose();
 g.Dispose();
 }
 }

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
 {
 wselected = false;
 }

 

 

 

方法二(上面的方法在拖动的时候闪烁比较严重):

 

原文:http://topic.csdn.net/u/20090121/21/fbdc1203-7f01-4c4b-86d7-0ae2960a76e4.html

 

Code
public Form1()
{
            InitializeComponent();
            //双缓存
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.ResizeRedraw | ControlStyles.Selectable, true);
        }
 Bitmap M_map_bufferpic;//加快GDI读取用缓存图片
/** <summary>
        /// 传入内存缓存中
        /// </summary>
        /// <param name="P_str_path">图片地址 </param>
private void inPutBuffer(string P_str_path)
{
            M_map_bufferpic = new Bitmap(P_str_path);
            pictureBox1.Image = M_map_bufferpic;
        }
private void button1_Click(object sender, EventArgs e)
{
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
                string path = openFileDialog1.FileName;
                openFileDialog1.Dispose();
                inPutBuffer(path);
            }
        }

 Point M_pot_p = new Point();//原始位置
int M_int_mx = 0, M_int_my = 0;//下次能继续
int M_int_maxX, M_int_maxY;//加快读取用

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
            M_pot_p = e.Location;
            M_int_maxX = pictureBox1.Width - M_map_bufferpic.Width;
            M_int_maxY = pictureBox1.Height - M_map_bufferpic.Height;
 Cursor = Cursors.SizeAll;
 }

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//当按左键的时候
{
//算差值
M_int_mx=M_int_mx - M_pot_p.X + e.X;
 M_int_my=M_int_my - M_pot_p.Y + e.Y;
//锁定范围
M_int_mx = Math.Min(0, Math.Max(M_int_maxX, M_int_mx));
 M_int_my = Math.Min(0, Math.Max(M_int_maxY, M_int_my));

 Graphics g = pictureBox1.CreateGraphics();
 g.DrawImage(M_map_bufferpic, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height), new Rectangle(-M_int_mx, -M_int_my, pictureBox1.Width, pictureBox1.Height), GraphicsUnit.Pixel);

 M_pot_p = e.Location;
 }
else
{
 Cursor = Cursors.Default;
 }
 }

相关文章:

  • 如何做域名解析(A记录、CNAME记录、二级域名)
  • 配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler” 解决办法...
  • Financial Management
  • Drupal 集成 Question2Answer(用户整合)
  • CCNA CCNP CCIE所有实验名称完整版
  • 【翻译】Ext JS 4.1的性能
  • javascript泛型集合类(转)
  • 华为面试2:1分2分5分的硬币,组成1角,共有多少种组合。
  • HTML5 Canvas 血战消除游戏[连连看]
  • 根据进程启动时间进行kill
  • ORACLE——Instant Client配置SQL*LDR、EXP等命令工具
  • Android系统的开机画面显示过程分析(13)
  • Android HttpURLConnection应用技巧分享
  • 【WindowsAPI之MoveWindow】 C#调整目标窗体的位置、大小
  • [转载] Discrete Mathematics——11 群的概念和性质
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • 【108天】Java——《Head First Java》笔记(第1-4章)
  • 【刷算法】求1+2+3+...+n
  • 4个实用的微服务测试策略
  • CNN 在图像分割中的简史:从 R-CNN 到 Mask R-CNN
  • Codepen 每日精选(2018-3-25)
  • Git同步原始仓库到Fork仓库中
  • iOS 颜色设置看我就够了
  • JavaScript新鲜事·第5期
  • java第三方包学习之lombok
  • JWT究竟是什么呢?
  • Linux gpio口使用方法
  • Linux各目录及每个目录的详细介绍
  • PHP 小技巧
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • Webpack4 学习笔记 - 01:webpack的安装和简单配置
  • 初探 Vue 生命周期和钩子函数
  • 从零搭建Koa2 Server
  • 分享一份非常强势的Android面试题
  • 关键词挖掘技术哪家强(一)基于node.js技术开发一个关键字查询工具
  • 规范化安全开发 KOA 手脚架
  • 汉诺塔算法
  • 计算机在识别图像时“看到”了什么?
  • 讲清楚之javascript作用域
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 可能是历史上最全的CC0版权可以免费商用的图片网站
  • 理清楚Vue的结构
  • 嵌入式文件系统
  • 使用 Docker 部署 Spring Boot项目
  • 最简单的无缝轮播
  • k8s使用glusterfs实现动态持久化存储
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • ​​​​​​​Installing ROS on the Raspberry Pi
  • ​你们这样子,耽误我的工作进度怎么办?
  • "无招胜有招"nbsp;史上最全的互…
  • # include “ “ 和 # include < >两者的区别
  • (arch)linux 转换文件编码格式
  • (day 12)JavaScript学习笔记(数组3)
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕