I'm writing a WPF app that has a Canvas in it. This canvas will be custom rendered at runtime. It's sort of like a game in that it needs to be measured in pixels. I need to be able to set my Canvas to 478x478 pixels (client rectangle size). I don't want any scaling or other resolution-independent steps to take place on my Canvas.
I'm not sure if this is possible in WPF, since its nature is to be resolution independent. My questions:
How do I resize my Canvas at runtime (function to call?)
When I resize my Canvas, is the renderable area (the client rectangle) going to be that size? If not, how can I resize it to make sure the client rectangle is a specific width/height?
Is it possible to set the width/height of the Canvas in Pixels? How does the resolution-independent aspect of WPF interfere with what I'm trying to do?
When I resize my Canvas, will other controls resize appropriately as they have been designed to do in the WPF designer?
Thanks in advance.
Any elements positioned in a canvas will not resize or reposition based upon the size of the canvas. So I don't think there's any advantage to setting the size of the canvas. Maybe you should just set a fixed size of the window instead.
Otherwise, just set the Canvas.Width, Height, and ClipToBounds=True and you have a fixed sized canvas that positions its child elements with X/Y coordinates.
Also you should be sure to set SnapsToDevicePixels=True on the canvas so that child elements will have crisp pixel-aligned bounds.
resizing is cake:
MyCanvas.Width = 350;
MyCanvas.Height = 450;
this sets the size, you CAN render to coordinates outside of this, but it will be clipped. You can wrap your canvas inside a scroller to allow the user to see what is outside the height/width of the canvas.
as for the rest of your questions, i think you can see this SO question for your answers
Update 2020: on WPF .NET CORE: If the Canvas is a child of a Viewbox, the content is automatically scaled to the Viewbox height and width.
<Viewbox Stretch="Fill"
Width="50"
Height="50">
<Canvas>
<Path.... />
</Canvas>
</Viewbox>
Related
Hi I'm hoping someone can help point me in the right direction to how i might go about solving this issue.
I have a blank image (white) which is 3000x1500 which acts a a canvas, i then have an image which again could be any size and any orientation. What i need to do is scale this image to fit inside the blank white canvas as best as possible and also center it. I would expect to see white gaps at the top or bottom of the final image if the image could not be scaled to fix the exact canvas.
Can anyone suggest what i should research, how i would go about drawing an image inside another in C# WPF and anything that may already exist that i could use to achieve this.
I forgot to mention that this would need to output to a bitmap so it could be saved to disk
All you need to do is to put an Image control into an appropriately sized Grid with white background, and set its Stretch property to Uniform. No Viewbox required.
<Grid Width="3000" Height="1500" Background="White">
<Image Source="<path to image file>" Stretch="Uniform"/>
</Grid>
Look up ViewBox. A ViewBox automatically scales its single child element based off of the stretch parameter.
I have a Canvas inside of a Grid in a WPF app. I want to draw a skeleton in Canvas from Kinect data. But the Canvas has a tiny width, probably 1px. The Grid makes it shrink, not keeping its set Width and Height. How to make Grid keep its declared size?
Make sure both the grid and the canvas have HorizontalAlignment="Stretch" and VerticalAlignment="Stretch"
I have a Windows Store app with a ScrollViewer and an Image in it. When i double tap on the ScrollViewer I want it to zoom the Image to its width. This part is not a problem but I also want the Image to be centered after it has beed zoomed in.
I tried calling the ScrollToHorizontalOffset method on the ScrollViewer but It does not seem to work with any number I give it. What is the problem?
Perhaps the offset only works for the non-zoomed view where your image fills the ScrollViewer completely and thus can't be scrolled. You could try setting the image dimensions so that it is larger than the ScrollViewer, but set original ZoomFactor, so that it fills the ScrollViewer at first. Then zooming and scrolling might work.
Assign a SizeChangedEvent in the scrollviewer.
<ScrollViewer SizeChanged="OnSizeChange"></ScrollViewer>
like this. Then it is better to place your image inside a canvas. So your code will be probably like this.
<ScrollViewer SizeChanged="OnSizeChange" x:Name="scrl">
<Canvas RenderTransformOrigin="0.5,0.5" x:Name="main">
<Image source="" Canvas.Top="insert desire double value here", Canvas.Left="Same goes here"/>
</Canvas>
</ScrollViewer>
then in the code behind you can change the height and width of your canvass depending on the scroll viewer
Main.Width = scrl.ViewPortWidth;
Main.Height = scrl.ViewPortHeight;
You can experiment on the size of the canvass while adding a double tap event to it. Changing the size of the canvas can zoom in or out the image because the image is inside the canvass
Try with 'ChangeView' instead of 'ZoomToFactor'
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
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.