How to select, display and save regions of a graphic? - c#

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.

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.

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.

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

Related

WPF or WinForms - Draw a complex User Interface

I want to build an App with a special UserInterface.
The idea looks like this image:
The big circle in the Middle is the StartPoint. The segments in the outer Circle are options the user can select. But to select an option the outer circle can be rotate and the selection is highlight or expanded on the right side of the inner Circle. But the outer cirlce is only level 1, I built more than one selection and for each selected option there comes the next outer circle with new Options.
Is this even possible in WPF or WinForms?
Regards
Chris
Pixels are pixels: you can do it in both ways.
The real question is: what platform offers me the best advantages in terms of simplicity/flexibility of development?
My two-cents answer is: WPF. No doubt.
That's because your goal is dealing with geometries, rotations, animations (maybe). WPF relies on a vector-based approach, which solves better problems like yours, although it might be a bit complex to deal with if you never had an experience.
Absolutely WPF. I've made simple games and physics simulations in WPF using transformations and rotations, and WPF makes it fairly painless.
Since you will be adding a lot of those items programatically, Winforms is based around the designer to add elements, so it may not give you the flexibility you would like, whereas WPF is better and more intuitive with run-time changes.

Slow drawing when using many controls in C#

I've been trying to create a roll-playing game for a little over a year now, and I thought a good way to test it and flesh it out a bit would be to turn it into a Dungeons and Dragons variant. However, the inherent problem with using a DnD setup is that there's a lot to keep track of, to the point where it becomes impossible for a single person to do by themselves. As such, I thought of writing a C# program to help.
I ran into this problem when I tried using a series of PictureBox objects to represent the spaces on the map where characters can move. I wanted to use PictureBoxes because that would allow me to use images to represent terrain and characters occupying the given space. However, the map I am using is roughly 46 x 75 1-inch squares, totaling 3450 PictureBoxes. This, understandably, slows the application down so much that it actually freezes for minutes at a time while it redraws the map every so often.
I tried two solutions. First, I tried using a Panel object for its free scrolling capabilities in the hopes that the application wouldn't have to redraw the whole map, but rather only the subset of PictureBoxes visible at that time. This helped only a little, and not enough to make the application usable. Second, I looked online, including this forum, and found people having similar problems. However, the problems they were having were entirely unrelated to mine (aside from the whole slow redraw thing) and thus the solutions didn't really apply to me. I did see a few recurring lines that I tried:
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
I'm not really sure what they do, but they did help a bit. I am still seeing way too much lag, though.
I'm out of ideas at the moment. Is there a better control I could use or a different way of drawing? Thanks in advance for your help!
I would try to develop a solution that only renders the images that are visible on the screen at any given time. Images should only be rendered when you scroll them into focus.
This is a common technique since rendering UI is usually the slowest operation by far. I would bind a view model object to the visible objects, which means you only have to render a subset of the images instead of the entire screen.
Grids with endless scroll sometimes use this approach.

Rotate Windows form upside down

I have a C# application that has an existing WinForm that I now need to display upside down.
The application will be displayed on a touchscreen Windows 7 device. If two people are using the device, one person is viewing it right-side-up while another user will be simultaneously viewing it upside-down. I will need to have one control displayed right-side-up while another control is displayed upside-down, each duplicate forms. Both need to be functional. It is not necessary for the title bar and Windows close, maximize, and minimize to be rotated.
Is there a way to easily rotate this Form and all of its contents without having to rewrite it from scratch?
Unfortunately, rotating controls is not directly possible in WinForms.
At least, not if you want them to retain their functionality. It would be relatively simple to draw the control into a bitmap, rotate the bitmap, and then draw that back to the desired location on the form. But you would obviously lose the ability to interact with the controls. They would just be static representatives of their original selves.
But making functional upside-down controls just isn't going to happen. I mean, you could try to write a bunch of custom drawing code for owner-drawn controls, but you'll still run into a bunch of bugs, corner cases, and compatibility problems. The Win32 controls that WinForms is based on just don't support this. No big surprise, really, considering they were invented some 20–25 years before anyone thought of computer screens that you could carry around in your pocket and rotate in any direction. There is a good reason that UI technologies like WPF came out around the time that touch screens and tablets did.
There are some possibilities that can be explored when it comes to flipping the entire screen, but that's not going to help when you want different controls going different directions. (And I guess it betrays my vantage point as a desktop app guy when I say this, but that just sounds like an incredibly confusing UI.)
If you absolutely have to have this, someone else is going to have to give you another route to hack it, perhaps along the lines of Dhawalk's comment: hosting the WinForms control inside of a WPF app that does provide built-in support for rotated controls. I don't know enough about this to make any concrete suggestions down that path. From a few minutes of searching, it appears that WindowsFormsHost does not actually support rotation transforms, so this may be a non-starter anyway.

Displaying a map using WPF

I am new to WPF, and relatively new to GUI coding, so bear with me if this seems obvious to some.
I am trying to display a randomly generated map using WPF/C#, in order to simulate the behaviour of a propagating RF signal. Each map coordinate can have zero or more items within it - it could be buildings, vehicles, fauna or geographical data (none indicates flat grassy fields). I would like to display this in a window, or on a page to the user. But I don't know how.
My original thought was a canvas with a pre-defined method for drawing items. But the map will be massive, and I only want to display a small portion of the map to the user at any one time.
So, what I want to know is, can I create a canvas, draw what I want onto it, and then display only a small portion of that to the user? If not, or if that is not simple, is there a better way of doing it? I don't necessarily want a specific answer either - a more generic solution would be better (i.e. tell me I need to use a "Blah" with a "Blah blah" would be better than writing out the whole code - I like to learn about these things as much as possible by doing, not copy and paste).
Thanks.
David
Here some ideas:
You can try to paint your objects directly via the GraphicsContext. Here you find an overview.
Also check out CompositionTarget.Rendering, it allows you also to render per frame (as far as possible).
If it is graphically intensive, perhaps it's also a good idea to look into the XNA-framework.

Creating SPECIFIC Dynamic Visuals - House Plan w/ Objects (FOLLOW-UP)

Yesterday, I asked a question about how to dynamically render something in .NET: specifically, I asked about how to create a white "canvas" for "drawing"/rendering upon, what framework to use, etc. However, many of the answers suggested for me to ask a more specific answer, so right now, I have come up with a completely hypothetical (and random) example of what I want to accomplish.
The example is as follows: let's assume that I'm trying to create an app where the user can first draw some rectangles (by clicking or entering dimensions) to create the outlines of their house. Next, they would be able to draw small rectangles inside to signify objects inside the house. Also, they would be able to pan or zoom. (Don't forget, this is a completely random example.)
So, if I were to try to do this, I guess I have the following questions:
How do I create the white "canvas" for drawing everything upon? Some answers to my first question suggested using WPF, and I want to try that. Is there a specific control I need to add to the XAML to allow me to render such dynamic stuff on top of it?
How can I draw/paint figures like these onto the canvas, and how can I change this during runtime (panning/zoom)?
Do I need to create a simple coordinate plane for referencing and storing all the objects and their locations inside the "house"? If so, how?
I hope this isn't too general still, as I think that using such an example would allow people to effectively answer this question. Thanks!
HTML makes a great blank canvas.
Many games utilize OpenGL or DirectX for more advanced (eg. panning/zooming) features.
Unity is a pretty decent platform for such things from what I can gather about it.

Categories

Resources