WPF canvas drawing with Graphics - wpf

I'd like to ask if there is any possibility to draw on WPF Canvas with some kind of a Graphics type providing methods like: DrawLine, DrawPath etc.. (as it was in .NET 2).
I know there's a lot of stuff like storyboards etc.. but I'm planning to do all the drawing in code behind and to have just 1 Canvas in WPF without any child elements.
Do you think it is a good idea? will it be smooth ?

I'd like to ask if there is any possibility to draw on WPF Canvas with some kind of a Graphics type providing methods like: DrawLine, DrawPath etc.. (as it was in .NET 2).
Yes, you need to use the DrawingContext class
Do you think it is a good idea? will it be smooth ?
That's hard to tell, depending on your exact needs... If the canvas doesn't have any child items, I think a better solution would be to create a custom control and override the OnRender method. Regarding smoothness, it all depends on how you implement it...

Related

Nine-Patch Image in Windows Phone

In Windows Phone UI Design Principle, MS recommended use solid color rectangle or coding-gradient for Control Background to avoid incompatible in multi-screen. But in many requirements, using image as Control Background is necessary. Then, 9-patch image technique is used. In Android and IOs, it was support in core, but in WP it is lacking. I try to use it in WP by 3 approaches:
Using 9-cells Grid: clip image into 9 patch and lay them into cells. It works ok, but i afraid app performance reduce when has many control.
Using Custom Brush: only custom Brush to draw 9-patch image as ImageBrush, but seem MS not allow for custom Brush.
Using FramworkElement: like Rectangle, Ellipse... i want to create a FrameworkElement can draw a 9-patch image. But, can't use low-level render.
How can i implement 2nd and 3th approach?
I created a lib for Windows Phone which do exactly as Android NinePatchDrawable. You just need to set a bitmap image.9.png, the width and heigh... And done!!! you have you new image scale to the size you want. Enjoy it :). In the future I will add more option :).
GitHub link
You can compensate for the lack of low-level rendering and custom brush by using a WriteableBitmap: http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap(v=vs.95).aspx
This way, you have complete control on how to render your background, then you can assign it to a single Image control. But it's way more complicated than the "use a grid with 9 image controls" method, and the performance improvement is probably insignificant.

How to build the following visual

I am not a real designer and I would like to know if anyone have an idea how can I build the following visual :
The idea is to get 2 rings where inside I will draw secific number of pie represented as "Interactive shape or button". Base on the number of shape, they should cover the whole circle.
In addition to that, I need to be able to interact with each pie shape, and inside and oustide edge of those shape should be a perfect arc based on the circle diameter.
As I am not a perfect designer, how can I represent this visual ?
I was thinking of using a custom panel but then how to draw each panel shape in order that they gets perfect inside and ouside arc and offer interactivity with each of them ?
Thanks for your help
I would appreciate samples as well
Theres actually a Silverlight tutorial on making something exactly like this.
If you didnt want to use that, you could always do it in javascript. Here's an example using Rapheal js
I would recommend looking into WPF Geometry and how you can create custom controls (such as the Circular Gauge Custom Control over on CodeProject) using said Geometry.
I've never created a control quite like what your suggesting, but I would image you would define some form of area that can contain children and style it so that it forms the circular shape you want. Then, adding interactive regions should be as simple as adding controls to standard containers.
Here's a link to a "generic radial panel that can be used to host any items" which subclasses Panel.

Improving WPF Canvas performance

I am developing a Maps like application using WPF. I have ~10,000 PathGeometry, Shapes added to the canvas. I have added ScaleTransform and TranslateTransform for zooming and panning controls.
The problem I'm facing is, when I zoom or pan, there is slight lag. Is there a way to organize the data so that I handle only the Shapes that are visible?
Any hints on making it more efficient will be helpful and appreciated.
What kind f stuff are you putting on the canvas? If using pathGeometry, are you enclosing them in Path class? If so, Path has FrameworkElement in its superclass hierarchy, which is responsible for massive performance loss.
Take a look at my question here. Although it is about Shape class, but the reason of performance degradation is the same, FrameworkElement.
If you are doing so, the solution is to use PathGeometry instead, and enclose it in DrawingContext of a DrawingVisual using DrawingContext.DrawGeometry() method.
Here are some links that should help.
Path Geometry
DrawingContext.DrawGeometry()
Optimizing Performance: 2D Graphics and Imaging
And draw the shapes yourself, using combination of lines, and other things provided by classes derived from Geometry class (ArcGeometry, PathGeometry etc).
This should help.
If you want the ultimate in performance for immediate drawing in WPF, then check out WriteableBitmapEx. This is an excellent open source library, which I recently contributed to. it provides GDI-like drawing capabilities on WriteableBitmap and is compatible with Windows Phone, WPF and Silverlight. The API is simple, you get blitting, polygons, lines and simple shapes etc... You won't get datatemplates and gradient brushes however.

winforms apps - best way to handle images for performance

Probably a bad title but I am working on a winforms app and it was all going well until I started doing usability testing at different sizes.
I noticed that the "redraw" of controls on a resize event would go VERY slow. I googled and found that splitcontainers and dockstyle fill don't play well so I changed my approach and the performance improved but it was still slow.
It turns out my background images on my usercontrols was causing the delay and it got me wondering what's best practice for this? I have a few different gradients that are around 1024x768 that are set to sizemode stretch.
My temporary fix is to replace my pretty gradient background with solid black. But that does not make things look as nice. It seems that if I had two different sized background images and swapped them on form_resize that would be most efficient but there is a lot of times where I do things the hard way because I don't know better. So I figured i'd ask here. This is specific to just winforms. I export vector when I can for my wpf/sl stuff.
Is the gradient image very specific? If it's just a linear gradient can't you paint the background yourself?
In the user control, add an OnPaint override like so:
protected override void OnPaint(PaintEventArgs e)
{
Brush brush = new LinearGradientBrush(
Point.Empty,
new Point(0, this.Height),
Color.Yellow,
Color.Green);
e.Graphics.FillRectangle(
brush, e.ClipRectangle);
}
Try to set the BackgroundImageLayout Property to 'None' on your controls with the gradients. This usually gives a moderate performance boost

Choosing between Drawing and Shape in WPF

I am not quite sure about the differences between the classes System.Windows.Media.Drawing and System.Windows.Shapes.Shape. They both expose functionality related to 2D graphics in WPF. When would you choose one in your WPF application, and when would you choose the other?
A Shape inherits from FrameworkElement and is therefore a high level object which provides features such as hit-testing, styling, layout and data binding. In contrast a Drawing does not inherit from FrameworkElemet and doesn't support any of these features. As the documentation mentions a Drawing is useful for lightweight visual objects. If you are creating a complex brush to use to paint areas or a background a DrawingBrush would be very efficient.
Drawings can combine text, video, images and Geometry objects (another light weight class) to create complex but very efficient and fast drawings.
In short a Drawing is a low-level alternative to a Shape.
As for use cases, it depends.
If you have to animate or do any sort of binding you would use Shapes.
If you are creating brushes or complex clip arts/vector graphics you would probably use Drawings.
Also, if you draw things by overriding OnRender you would mostly use Geometries.
A Drawing is also Freezable and can thus be shared among threads (assuming it is frozen).

Resources