Skip to content

Instantly share code, notes, and snippets.

Created Oct 28, 2016

What would you like to do?
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 DragSelectBox
public partial class Form1 : Form
public bool leftMouseButtonIsDown;
public Point leftMouseButtonDownLocation = Point.Empty;
public Point currentMouseLocation = Point.Empty;
public Form1()
pictureBox1.BackgroundImage = Image.FromFile(@"C:\Users\chris\Desktop\post\backgroundimage.jpg");
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
leftMouseButtonIsDown = true;
leftMouseButtonDownLocation = currentMouseLocation = e.Location;
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
leftMouseButtonIsDown = false;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
currentMouseLocation = e.Location;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
if (leftMouseButtonIsDown)
Rectangle window = new Rectangle(
Math.Min(leftMouseButtonDownLocation.X, currentMouseLocation.X),
Math.Min(leftMouseButtonDownLocation.Y, currentMouseLocation.Y),
Math.Abs(leftMouseButtonDownLocation.X - currentMouseLocation.X),
Math.Abs(leftMouseButtonDownLocation.Y - currentMouseLocation.Y));
Graphics G = e.Graphics;
Pen pen = new Pen(Color.Red, 2);
G.DrawRectangle(pen, window);

This comment has been minimized.

Copy link

commented Mar 12, 2019

Hello can i move red regtangle?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.