如何使用该图像上的“鼠标位置”放大和缩小图像

时间:2014-06-23 06:29:32

标签: c# winforms picturebox

我在标签面板中添加了图片框。我正在尝试对其应用放大和缩小操作。实际上两个操作工作正常,但我想放大和缩小在鼠标位置工作的操作。表示在光标位置或鼠标位置放大/缩小图像。

我的代码在

下面
protected override void OnMouseWheel(MouseEventArgs e)
{
 if (_isZoomInFlag == true)
  {
   zoomInt++;
   if (zoomInt > 4)
   {
    zoomInt = 4;
   }
    zoomPicturebox();
  }
  if (_isZoomOutFlag == true)
  {
   zoomInt--;
   if (zoomInt == -3)
   {
    zoomInt = -2;
   }
  zoomPicturebox();
 }
}

public void zoomPicturebox()
{
            pbDisplay.Width = picWidth;
            pbDisplay.Height = picHeight;

            switch (zoomInt)
            {
                case -2:
                    pbDisplay.Width = Convert.ToInt32(((double)pbDisplay.Width) * 0.25);
                    pbDisplay.Height = Convert.ToInt32(((double)pbDisplay.Width) * picRatio);

                    break;
                case -1:
                    pbDisplay.Width = Convert.ToInt32(((double)pbDisplay.Width) * 0.5);
                    pbDisplay.Height = Convert.ToInt32(((double)pbDisplay.Width) * picRatio);

                    break;
                case 0:
                    pbDisplay.Width = picWidth;
                    pbDisplay.Height = picHeight;

                    break;
                case 1:
                    pbDisplay.Width = Convert.ToInt32(((double)pbDisplay.Width) * 1.1);
                    pbDisplay.Height = Convert.ToInt32(((double)pbDisplay.Width) * picRatio);

                    break;
                case 2:
                    pbDisplay.Width = Convert.ToInt32(((double)pbDisplay.Width) * 1.5);
                    pbDisplay.Height = Convert.ToInt32(((double)pbDisplay.Width) * picRatio);

                    break;
                case 3:
                    pbDisplay.Width = Convert.ToInt32(((double)pbDisplay.Width) * 1.75);
                    pbDisplay.Height = Convert.ToInt32(((double)pbDisplay.Width) * picRatio);
                   // lblZoom.Text = ("Zoom = 175%");
                    break;
                case 4:
                    pbDisplay.Width = Convert.ToInt32(((double)pbDisplay.Width) * 2);
                    pbDisplay.Height = Convert.ToInt32(((double)pbDisplay.Width) * picRatio);

                    break;
            }


            pbDisplay.Update();
        }

放大图片

enter image description here

缩小图像

enter image description here

1 个答案:

答案 0 :(得分:-1)

picturebox有MouseWheel事件,但你需要首先关注它。

public Form1()
{
    InitializeComponent();

    pictureBox1.MouseWheel += pictureBox1_MouseWheel;
}

void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
  if(e.Delta > 0)
  {
      //up
  }
  else
  {
      //down
  }
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    pictureBox1.Focus();
}

这里,在mousedown事件中,给它焦点,并使轮子事件激活。那么你可以在你的代码中完成它。