Click here to Skip to main content
15,847,653 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have marked multiple areas on an image and stored its coordinates in an array. Now i want to show the regions I have marked on the image on button click.
For eg. I have marked 5 regions on the image, I want to show all the 5 regions marked at the same time on the image on button click. As for now,I am just getting the 5th region marked.
I can't post the code , as it is the part of a project, thus containing many other things.
Thanks in advance.
gggustafson 8-Dec-13 17:18pm    
When you say "region" are you referring to the GDI Region object?
mg9893 8-Dec-13 17:34pm    
I have used mouse events and paint event to draw a rectangle to extract an area from image. I have upper left and lower right coordinates of the rectangle in an array, selecting an area in the image. Sorry I am new to this field so can't say it in its specific terms used for it.
gggustafson 8-Dec-13 17:53pm    
From what I understand, you are "rubber banding" areas of you image. That is you position the mouse at some point and then holding down the mouse button move the mouse to some position where you release the mouse pointer. This is rubber-banding. The effect could be enhanced by drawing a rectangle as the mouse moves (thus stretching a rubber band). The end result is a rectangle defined by the mousedown and the mouseup positions.

If you are just clicking on two distinct positions, then you would also define a rectangle.

Which one are you using?
mg9893 8-Dec-13 17:57pm    
I am doing rubber banding.
gggustafson 8-Dec-13 19:24pm    
Are you drawing the rectangle as the mouse moves?

1 solution

Personally, I'd use a List of Rectangles (rectangles) to save the "regions". The other two variables are used to create (new_rectangle) and draw (path_rectangles) the rectangles.

Rectangle                 new_rectangle = new Rectangle ( );
Rectangle [ ]             path_rectangles;
List <Rectangle>          rectangles = new List < Rectangle > ( );

Your MouseDown event handler must populate the upper left corner of new_rectangle.

new_rectangle.X = e.X;
new_rectangle.Y = e.Y;
new_rectangle.Width = 0;
new_rectangle.Height = 0;

Your MouseUp event handler must populate the width and height of new_rectangle and also add the new_rectangle to the List of Rectangles.

new_rectangle.Width = Math.Abs ( new_rectangle.X - e.X );
new_rectangle.Height = Math.Abs ( new_rectangle.Y - e.Y );
rectangles.Add ( new_rectangle );

The button click event handler would then convert the List of rectangles into an array of rectangles and then invoke the Invalidate method.

path_rectangles = rectangles.ToArray ( );
this.Invalidate ( );

Finally, your OnPaint event handler would look something like:

protected override void OnPaint ( PaintEventArgs e )
    GraphicsPath path;

    base.OnPaint ( e );

    path = new GraphicsPath ( );
    path.AddRectangles ( path_rectangles );
    e.Graphics.DrawPath ( Pens.Black, path );

I point out that the GraphicsPath object can be composed of polygons so your lines need not necessarily form rectangles.

Note that you must solve the problem of the initial point being below or to the right of the final point. The solution that I give does not consider this.
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