How can i calculate the right coordinates? - c#

I need help. I have a Picturebox and would now like to calculate given coordinates on the picture and then play them in the label. How can I do that best?
As seen in the picture.
If you then click on the image on it, then the data is entered in a list box.
Thank you for your help.
My Picture here: https://prnt.sc/puxyu6

In WPF this particular might for once be the hardest. WPF/UWP is designed for the MVVM, and I do not know anyone but beginners that programm outside of MVVM. And I can not think of way to do this with MVVM.
PictureBox is also the WinForms Element. The WPF equivalent is called Image.
Navigation aids like this are not a trivial thing. One reason there are so few of it. But it comes down to few step process:
Get the x and y pixel coordinates that was clicked, also in relation to that the overall display size of the Image. Usually the MouseClick Event would be the tool for that, but I can not find it. MouseDown or LeftMouseDown are the closest events.
If the entire image was shown with no zooming or cropping, it is now just simple math. If it was 20% of the X axis and 21% of the Y axis, it is pretty easy to figure out where 20% of X and 21 of Y is on the SourceImage.
If there was any zoom or crop, this has to be taken into account, otherwise it is 2.
Equate the Image Pixel position with the coordinates you know for it.
Part 1 would look like this and needs to be registered with the MouseDown or LeftMouseDown Event of the Image:
private void ContentControl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//Unpack the sender
Image source = (Image)Sender;
//Get the click point
Point clickPoint = e.GetPosition(source);
//There is more then 1 height Property in WPF.
//The Actuall one is what you are looking for
//Unfortunately you get doubles for this and int for the other. Consider general advise regarding double math
double ElementHeight = source.ActualHeight;
double ElementWidth = source.ActualWidth;
//Do what you need to find the relative position
//Part 2
}
Hopefully someone else can give you a better answer.

Related

I can't get the correct coordinate of an image with mouse over C#

This is my first question ever on Stackoverflow. So I hope it for the best answer.
I want to get correct X and Y coordinate of an Image with mouse over Event (WFA .NET Framework).
Take a look at my cursor
The coordinate should be somewhere between 500 for X and 427 for Y, but I only get as I posted. I already maxed out the scroll. And I think the image resolution is correct, here's the image properties
Here's my code:
private void pbInput_MouseMove(object sender, MouseEventArgs e) {
mouseX.Text = e.X.ToString();
mouseY.Text = e.Y.ToString();
}
And I have a plan to zooming the image for the future, so I put "auto scroll" panel below the picture box.
Could you help me? Thank you so much.
PS: Sorry for my bad English
Try putting picturebox inside panel
set panel to:
this.panel1.AutoScroll = true;
and picturebox to
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this will allow panel to have scroll bars and picturebox will have its full size adjusted to actual picture size

Polygon selection

I'm using the WPF version of GMaps.net. Certain functionalities such as polygon selection is not that obvious.
What I need to do is detect a polygon click on the map, and then change properties of the polygon (opacity etc) to show that it is selected. A custom way to achieve this is by getting the mouse coordinates with a mouse click event, and do a polygon intersection test with some vector maths, however I'm sure there must be a built-in way to do this?
For clarity sake, this is how I create my polygons:
GMapPolygon polygon = new GMapPolygon(polyPointList);
polygon.RegenerateShape(gMapControl1);
(polygon.Shape as Path).Stroke = Brushes.DarkBlue;
(polygon.Shape as Path).Opacity = 0.5;
gMapControl1.Markers.Add(polygon);
I believe you're right, the WPF version of the polygon doesn't offer it right out of the box.
Think you could use the PointLatLng of your click and check if that's within the bounds of the polygon. Take the WinForms version of it and adapt it. In the end it's just that bit of math that's lacking.

How to move a rectangle that's used as a Meter bar in unity

