C#重写PictureBox控件,使控件自带缩放和鼠标拖动功能

1、新建自定义控件SnsPictureBox,拖入PictureBox控件,设置PictureBox的Anchor属性Top, Bottom, Left, Right
SizeMode为Zoom。

		private Point mouseDownPoint = new Point(); //记录拖拽过程鼠标位置
		private bool isMove = false;    //判断鼠标在picturebox上移动时,是否处于拖拽过程(鼠标左键是否按下)
		private float fScale;//图片放大倍数

2、添加滚轮事件

		public SnsPictureBox()
		{
			InitializeComponent();
			this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel);//添加滚轮事件
		}

3、鼠标滚动响应函数

		private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
		{
			if (pictureBox1.Image == null) return;
			Point mousePoint = new Point(e.X, e.Y);
			int dx = 0, dy = 0;
			if (e.Delta > 0)//鼠标向上滚动执行放大
			{
				if (pictureBox1.Height <= this.Height * 20 && pictureBox1.Width <= this.Width * 20
			                    && pictureBox1.Height < 15000 && pictureBox1.Width<15000)//图片长或者高未超过20倍视野时
				{
					fScale *= 1.5f;
					dx = (int)(mousePoint.X * (1 - 1.5f));
					dy = (int)(mousePoint.Y * (1 - 1.5f));
				}
			}
			if (e.Delta < 0)//鼠标向下滚动执行缩小
			{
				if (pictureBox1.Width / 1.5 >= this.Width || pictureBox1.Height / 1.5 >= this.Height)//缩小后图片长或者高扔大于视野时
				{
				fScale /= 1.5f;
				dx = (int)(mousePoint.X * (1.5f - 1) / 1.5f);
				dy = (int)(mousePoint.Y * (1.5f - 1) / 1.5f);
				}
				else//初始化
				{
					fScale = GetScaleLoad(this.Image);
				}
			}
			pictureBox1.Size = new Size((int)(this.Image.Width * fScale), (int)(this.Image.Height * fScale));
			SetPosition(dx, dy);
			pictureBox1.Update();
		}

4、鼠标按下

		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();
			}
			this.OnMouseDown(e);
		}

5、鼠标移动

		private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
		{
			pictureBox1.Focus();
			if (isMove)
			{
				int moveX, moveY;
				moveX = Cursor.Position.X - mouseDownPoint.X;
				moveY = Cursor.Position.Y - mouseDownPoint.Y;
				mouseDownPoint.X = Cursor.Position.X;
				mouseDownPoint.Y = Cursor.Position.Y;
				SetPosition(moveX, moveY);
			}
		}

6、鼠标松开

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

7、用到的其它函数

		private void SetPosition(int moveX, int moveY)//假设图片大小已经保证比控件大小要大
        {
            int x = pictureBox1.Location.X + moveX;
            int y = pictureBox1.Location.Y + moveY;

            if (pictureBox1.Width >= this.Width) //图片宽度大于视野
            {
                if (x > 0) x = 0;
                if (x < this.Width - pictureBox1.Width) x = this.Width - pictureBox1.Width;
            }
            else
            {
                x = (this.Width - pictureBox1.Width) / 2; //居中显示
            }

            if (pictureBox1.Height >= this.Height) //图片高度大于视野
            {
                if (y > 0) y = 0;
                if (y < this.Height - pictureBox1.Height) y = this.Height - pictureBox1.Height;
            }
            else
            {
                y = (this.Height - pictureBox1.Height) / 2;//居中显示
            }
            pictureBox1.Location = new Point(x, y);
        }

        private float GetScaleLoad(Bitmap bitmap)
        {
            if (bitmap == null) return 1.0f;
            float scale = 1.0f;
            if ((bitmap.Width / (float)this.Width) < (bitmap.Height / (float)this.Height))//说明这图片是偏向于高度的,也就是窄
            {
                scale = (this.Height * 1.0f) / bitmap.Height;
            }
            else
            {
                scale = (this.Width * 1.0f) / bitmap.Width;
            }
            return scale;
        }

        private float GetPicBoxScale(Size picBoxSize, Size imgSize)
        {
            float scale = 1.0f;
            if ((imgSize.Width / (double)picBoxSize.Width) < (imgSize.Height / (double)picBoxSize.Height))//说明这图片是偏向于高度的,也就是窄
            {
                scale = (picBoxSize.Height * 1.0f) / imgSize.Height;
            }
            else
            {
                scale = (picBoxSize.Width * 1.0f) / imgSize.Width;
            }
            return scale;
        }

项目源码下载地址

你可能感兴趣的:(PictureBox,图片浏览器,图片放大缩小,鼠标拖动,C#)