views:

95

answers:

3

So here's the situation: I need to take a (user-specified) graphic, and allow the user to define and label regions within that graphic. For example, if you uploaded a picture of a face, you might want to define "right eye", "left eye", "nose" etc. Also, having defined the regions, if I select a previously defined region, it should be highlighted on the image somehow. These regions are (obviously) not necessarily rectangular, and they cannot overlap. And if you click within a defined region in the graphic, I would be able to identify which region was clicked on.

There are a couple ways I can think of for doing this, none of which are quite satisfactory. Another developer before me tried doing it with a transparent grid overlaid on the original graphic, fiddling with the background alpha/color for highlighting regions, but I think they rather kludged it. I could either get my hands really dirty trying to clean up their code, or try a completely new approach.

What would you suggest for maximum speed and user-friendliness?

Bounty added: for the best solution that will get me up and running in the minimum time.

A: 

You need polygons, saved as list of points. And you need hit testing for them. See the link:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/40ebadc1-6154-4c7c-9cb1-d608a426b29c

Dialecticus
I see there is a bounty now. Would you care to comment on my answer?
Dialecticus
+1  A: 

The GraphicsPath class is made to do this. Keep a list of them along with the image. Draw the image first, then Graphics.DrawPath() to draw the regions on top of the image.

Hit testing is simple with GraphicsPath.IsVisible(). Iterate the list in reverse order so overlaps work.

Hans Passant
OK, this answer had the most useful information for me, so you get the bounty. Thanks! :)
Shaul
@Shaul - you have to assign the bounty separate, the answer mark isn't enough.
Hans Passant
Ah, right - I hadn't awarded a bounty under the new system before. Thanks for the heads-up! :)
Shaul
+1  A: 

Assuming you haven't decided yet on the technology you'll use, I'd suggest WPF; I find most graphics-related tasks easier with WPF (at least in version 4) and it's specifically geared for interactivity, so creating non-rectangular regions using mouse clicks and hit-testing clicks to select shapes would be pretty easy. Loading images is also easy.

However, if you haven't used WPF or Silverlight until now, there is some overhead in learning the basic concepts and APIs; so I'm afraid there's no real way I can recommend it as a maximum speed solution without knowing your (or whoever's will be working on it) competencies. That said, using MVVM and WPF would be definitely the maximum speed solution for me. Also the maximum user-friendliness since WPF enables quite interesting interaction models out-of-the-box, like multi-touch support (that's the trendy one that should be mentioned, right?) and easy non-standard layout and placement of controls.

Alex Paven