This is in reference to the following answer: c# pan and zoom image
I want to add a grid that scales along with the image. I've tried adding a canvas with IsHitTestVisible = "False" and use border.RenderTransforms.Value.OffsetX which gives a value, but doesn't update. I also tried ((TransformGroup)border.RenderTransform).Children.First(tr => tr is ScaleTransform)) but it provides a MatrixTransform instead of a Scale.
Basically, I'm looking to share the ScaleTransform and TranslateTransform values from the ZoomBorder class object and place them into a canvas.
After some trials, I found that a simple canvas.RenderTransform = image.RenderTransform works well.
Related
I want to clip a Grid by a dynamic PathGeometry and it's PathFigure.
In WPF application UIElement.Clip has some properties like CombinedGeometry,PathGeometry ... But in UWP there's only RectangleGeometry.
I'm very new to .Net and stuck on this right now, does anyone know the equivalent way in UWP?
Thanks.
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.clip
According to MSDN,
Remarks
The clipping geometry for UIElement.Clip in the Windows Runtime API
must be a RectangleGeometry. You can't specify a non-rectangular
geometry, as is permitted in some XAML frameworks like Microsoft
Silverlight. The clipped area is the "outside" of the geometry. In
other words, the content that is shown (not clipped) is the area of
the rectangle that is drawn with Fill if the geometry were used as
data for a Path rather than for clipping. The clipped area is any area
that falls outside the rectangle. The clipped area isn't hit-testable.
As #Jerry pointed out in this answer,
you can apply a Transform to a RectangleGeometry which gives you a
little more insofar as options.
I've been working on a proper slider for my C# WPF project.
I wanted to create a slider, with a background that indicates different parts of the process, by adding a different color to each section on the slider. Furthermore I wanted to add small indicators (like the default ticks, but custom shape and irregular position) to the background.
I achived this by creating a drawing brush and adding correspondingly colored rectangles. This seemed to work fine, but a small distortion was still present, so I investigated further and realized the following:
With slider.ActualWidth I get the width of the whole widget. So in order to create a background covering the actual "slider" part, I'll have to be aware of the distance from the widget to the actual slider. (See image)
I measured the distance in a very small window, in fullscreen and stretched on two screens. It seems this distance is always 5 pixels. I tried google and looked through the info WPF provides on its pages, but either I read over it, or there is no information on this.
Can I be sure this distance is always 5 pixels ? In there any place such information is kept ? Is there maybe another way, to determine the size of the slider itself?
Assuming you haven't tinkered with the Slider template you can just walk down the visual tree and check the ActualWidth of the track:
Border b = VisualTreeHelper.GetChild(slider, 0) as Border;
Grid g = VisualTreeHelper.GetChild(b, 0) as Grid;
Border track = VisualTreeHelper.GetChild(g, 2) as Border;
Console.WriteLine("Track ActualWidth: " + track.ActualWidth);
I am able to add an image to my map just fine via code.
However when I zoom in/out, the image stays the same. I would like it scale relative to the map.
In the WPF version of the Map, you could use an ImageBrush for a MapPolygon and it would be constrained to the bounding box.
I tried the solution from this SO question, but it seems to have no effect on the Image.
imageLayer.Children.Clear();
MapLayer.SetPosition(_vm.RadarImage, new Location(_vm.Overlay.LatN, _vm.Overlay.LonW));
imageLayer.Children.Add(_vm.RadarImage);
shapeLayer.Shapes.Clear();
var rect = new MapPolygon();
rect.Locations.Add(new Location(_vm.Overlay.LatN, _vm.Overlay.LonW));
rect.Locations.Add(new Location(_vm.Overlay.LatS, _vm.Overlay.LonW));
rect.Locations.Add(new Location(_vm.Overlay.LatS, _vm.Overlay.LonE));
rect.Locations.Add(new Location(_vm.Overlay.LatN, _vm.Overlay.LonE));
rect.FillColor = Colors.Green;
shapeLayer.Shapes.Add(rect);
mappy.SetView(new LocationRect(new Location(_vm.Overlay.LatN + 0.0001, _vm.Overlay.LonW + 0.0001), new Location(_vm.Overlay.LatS - 0.0001, _vm.Overlay.LonE - 0.0001)));
This is the correct scaling.
When you zoom once via the Navigation, you can see the image is now larger than the Polygon
There isn't a simple solution for this. I have put together a sample app that shows one approach to do this. You can find it here: http://code.msdn.microsoft.com/Binding-and-Image-to-a-01a56e48 What I did was add a Canvas to the map, and then use the map to calculate the pixel coordinates of the bounding box for the image. I then used these pixel coordinates to scale and position the image on the canvas overtop the map. I've done something similar to create custom polygons that support image brushes in the past but haven't uploaded that code sample yet.
All I want to do is add my UserControl at a specific point on the Grid.
Now I can do this using a RenderTransform after adding to the Gird, but I don't think they have been added to the VisualTree when this happens.
Is there a way to wait for the UI thread to complete before doing my Transform? Any way to add an item to the grid using pixels? i.e. 40px from the left and 40px from the top?
The code I am using is as follows....
grid.Children.Add(temp);
TranslateTransform transform = new TranslateTransform(-oldPosition.X+temp.posx, -oldPosition.Y+temp.posy);
temp.RenderTransform = transform;
oldPosition is where it gets added to the gird in the first place, a Point.
temp.posx and temp.posy are the x and y coordinates where I would like the UserControl to be.
Also note that I do not have any rows and columns set up on the grid, the reason for using the grid is so I can have the application maximised and the grid will fill the whole screen, a Canvas does not seem to do this.
Can't you just do:
grid.Children.Add(temp);
Grid.SetColumn(textBlock, Y);
Grid.SetRow(textBlock, X);
I believe UpdateLayout will do it:
grid.Children.Add(temp);
grid.UpdateLayout();
TranslateTransform transform = new TranslateTransform(-oldPosition.X+temp.posx, -oldPosition.Y+temp.posy);
temp.RenderTransform = transform;
I'm doing a software where I need to put squary bordering fields on a satelite map (.png image), so that the fields can be clicked.
What is the best way to add shapes on a picture and associate them with data ?
Overlay a custom-draw UserControl on top of the Image control. Make part of it transparent to reveal the underlying image, but still be able to capture the mouse interaction.
You will have to calculate the exact position (pixel offset from the map top-left corner) of your control to overlay the proper map area. How you calculate that offset and the actual size of your custom control depends on the map zoom level and whether you use GPS coordinates or image recognition to determine which area needs to be overlayed.
Graphics.FillPolygon()
Is your friend. Hit testing is relatively trivial, with several algorithms available
You want to use the System.Drawing namespace to initially create a graphics object from your source image..then you want to draw on top of it, and finally export your edited graphics object to the filesystem...
Image image = Image.FromFile(Server.MapPath(String.Format("~/{0}.jpg", "YourImageNameHere")));
Graphics MyGraphic = Graphics.FromImage(LabelImage);
MyGraphic.DrawRectangle(SomePenObject, Point1, Point2, Point3, Point4);
Image.Save("C:\somepath.jpg",ImageFormat.Jpeg);