title: Move rectangle with mouse using translate/transform
body: <p>I need to make a graph of lines from a csv file and draw it with GDI got a Windows form but now I want to change the scale and move.</p>
  
  <p>I started with something more simple as moving a rectangle with the mouse but to start drawing the rectangle always from the origin.
  Can anyone help?</p>
  
  <p>My code is:</p>
  
  <pre><code>public partial class Form1 : Form {
      int origenX;
      int origenY;
  
      bool transformar = false;
  
  
      public Form1() {
          InitializeComponent();
      }
  
      private void Form1_Paint(object sender, PaintEventArgs e) {
          dibujar(e.Graphics);
      }
  
      private void Form1_MouseMove(object sender, MouseEventArgs e) {
          int deltaX = e.X - origenX;
          int deltaY = e.Y - origenY;
  
          if (transformar) {
              System.Drawing.Graphics g = this.CreateGraphics();
              Matrix mAux = new Matrix();
              mAux.Translate(deltaX, deltaY);
              g.Transform = mAux;
              dibujar(g);
              g.Dispose();
          }
          this.Text = "x=" + deltaX.ToString() + ", y=" + deltaY.ToString(); 
      }
  
      private void Form1_MouseDown(object sender, MouseEventArgs e) {
          transformar = true;
          origenX = e.X;
          origenY = e.Y;
      }
  
      private void Form1_MouseUp(object sender, MouseEventArgs e) {
          transformar = false;
      }
  
      private void dibujar(System.Drawing.Graphics g) {
          g.Clear(Color.White);
          g.DrawEllipse(new Pen(Color.Blue, 2), new Rectangle(50, 50, 50, 50));
      }
  }
  </code></pre>