图像从picturebox1到picturebox2

时间:2014-06-11 12:31:34

标签: c# image drag-and-drop picturebox move

我制作了一个带有2个图片盒的1个表格的脚本,直到这里一切都很好。

如果你执行下面的代码,你可以看到你可以移动picturebox1并将其放在picturebox2中。现在我希望丢弃的picturebox1可以调整大小,旋转并在picturebox2内移动(一旦由客户端执行)。 我环顾四周但找不到这个问题的答案。任何帮助,我将不胜感激,谢谢

以下是代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    int x = 0;
    int y = 0;
    bool drag = false;
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
        x = e.X;
        y = e.Y;
        drag = true;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (drag)
        {
            //position new get
            pictureBox1.Top += e.Y - y;
            pictureBox1.Left += e.X - x;
        }
    }

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

    private void pictureBox2_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.All;
    }

    private void pictureBox2_DragDrop(object sender, DragEventArgs e)
    {
        pictureBox2.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        pictureBox2.AllowDrop = true;
    }
}

}

1 个答案:

答案 0 :(得分:0)

为了调整图片框的大小,您可以定义以下方法:

  private void IncreaseSize(PictureBox p,int dt)
        {
            Size size = p.Size;
            size.Height = size.Height + dt;
            size.Width=size.Width + dt;
            p.Size = size;

        }

        private void DecreaseSize(PictureBox p, int dt)
        {
            Size size = p.Size;
            size.Height = size.Height - dt;
            size.Width = size.Width - dt;
            p.Size = size;
        }

这些方法可以调用您在主窗体中决定的事件,例如:

  private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
          IncreaseSize(pictureBox1,5);
        }

 private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
          DecreaseSize(pictureBox2, 10);
        }