I have a silverlight Canvas which holds an image with drawings on it (polygons). I need to develop a control to zoom and pan this canvas within a work area (Border within a Grid cell, as of now) as shown below. What is the best way to do this. Is there any libraries I can make use of?
I need to be able to add drawings to the zoomed/panned canvas too.
Lots of code examples available on the internet. Check out ZoomControl and CodePlex.
You can try creating a UserControl that is basically an image inside a canvas, and expose two transform properties to control the zooming and panning. A ScaleTransform would handle zooming, and a TranslateTransform would handle panning. You can create a CompositeTransform from both of them and assign that as the RenderTransform of the canvas.
You can bind the zoom slider to the ScaleTransform, and handle mouse events to change the TranslateTransform. As long as you get the mouse coordinates relative to the canvas itself that should work (i.e. mouseEventArgs.GetPosition(canvas)).
Related
In WPF, I have a custom "toolbox" which consists of Label controls and some vector icons docked to the left of the screen.
In the center, I have a Canvas control which I eventually am going to need to serialize out the relative coordinates (for other platforms) for this "designer surface".
Basic question, I can drag/drop controls from this psuedo-control box onto a Canvas but I need to know how to place this WPF control properly in the canvas, under where the mouse pointer is, realtive to the Canvas and not the screen or main Window.
What are the functions that needed to be called so that I can ensure that if I drop a Button control at 10%, 20% of the canvas, I get an actual location back and the button drops where expected?
Mouse events provide a Point structure.
Converting of positions can be done by Control.PointToScreen and TargetControl.PointToClient.
I am creating a simple app to display multiple images one below the other.
In WPF, I used Number of canvas equivalent to number of images and added those canvas to the main canvas.
And using Image control in each canvas, i uploaded the images with me and it is looking good.
Now, I Am trying to do the same in Windows forms.
I tried Panel (as the main canvas in WPF) and draw images over it by using Panel_Paint event. it is fine. But I need to add something(as I added multiple canvas in WPF), but did not get strike any thing.
I planned for few panels, but all them need Panel_Paint to draw images over it.That is some what difficult to maintain... any other ideas?
You can create your own custom control and override OnPaint method. There you will be able to draw whatever you like in Canvas like mode. Create element specify its coordinates, draw it with Graphics object. And for overlaying use linear drawing order, items drawn later will be top most.
If you want to create a Paint-like canvas, where you can draw simple graphics and images, you could use an instance of Graphics, like the following:
// myPictureBox is the control where your graphics will be drawn
Graphics myCanvas = myPictureBox.CreateGraphics();
If you want to display a group of images, like .jpg are displayed in the files explorer, you could use a ListView.
Now I can draw rectangle in canvas using a mousedown and mousemove events
I want to select rectangles and drag them in canvas ( move ) and resize them
how ?
http://www.codeproject.com/KB/WPF/WPF_DrawTools.aspx
Even if your implementation is simpler than this sample, you can still learn from the sample how to make hit test, selection, group selection, draw resizing handles, change z-order, and finally, move and resize objects. Note that this is kind of low-level Winforms-style solution. You can get similar functionality using high-level WPF support.
I am creating shapes on a wpf using a Canvas. I have created by C# code some Rectangles, each one of which is assigned with three circles and a TextBlock. I want now to make them move on the Canvas by a mouseEvent , in other words to drag them with the mouse and to move them on the Canvas. How is it possible to manipulate each Rectangle with the circles and the TextBlock as a compact group?
It is required, when the user clicks on the Rectangle to transfer it with its contents. However, the circles and the textBlock have their own coordinates, so if not grouped they stand still. How can I get over this?
Put the rectangle and everything else in a grid or another canvas, as described in this related question of yours: Drawing circles on a Rectangle
This is basically a user control composed of the rectangles, circles, and textbox, and you just allow that whole user control to be moved around, rather than the constituent parts.
If you don't want to add another layout panels try to apply the same transform to all elements you want to move, it's not so heavy solution.
Hey, I'm having an issue with adorners. I created resize adorners which have four Thumb controls, one for each of the corners of the control being decorated. The control is being drawn on a Canvas, and the user may select it and resize it.
The canvas is hosted on a ZoomBoxPanel control, which basically applies a ScaleTransform and TranslateTransform to the whole thing to provide zoom and pan support.
Problem is that when an item is outside the bounds of the zoom panel, and the item is cropped as you'd expect (like when you set ClipToBounds = true), you can still see the adorners! Could anyone please tell me a way to prevent this?
Evidence. The black square is the one selected, two resize thumbs are visible (right), the others should not be visible (left)
Thanks!!
Fixed it, solution was to put an AdornerDecorator object immediately above the Canvas :)
<AdornerDecorator>
<Canvas>
</AdornerDecorator>