I want to do basic WPF graphics, i.e. rectangles, lines, circles and text.
When should I use Drawing and when should I use a DrawingVisual?
I have some code that uses Drawing and I render those to a DrawingImage and display that in an image control. Is this the right way? I could not see how to add text to a drawing. I had trouble positioning it too. Should I be rendering to a Canvas?
I have some code that uses DrawingVisual and writes to a DrawingContext. That is like WinForms. Is this the recommended way?
Do you have any high level advice on which APIs to use for basic graphics and labels? Will they options work on Silver Light and Desktop?
You mention in your comment that you're actually implementing scatter plots or similar graphs. Typically in WPF this is created by templating the existing controls, like a listview. It sounds counterintuitive, but this is far easier and more powerful than drawing your own.
This article by Charles Petzhold shows a scatter plot implemented in this way and goes into great detail about how to make it performant to 10,000+ data points.
You'll most likely want to just render to a Canvas. If you add "shapes" to a canvas, WPF will handle all of the drawing for you.
For details, see Shapes and Basic Drawing in WPF.
Related
I'm using the WPF InkCanvas for a drawing application. The canvas is over a image. I can scribble over the image OK but I need it to have a glow effect - i.e. the actual lines drawn must have this effect. Is there a way to do this in XAML or in the C# code?
You want to use a ShaderEffect and attach it to the UIElement.Effect of the InkCanvas. This MSDN article contains a sample that shows how to write ShaderEffects.
Try starting with the excellent set of ShaderEffects implemented in the Windows Presentation Foundation Pixel Shader Effects Library.
I am looking for a control for cropping images as it is in eg. IrfanView (Drawing rectangle with possibility to move individual sides). I have not found anything like this. Does such control exists?
You should have a look at the CroppedBitmap class, there are several tutorials out there explaining how to use it. This example uses it with an adorner in order to visually select the area that should be cropped.
I am developing an application in C# .NET using WPF. I want the users to be able to draw shapes on a draw area. The shapes are zigzag lines and polygons. The end points of line segments should be small squares as shown in the image below. In some cases, I need arrows in the middle of line segments as shown in the image below.
I have partially implemented the drawing by adding lines to a canvas and resizing the lines on mouse events. However, I have not been able to implement creation of small squares on the end points of line segments and arrows.
Is there any package or tool that provides the functionality of drawing such shapes?
Thank you for any help which you can provide.
A common way to approach this is to use Adorners. Basically, there is a virtual layer rendered for adorners. You can use these to drag / resize (among other things) visual elements on the screen with a little bit of glue code.
Here is a page from Microsoft on the subject:
http://msdn.microsoft.com/en-us/library/ms743737.aspx?ppud=4
I need to load an image in a WPF window, an be able to read and modify individual pixels (in an efficient way), zoom the image (and scroll it), get the value RGB/grayscale of the pixel under the cursor, select areas (I guess knowing the cursor position and being able to modify pixels I could draw myself the square which represents the selected area)...
What is the best combination of WPF controls and classes to accomplish this?
I've been trying to do it loading a System.Windows.Media.Imaging.BitmapImage and putting it into a System.Windows.Controls.Image, but it's taking much longer than I expected.
Thank you very much
I once used this WPF Interactive Image Cropping Control. Go check it out, it should at the very least give you a good place to start. Oh, and welcome to Stackoverflow. :)
I would like to learn about creating a program that I could draw simple shapes and be able to select them for editing - like resizing, order of display, color change. Is there an online resource that someone knows of that would help me reach my goals.
thanks
"GDI+" is what you are looking for. You can start here: http://msdn.microsoft.com/en-us/library/da0f23z7.aspx
A sneaky way I used to do it was to create a custom control, remove the background from it and paint my shapes and sizes on it. Then, you can easily implement selection (override OnClick), dragging and resizing (OnMouseDown, OnMouseMove, OnMouseUp). You can then implement options like color, etc. by means of a property (see Browsable attribute and property get/setters) and a PropertyGrid control.
Anything beyond that, though - Bezier curves and such - would need something a tad more advanced, though.
The alternative is to only use such controls for the sizing handles, and do all the drawing on one central canvas - the only drawback then is figuring out how to select a shape on the canvas.