I want to make a vertical meter for values between 2 ranges.
so what i tried so far, was to make a frame, and under it put a mask, and under that a rectangle.
with the idea, of lowering/increasing the rectangle Y position to make it seems to move.
rect.position = new Vector3(rect.position.x, (rect.position.y - rect.sizeDelta.y));
so i did that, to first lower by it's height, so the bar will seem empty.
but it doesn't work right, the position depends on the screen size, and the rect Y end up way lower then exactly his height.
so, tl;dr, i want a bar that i can give a value between 0-100 and it will fill it according to that.
and i might have gone wrong about it.
so, any suggestions would be appreciated.
edit:
Add picture to show what i want the bar to look like:
Well i feel kinda dumb
I didn't realize how customizable the Slider control is.
I removed it's handle, unchecked interactble and removed it's background, and now i can give it a value between 0-100 and it works exactly as i wanted.
thanks Mathias Siig Nørregaard for referencing me to the video link that opened my eyes.

Handling Swipe Guesture in Windows 8 Grid

I am trying to implement a custom control which consists of a grid with some canvas elements as children , When a swipe action is made on the grid , I am intended to preform some operation with the canvas elements .
I am unable to handle the swipe for the grid , i have posted the same in the
msdn - win8 Dev forum
I was in the same boat as you guys, since there was no samples out there on how this was done, but after perusing and scrutinizing the MSDN documentation on how a swipe gesture is implemented on a Windows 8 Store app using C#, this is what i came up with (and it works for my app which requires swiping up / down / left / right):
First of all, instead of the GestureRecognizer, the Manipulation events need to be used, so on the grid that you want to handle the swiping (lets' say you make it so that it takes the whole screen so it interprets the gestures) do the following:
I called my grid swipingSurface and i'm handling manipulation modes for both the Y-axis and X-axis:
swipingSurface.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
Then wire up the manipulation events that you want to be notified, in my case i just want to know then the manipulation started and when it ended:
swipingSurface.ManipulationStarted += OnManipulationStarted;
swipingSurface.ManipulationCompleted += OnManipulationCompleted;
Do whatever you want on your manipulation started, such as getting the initial point if you want. But the actual trick is on the ManipulationCompleted event, in which you need to get the Velocities resulting from your gesture, as follows:
public void OnManipulationCompleted(object sender, ManipulationCompletedEventArgs e) {
var velocities = e.Velocities;
}
The ManipulationCompletedEventArgs Velocities property will bring back a struct of type ManipulationVelocities, which contains other properties inside:
-Angular: The rotational velocity in degrees per millisecond.
-Expansion: The expansion, or scaling, velocity in DIPs per millisecond.
-Linear: The straight line velocity in DIPs per millisecond.
I'm actually looking at the Linear velocity, which is a Point that contains X and Y values indicating the direction in which the gesture was performed; for example, if the swipe was upward, you will notice that the Y value is positive, and if its down the Y value is negative; the same goes for the X value, if the swipe is left, the X values are negative and if the swipe is right, the X values are positive, so you can play around with those values and check your swiping direction, final points, etc.
Hope this helps.
You could try setting ManipulationMode on your swipe-able control and handling the Manipulation~ events. Note that some controls might stop bubbling of UI events, so if you say put your control inside of a Button or a ScrollViewer - the events might not work.
You could check out SwipeHintThemeAnimation that uses GestureRecognizer to hook up to a Rectangle control or modify it to use your Grid control, see the documentation.

Graphics/Bitmap Limits?

Im having some weird problems with Graphics and Bitmap.
I have a Graphics Object that is displayed on a PictureBox and im capturing the MouseMove and MouseClick Events that give X and Y Position of the Mouse on the Image but if the Y Position goes Bigger then 32775 it then goes into Negatives which means everything breaks. And if the Image is Bigger then 65535 it then stops displaying the Image.
Any Ideas how these problems can be fixed?
Thanks
Example Code:
http://pastebin.com/YEX0XD1q
Just Click Make 10,000 Bigger about 4 times then scroll down and on the right it will show the mouse X and Y position and as you move down through the image and hover over the Red Area if you go down enough it will go into Negative Y.
By Researching i've managed to solve my problem
Dont Use PictureBox inside Panel, Instead use ScrollableControl. This will fix the Problem where i couldn't make something bigger then 65K Height.
Use GetVirtualMouseLocation to get Virtual Mouse Positions
I Just used what was posted here
The Above Works Perfectly.

Categories