I am adding an unknown amount of images to a canvas at runtime via code behind but I suspect they are laying on top of each other so only 1 image is visible.
I am converting multiple page PDF's into jpg images with ghostscript and wish to add them to a canvas in a vertical sequential row inside a scroll viewer.
Currently I am just rolling through a for loop generating each jpg and adding it to the canvas with canvas.children.add()
Is there a common way to ensure elements added to a canvas do not overlap? Or a common method people use to make sure children.add places the element in the right position?
I am thinking inside my for loop I will need to see what else exists on the canvas and add the heights together and palace my new image at the correct position but I am not sure how this is done. Is anyone able to steer me in the right direction?
You could use a StackPanel instead of the Canvas and add the images in there. The StackPanel will then place the images next to each other (or one under another, depending on its Orientation).
Related
In a cross platform Xamarin.Forms application, I would like to add a SKCanvas on top of other UI elements to allow the user to draw e.g. on images. I know it is possible to load an image directly into an SKCanvas, but images are not the only application I need.
A perfect solution would be able to load the underlying content of the page, including all of its UI elements into the Canvas/Surface, so that the user can then draw on it and save the drawing + underlying content into an image (probably using the snapshot method?).
Is this possible?
I didn't find a way to do this exactly as planned originally, but ended up doing a workaround that satisfied the requirements at the time.
I added a transparent canvas on top of my content, such that the underlying content would be shown through the canvas, but anything on the canvas would always be drawn on top. The drawings were saved and transmitted separately from the content and re-drawn when needed. On demand, when the user would need to export both together for a snapshot/share/export function, I then loaded the underlying content as image into the canvas and exported it together with the drawings from the canvas into a new image.
I started my app by placing rectangles and other objects on a stackpanel. That worked will until I wanted to split my rectangles and have two columns of rectangles. The vertical stackpanel worked well until I needed to split my rectangles into two columns and put stuff on the left and stuff on the right.
So I converted to a canvas. Now
mainCanvas.Children.Add(grid); // seems to replace what the last Add placed on screen. Any ideas how to control the position of items added to the canvas?
Edit:
Ok clearly a canvas is the wrong panel to use. Two column's of stackpanel's might be made to work. But when I look at what I am actually trying to accomplish, the column's aren't limited to two, but could be n number of columns. Why? Because the application is a flowchart style app that builds a custom language script. The diamond decision shape splits one column into two which can split into two more extra.
I wonder about using a single stack panel and just making a grid with multiple grids horizontally in it. But mouse events would have to be smart enough to know which grid in the grid your actually in. Not undoable I think, but not trivial...just looking to see if there is a obvious use this choice that i am missing by being a wpf rookie.
Edit2:
The issue I have with just using a grid is that when the window is stretched a
grid does not resize to the new window size.
making a grid of multiple other grids, then using isMouseOver to test to see which grid in the single child of the stackpanel actually needs mouse highlighting works.
I'm building an application for users to create game terrain using hex tiles. I have a tree view with the various tile types and have successfully managed to load the images that I want to use (png's) into the tree as the node images.
I have used the code from here http://www.codeproject.com/Articles/14948/Hexagonal-grid-for-games-and-other-projects-Part as a basis for my project, although I have expanded it to allow for corner, and both "types" of half hexes.
What I want to accomplish is to drag a tile type from the Tile Catalogue tree view onto the hex grid drawn, and for that image to snap to the grid and align itself correctly. I imagine that the actual image to be drawn on the grid will end up being loaded separately as the treeview images are obviously very small versions of the original images.
Now I know there's a whole heap of items in there that I don't know how to do as yet but the first in the list is the drag and drop. All the searching I've done has turned up drag and drop examples for dragging within the treeview, or from one control to another and I'm not sure that those examples are relevant.
Can someone point me in the right direction to begin playing with this myself or give an example I could base what I need to do on?
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/
Following the guide here, I have created a full-screen WPF application. But I met a problem: the various size & resolution of screens. For example, I want to put several sprites on the screen as buttons; but they are located at different positions in each screen, and even different to what shown in the XAML designer.
I have searched all over without a clue got. How can I fix this problem? (to make the buttons appears the exact place (in the center), and better, help the xaml designer reflect exactly what will happens when the program is running). Any help will be appreciated.
UPDATE: I'm defining my page as a Canvas inside the Window element. Actually I like Canvas more, cause I can easily put my sprites anywhere, not like a grid.
In general, you should not use pixel values in WPF.
Instead, you should layout your content in <Grid>s with rows and columns, and it will automatically expand to fill the screen (based on the alignments and row / column definitions).
Avoid using the canvas. Also, do not rely too much on the designer to build your layout. Using Grids, Stackpanels and/or Dockpanels will give much better results (and scale when resizing your window). For example, if you use only the designer and drag-and-dropp all your elements, the designer often puts huge margins a bit randomly and this will not always scale properly if you resize your window.