Click here to Skip to main content
15,886,258 members
Articles / Desktop Programming / Windows Forms

Cropping Images

Rate me:
Please Sign up or sign in to vote.
4.87/5 (43 votes)
5 Nov 2008CPOL2 min read 214.9K   20.2K   111  
A demo that shows how to crop images by selecting a region with the mouse.
using System.Drawing;
using System.Windows.Forms;
using gfoidl.Imaging;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		private Image _originalImage;
		private bool _selecting;
		private Rectangle _selection;
		//---------------------------------------------------------------------
		public Form1()
		{
			InitializeComponent();
		}
		//---------------------------------------------------------------------
		private void Form1_Load(object sender, System.EventArgs e)
		{
			// Save just a copy of the image on no reference!
			_originalImage = pictureBox1.Image.Clone() as Image;
		}
		//---------------------------------------------------------------------
		private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
		{
			// Starting point of the selection:
			if (e.Button == MouseButtons.Left)
			{
				_selecting = true;
				_selection = new Rectangle(new Point(e.X, e.Y), new Size());
			}
		}
		//---------------------------------------------------------------------
		private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
		{
			// Update the actual size of the selection:
			if (_selecting)
			{
				_selection.Width = e.X - _selection.X;
				_selection.Height = e.Y - _selection.Y;

				// Redraw the picturebox:
				pictureBox1.Refresh();
			}
		}
		//---------------------------------------------------------------------
		private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Left && _selecting)
			{
				// Create cropped image:
				Image img = pictureBox1.Image.Crop(_selection);

				// Fit image to the picturebox:
				pictureBox1.Image = img.Fit2PictureBox(pictureBox1);

				_selecting = false;
			}
		}
		//---------------------------------------------------------------------
		private void pictureBox1_Paint(object sender, PaintEventArgs e)
		{
			if (_selecting)
			{
				// Draw a rectangle displaying the current selection
				Pen pen = Pens.GreenYellow;
				e.Graphics.DrawRectangle(pen, _selection);
			}
		}
		//---------------------------------------------------------------------
		private void button1_Click(object sender, System.EventArgs e)
		{
			pictureBox1.Image = _originalImage.Clone() as Image;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Foidl Günther
Austria Austria
Engineer in combustion engine development.
Programming languages: C#, FORTRAN 95, Matlab

FIS-overall worldcup winner in Speedski (Downhill) 2008/09 and 2009/10.

Comments and Discussions