List of Exercises

Picturebox Move Image With Mouse in C#

	
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Picturebox_Move_Image_With_Mouse
{
  public partial class Form1 : Form
  {
    bool Dragging;
    int xPos;
    int yPos;
    public Form1()
    {
      InitializeComponent();
    }

    private void selectIm_Click(object sender, EventArgs e)
    {
      OpenFileDialog opf = new OpenFileDialog();
      if (opf.ShowDialog() == DialogResult.OK)
      {
        pictureBox1.Image = Image.FromFile(opf.FileName);
        pictureBox1.Cursor = Cursors.Hand;
      }
    }
    private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { Dragging = false; }
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
      if (e.Button == MouseButtons.Left)
      {
        Dragging = true;
        xPos = e.X;
        yPos = e.Y;
      }
    }
    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
      Control c = sender as Control;
      if (Dragging && c != null)
      {
        c.Top = e.Y + c.Top - yPos;
        c.Left = e.X + c.Left - xPos;
      }
    }
  }
}

	


Download the project of Visual Studio 2013 in DropBox Download


C# Picturebox || Move and Drag Image With Mouse


Video Thumbnail