繁体   English   中英

在PictureBox中移动图像

[英]Move image in PictureBox

我在互联网上尝试了一些示例,但那些尝试失败的示例。 我在运行时随身携带了一个图像,它运行良好,但是我无法移动加载到图片框中的图像,

遵循代码:

public partial class imagem : Form { private CarregaDados parent = null; int width; int height;

    public imagem()
    {
        InitializeComponent();
    }       

    /// <summary>
    /// Cria uma referencia entre os form
    /// Assim conseguimos controlar os elementos do outro form
    /// </summary>
    /// <param name="_parent"></param>
    public imagem(CarregaDados _parent)
    {
      this.parent = _parent;
    }

    public void CarregaImagem(string caminho)
    {
        Image image =  Image.FromFile(caminho);            

        //valores da largura  e altura
        int width = image.Width;
        int height = image.Height;

        //verificar a largura
        //e o pixel em que deve comecar a imagem
        Rectangle rect = new Rectangle(0, 0, width, height);
        imagemPictureBox.Image = image;
    }

    //Primeiro ponto na imagem carregada
    private Point firstPoint = new Point();
    private int x = 0;
    private int y = 0;        

    private void imagemPictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        x = e.X;
        y = e.Y;        
    }

    private void imagemPictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            imagemPictureBox.Left = (imagemPictureBox.Left + e.X) - x;
            imagemPictureBox.Top = (imagemPictureBox.Top + e.Y) - y;
        }
    }

    private void imagemPictureBox_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            imagemPictureBox.Left = x;
            imagemPictureBox.Top = y;
        }
    }



}

我将图片框放在Panel中。 我无法移动图像,有人可以告诉我出了什么问题

将它们添加到设计器中

this.imagemPictureBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.imagemPictureBox_MouseMove);

this.imagemPictureBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.imagemPictureBox_MouseUp);

this.imagemPictureBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.imagemPictureBox_MouseDown);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM