This is a migrated thread and some comments may be shown as answers.

Clicking the mouse on the image in RadImageEditor

3 Answers 260 Views
ImageEditor
This is a migrated thread and some comments may be shown as answers.
Andrzej
Top achievements
Rank 2
Andrzej asked on 09 Apr 2019, 11:57 AM

I need coordinates where exactly I clicked the mouse on the loaded image in WPF RadImageEditor (not the coordinator of the editor but the image). Any ideas how to achieve this goal?

Andrzej

3 Answers, 1 is accepted

Sort by
0
Marc
Top achievements
Rank 1
answered on 31 Mar 2021, 02:59 PM
Was a solution ever found for this?
I am facing the same (or at least a very similar) issue.

I am looking to find the exact pixel coordinates of a mouse click on the image loaded within a RadImageEditor.
I need to find the true coordinates of the click on the full image, regardless of zoom-level or panned position.
I have only been able to capture the coordinates of the zoomed in area.

E.g. - If I am zoomed in on the bottom-right corner of an image, and I click on the very top-left of the zoomed-in, visible part of the image, I will be given the coordinates (0,0). However, I am looking to get the coordinates of the click in relation to the overall image as a whole.

If anyone has any information on how to accomplish this, please let me know. It would be extremely helpful.
Using C# WPF MVVM. 
0
Marc
Top achievements
Rank 1
answered on 31 Mar 2021, 02:59 PM
Any luck finding an answer to this issue?
0
Martin Ivanov
Telerik team
answered on 05 Apr 2021, 11:31 AM

Hi,

To achieve this requirement, you can get the Image control that shows the image in the control and use the GetPosition() method to get the actual coordinates.

public MainWindow()
{
	InitializeComponent();
	imageEditor.AddHandler(RadImageEditor.MouseLeftButtonDownEvent, new MouseButtonEventHandler(imageEditor_MouseLeftButtonDown), true);
}

private void imageEditor_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
	var image = e.OriginalSource as Image;
	if (image != null)
	{
		Point mousePosition = e.GetPosition(image);
	}
}

However, note that those coordinates will be based on the Image control size and not the actual size of the image file. If you need the pixel position of the original image instead of the scaled size of the control (in case it is zoomed), you will need to implement some mapping calculations that translate mouse coordinates to image pixels. Here is one way to do this:

private void imageEditor_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
	var image = e.OriginalSource as Image;
	if (image != null)
	{
		Point mousePosition = e.GetPosition(image);
		double relativeWidth = mousePosition.X / image.ActualWidth;
		double relativeHeight = mousePosition.Y / image.ActualHeight;
		
		var originalImageWidth = ((WriteableBitmap)image.Source).PixelWidth;
		var originalImageHeight = ((WriteableBitmap)image.Source).PixelHeight;

		var pixelXPosition = originalImageWidth * relativeWidth;
		var pixelYPosition = originalImageHeight * relativeHeight;
	}
}

I hope this helps.

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
ImageEditor
Asked by
Andrzej
Top achievements
Rank 2
Answers by
Marc
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or