Click here to Skip to main content
15,797,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All

I have an Image viewer that displays an image. I want to draw a rectangle using the mouse over the image and get the x and y coordinates of the rectangle (X1, X2, Y1, and Y2). I will use these coordinates to create a search region and find the max and min values in an array that have the exact number of pixels as the image in both axes.
Can anyone guide me to a direction to start please?
Updated 5-Jan-12 18:55pm
Sergey Alexandrovich Kryukov 6-Jan-12 1:44am    
So, what's the problem? Just do it.

I reported this question as "Unclear or Incomplete" and asked you: so, what's the problem? Just do it.

There is one detail in the question that makes me think this is not so easy for you: overlay. Probably, you had some experience working with DirectX, that's why you think this is difficult. Maybe you tried to combine GDI rendering with DirectX, which is really pretty difficult. Not with WPF, which is based on DirectX and has nothing to do with GDI. Everything is implemented transparently; in other words, for you as a WPF developers there are no "overlays". You can place any shape on top of anything: bitmap, even video clip, and all parts will be rendered correctly.

All you need is using the class System.Windows.Shapes.Rectangle,[^].

Alternatively, you can put both image and rectangle on the System.Windows.Controls.Canvas ([^]), which makes it easy to manipulate shape and image coordinates using attached properties Top, Left… During run time, this is done by the calls to Canvas.SetLeft(element, left), Canvas.SetTop(element, top),[^].

Naturally, you will need to handle mouse/keyboard events to manipulate position and size of the rectangle during run time.

Share this answer
Thanks for the pointers and help:
Here is my finished code and it works. You place the mouse anywhere on the canvas hold mouse down and drag to create the rectangle. It could use some more improvement to drag and create the rectangle in any direction.

<Canvas Name="ImageCanvas"
           Height="240" Width="320"
           Margin="87,514,0,0"  VerticalAlignment="Top" MouseLeftButtonUp="ImageCanvas_MouseLeftButtonUp">
           <ImageBrush x:Name="Image" Stretch="Uniform" ImageSource="C:\image.bmp">
           <Rectangle x:Name="ROI" Stroke="#FFF1133E"  Width="20" Height="20" Canvas.Left="155" Canvas.Top="115" />



double topLeftX = 0;
double topLeftY = 0;
double bottomRightX = 0;
double bottomrigthY = 0;
bool setRect = false;

private void ImageCanvas_MouseDown(object sender, MouseButtonEventArgs e)
    topLeftY = topLeftX = bottomrigthY = bottomRightX = 0;
    setRect = true;
    System.Windows.Point pt = e.MouseDevice.GetPosition(sender as Canvas);
    topLeftX = pt.X; topLeftY = pt.Y;

private void ImageCanvas_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
    if (setRect == true)
        //get mouse location relative to the canvas
        System.Windows.Point pt = e.MouseDevice.GetPosition(sender as Canvas);
        Canvas.SetLeft(ROI, topLeftX);
        Canvas.SetTop(ROI, topLeftY);
        ROI.Width = System.Math.Abs((int)(pt.X - topLeftX));
        ROI.Height = System.Math.Abs((int)(pt.Y - topLeftY));
        commandReturnTB.Text = (Convert.ToString(pt.X) + "," + Convert.ToString(pt.Y))+"\n";  

private void ImageCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    System.Windows.Point pt = e.MouseDevice.GetPosition(sender as Canvas);
    bottomRightX = pt.X;
    bottomrigthY = pt.Y;
    ROI.Width = System.Math.Abs((int)(bottomRightX - topLeftX));
    ROI.Height = System.Math.Abs((int)(bottomrigthY - topLeftY));
    Canvas.SetLeft(ROI, topLeftX);
    Canvas.SetTop(ROI, topLeftY);
    setRect = false;
    commandReturnTB.Text = topLeftX + "," + topLeftY + "--" + bottomRightX + "," + bottomrigthY;
Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900