Move Image in Picturebox C# Windows Forms

Asked

Viewed 1,142 times

0

I looked for some examples on the Internet, but the ones I tried didn’t work. I load an image at runtime, this works normally, but I am not able to move the image that is loaded inside a PictureBox,

Follows the code:

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;
        }
    }



}

I put the PictureBox inside Panel. I can’t move the image, could someone tell me what’s wrong?

  • This file (in English) may be useful https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage(v=vs.80). aspx

  • Thank you, I’ll see.

  • You want to move using the mouse?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.