I looked for some examples on the internet, but those I tried did not work. I load an image at runtime, this is working normally, but I am not able to move the image that is loaded inside a picturebox,
Follow the code:
public partial class image: Form { private ParentData 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 placed the picturebox inside Panel. I can not move the image, someone could tell me what is wrong.