Rotating Viewbox contents smoothly - c#

I'm looking to teach myself better methods of doing things in WPF that I would normally do manually.
In this case, I have a ViewBox with an image in it. I also have a button that uses a DoubleAnimation to rotate the image 90 to the right.
This animation works fine, but obviously because it's square as it turns, the image does a "best fit" to the ViewBox which makes the rotation look quite bad, as it gets larger and smaller as its longest edge shrinks or grows to fit to that particular rotation angle.
I am looking for any advice on the best way to handle this using appropriate WPF methods. Obviously I could do all the calculations manually, but I would be more interested in finding a way to use the controls and methods built into the .NET architecture.
Thanks for your help.

If you only have an Image in your ViewBox, drop the view box. An image is already capable of stretching correctly with the Stretch attribute set to Uniform.
In any case, use a RenderTransform instead of a LayoutTransform, to avoid recalculating the position of the controls when the images rotates. RenderTransform will rotate the object after all position calculations are done so you'll be fine. Just add a margin around the image if you find that it pass over some control while rotating.

Related

Setting a texture using a "tileset" to a WPF button

I'm trying to create a button using a tiled texture. Basically there are textures for the corners, borders and the center. These should be repeating, completing the button's texture completely, without having to create each texture for different button sizes. If possible, the texture should be made and applied at runtime (if possible, the textures should be combined into a single texture to keep performance higher when using large or many controls). The problem is that I don't know where to start with this. A simple reference to something that might solve this problem would be great.
If you don't understand what I mean, this may help. https://i.stack.imgur.com/t89dT.png
Note that the above images contains a simple texture with two borders and a solid fill, but if I would like to do something more complicated for e.g. the corners, this wouldn't be as easy to recreate with simple WPF button properties (I think).
Edit: I made a small gif animation hopefully explaining it a bit more: https://i.stack.imgur.com/fFhVY.gif
Regards, tizu

Enable Anti-Aliasing for WPF shapes

It seems that many people are trying to turn OFF anti-aliasing in WPF, but I seem to have the opposite problem. I am drawing shapes in WPF, and the edges are all aliased and ugly. The worst part is that when I use a ScaleTransform to zoom out such that a shape is less than one pixel tall/wide, it disappears entirely. How can I make them smooth and pretty?
Currently, I am drawing Rectangles and Ellipses and placing them in grids and StackPanels.
After a lot of googling, I found out how to get my images to anti-alias by using the following line in my window's constructor: RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.Fant);
However, this only affects my Images and not my Shapes.
I have what I think is the same problem. Setting SnapsToDevicePixels to false works for me:
<Ellipse SnapsToDevicePixels="False" />
You can set the EdgeMode the same way you set BitmapScalingMode
RenderOptions.SetEdgeMode(this, EdgeMode.Unspecified);
But Anti-Aliasing is set as default so you should not need to change it, but give it a go :)
Also setting SnapsToDevicePixels = true; on your window may help with the shapes.

Unable to pan a canvas - Silverlight

I have seen lot of posts which demonstrate how to move objects in a Canvas but what I need is, a way to move either
1. the entire canvas along with its every child
or
2. move every object manually (which is certainly not advisable)
I have put my Canvas in a ScrollViewer.
My actual issue is : I m trying to zoom the canvas using ScaleTransform but after zooming, i also need to move the scroll viewer to a point such that the clicked point is at the center after zooming.
I tried Canvas.SetLeft() and Canvas.SetTop() but bad luck..
Any idea?
Thanks in Advance..
You can communicate with the ScrollViewer to make it scroll its content to a particular offset by using ScrollToVerticalOffset and ScrollToHorizontalOffset.
You'll need to calculate the correct offset by taking into account the size of the "viewport" i.e. the area that you can see of the content, and the zoom level.
http://go4answers.webhost4life.com/Example/center-zoom-wpf-problem-159135.aspx
This might be useful for what you are doing:
http://autoscroller.codeplex.com/

Motion animation which is scalable?

is it true: a motion animation always based on a Canvas element – the Element which move is addressed via attached Property? Unfortunately it’s not possible to resize a Canvas-Element at Runtime!? So the Motion gets wrong when the user maximize the Window.
How can I create a motion animation which is scalable in WPF / C#?
Thanks a lot.
Draw your elements on the Bitmap,
and than draw your Bitmap with scale as you want
If you try resize not the self drawed elements, you can use "Viewbox" - content decorator that can stretch and scale a single child to fill the available space.
Here you can see how it works:
http://www.wpftutorials.com/2011/04/wpf-viewbox.html

WPF: How to find space available for Canvas?

I'm using WPF shapes to create Hexagons (for a game map) on a Canvas. After some playing around with ScrollViewer, I've decided to implement the scrolling and zoom of the map myself rather than using WPF functionality, just using WPF to get the events for mouse wheel, arrow keys etc. I'm placing the (Hex Map) Canvas as the last child inside a Dock Panel so it will get all the available remaining space. The Dock Panel will be set to be the content of the Main Window. But I want to find out how big the Canvas can be before I put any Children on the Canvas so that I can centre the screen over the Hex I want and only add the Shapes (Hexs) that can actually be seen. When zoomed out, a long way I will remove Polygons altogether and use another method of rendering and when zoomed in a long way I will add more details.
Is there any neat way of getting the available space? The only way that I can think of that will hopefully work is to get the current dimensions of the windows and subtract the dimensions of the outer elements of the Dock Panel, but that feels rather messy.
You may use the ActualWidth and ActualHeight properties of Canvas to determine size available to it. Be sure that HorizontalAlignment and VerticalAlignment are set to Stretch.

Categories