Which control should I use for 2D drawing with WPF? - c#

I want to programatically draw a few simple 2D shapes in a WPF application. Which control should I use for that? I've heard that Canvas can do that but that it's mostly designed to be a container for other controls and not a "drawing" canvas.

Those other controls could be shapes like rectangles, lines, etc., so Canvas may be a good start. If you want to render directly through a function, you could basically use any (user)control as base class, though.

Canvas is meant for drawing, obviously. What is your concern ? if it is Speed, you should have a look on Windows XNA.
Maybe Direct X, Open GL or any library providing access to them could be of some use. Give more details on your needs for an answer.

Related

Drawing custom Editor controls in Unity

Sometimes a Unity project has data that demands more than ordinary inspector fields and we want to create more sophisticated tools to edit the data. For example, here's a blog post about creating a node editor: Creating a Node Based Editor in Unity
It's useful to be able to create node editors, but that project draws nothing but boxes and lines and curves using the tools in GUI and Handles, which is fine for what it is, but what if we need to draw something not supplied by Handles?
For example, if we want to draw an elaborate mesh to represent some data that we want to be able to edit, it seems not ideal to render each individual polygon of the mesh using Handles.DrawAAConvexPolygon(...). Shouldn't we instead have a way to more directly send the mesh to be rendered? Or is DrawAAConvexPolygon exactly what we should be doing?
Is the GL class the appropriate approach when wanting to draw arbitrary meshes in an editor control? It is certainly capable of drawing, but is it bad practice? In particular, the GL.Viewport(Rect) method seems to work very strangely within a GUI. One cannot simply give it a GUI Rect and thereby have a viewport in the same place we'd have a GUI control if we gave it that same Rect. We need to calculate the Rect that will put the viewport in the appropriate place, and even then we have to determine the coordinate system within the viewport. Based on the documentation for Viewport(Rect) one might expect the viewport to be (0, 0) to (Screen.width, Screen.height), but it does not always work out that way exactly, and it all gives the impression that GL is not designed to be used within Editor GUI. The documentation for GL.Viewport has it used in an OnPostRender method, so is it misguided to try to use GL in other places?
If we should not be using the GL class, then what is the technique for drawing within custom Editor controls?
You may wanna look at Unity Graph view if you want to make a Node Based Editor in Unity.
It use UXML and USS with is close to HTML and CSS.
Making it pretty easy to customise as you wanted.
Unity Video on UXML and USS: Customize the Unity Editor with UIElements!
https://www.youtube.com/watch?v=CZ39btQ0XlE&t=98s
Here are 3 Github you can download and look at.
https://github.com/rygo6/GTLogicGraph
https://github.com/rygo6/GraphViewExample
This one is made by me.
https://github.com/KasperGameDev/Dialogue-Editor-Tutorial/tree/Dialogue-Prototype-Bonus

How to create interactive graphics with WPF?

I wonder how I can code an application that allows the user to handle simple graphic objects like in a vector graphics program. As a starting point I would like to have a program which allows the user to draw some rectangles, select them with the mouse and move them around.
I have some base knowledge in WinForms but it seems that WPF is a better choice for this task (tell me if you think different. I wouldn't mind using a free graphics library for Winforms as alternative).
I think I know hot to draw a rectangle and how to find out which rectange was clicked by the user. But I don't know how to move the rectangle around with the mouse. Can you give me a hint? I had a look into animations where I could move the rectanle around programatically but I am not sure whether this is the right way to implement it for mouse control.
You might want to have a look into PathGeometry. Check this link.
Combined with a Canvas and some controls you can make a pretty nitty editor ;)
Hope this helps.

Drawing a lot of lines and other shapes in WinRT App

In my WinRT app I need to draw about 3000 objects on a canvas, where I can translate and zoom the view. Unfortunatley, after adding about 1500 lines to my canvas my Windows 8 App always crashes. What could be the best practice to achieve this?
One solution could be rendering everything on an image (how do I do this?). But then I loose comfort of easy access and editing of every element.
Also my scale and translate is very slow. But since I also need a big overview, it makes no sense to put only the objects of the visible area in the canvas, since on minimum zoom it's still everything and zoomed it's still very laggy cause of add and remove operations.
There are a couple of different things you should employ to have a smooth UX:
Use a Quadtree, whenever you add a shape to your canvas you also put it on your Quadtree. This will be helpful when you will zoom on a portion of the image: you will know what objects are in this portion of the image; you will render them again (against using a cached/pixellated version).
To overcome the potentially lengthy drawing process you could do the following:
display the portion of the cached image overview at the right scale
use a progress indicator to let know the user that the program is working render this portion
when the faint rendering is done, blit it on the screen
A concrete example: Google Maps does that.

Create an Object Editor in C# for WinForms

I need to find, or create an editor that will handle text and images as objects. For instance I have a 3 line string of text, to be able to move it around and position it within a canvas, also the ability to add an image, and possibly resize it within that canvas. and take the result, and save it, whether I get the the offsets and positions manually, of each of the objects (preferable) , or get the entire canvas as an image, to be able to save and print.
Rulers would be great... Im not trying to re-create Photoshop, but the idea is similar.
I will be doing this in a C# WinForms application, it does seem however that a WPF solution might be better suited, and I think I can have a WPF control within winforms...
Any direction or advice would be greatly appreciated.
Forget winforms. It doesn't support anything. Your best bet is to do it in WPF and if you need, you can integrate it into an existing winforms application via the ElementHost.
Please see my similar answers/samples about this:
https://stackoverflow.com/a/15580293/643085
https://stackoverflow.com/a/15469477/643085
https://stackoverflow.com/a/15821573/643085
Also, see this example with support for zoom, panning and resizing functionality:
https://stackoverflow.com/a/16947081/643085
They're all MVVM based and have some interesting features.
You can easily customize these samples and add ANY type of elements:
images,
geometries,
usable interactive UI elements with functionality (TextBoxes, ComboBoxes, whatever),
text,
videos,
FlowDocuments,
or whatever that's visible on screen)
by adding additional data items and their corresponding DataTemplates.

Making fast drawing canvas like Photoshop in wpf

I have to write a faster canvas in wpf for my upcoming project. The canvas will be similar to adobe photoshop canvas. It should have layers and objects and also direct drawing methods.
I am thinking to write the canvas in vc++ or in c# and use it into wpf. Is it a good idea to write the canvas in any other language and use it with wpf? Or should I extend the existing canvas of wpf? If using other language, then vc++ will be better or c#?
Thanks
You are looking for the writeable bitmap - see http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.aspx
This allows direct pixel level manipulation of graphical data, basically designed for what you want to do.
I suggest you use QT. :)

Categories