Hellooooooo! I've one questions regarding image cropping, and I've been trying very hard to solve it since forever I hope you guys could help me out Thanks in advance!

I am using Microsoft Visual Studio C# 2010(WPF Application) and I am working on this program where user can crop any part of the image but clicking on crop button. And what I've been working on was mouse event. Firstly, I used Mouse down to determine the point where the mouse is first pressed on the image, then I use mouse move event so that user could select a rectangle using mouse and the second point of the rectangle is determined.

Then, I use mouse up event to get the point displayed as title of the program. Then when the crop button is clicked, it is supposed to crop the part selected by the rectangle. However, I faced problem coding the crop button part.

Can you guys help me out?

Attached is my source code

Point MouseDownPoint;
Rectangle selectedRectangle;

private void displayTwoSecondImageBox_MouseDown(object sender, MouseButtonEventArgs e)
{
if (!image1.IsMouseCaptured)
{
image1.CaptureMouse();
MouseDownPoint = e.GetPosition(image1);
this.Title = string.Format("X={0}, Y={1}", MouseDownPoint.X, MouseDownPoint.Y);
}
}

private void displayTwoSecondImageBox_MouseMove(object sender, MouseEventArgs e)
{
if (image1.IsMouseCaptured)
{
Point current = e.GetPosition(image1);
this.Title = string.Format("X={0}, Y={1} - x={2}, y={3}", new object[] {
MouseDownPoint.X, MouseDownPoint.Y, current.X, current.Y});
if (selectedRectangle == null)
{
selectedRectangle = new Rectangle();

selectedRectangle.Stroke = new SolidColorBrush(System.Windows.Media.Colors.White);
selectedRectangle.Opacity = 0.50;

canvas1.Children.Add(selectedRectangle);
}

double width = Math.Abs(MouseDownPoint.X - current.X);
double height = Math.Abs(MouseDownPoint.Y - current.Y);
double left = Math.Min(MouseDownPoint.X, current.X);
double top = Math.Min(MouseDownPoint.Y, current.Y);

selectedRectangle.Width = width;
selectedRectangle.Height = height;
Canvas.SetLeft(selectedRectangle, left);
Canvas.SetTop(selectedRectangle, top);

}
}

private void displayTwoSecondImageBox_MouseUp(object sender, MouseButtonEventArgs e)
{
if (image1.IsMouseCaptured)
{
image1.ReleaseMouseCapture();
// this.Title = FileName;
}
}


What should I do to crop the selected rectangle when crop button is clicked?

private void button2_click(object sender, RoutedEventArgs e)//crop button
{

}


Please help me with this! Thanks!