SilverBrother
SilverBrother
CC#
Created by SilverBrother on 2/5/2024 in #help
WPF mousePos and icon.Margin align. Visual rep does not.
So I'm trying to make an icon visible in a map at the same position as the mouseclick, but it always appears somewhere in the bottom right quadrant. What am I not seeing? It is like there is a layer askewed to the map .Here's the xaml bit
<Viewbox x:Name="ViewMap" MouseLeftButtonDown="Viewbox_MouseLeftButtonDown" Stretch="Uniform">
<!-- Your image display content -->
<Image x:Name="imageControl" MouseWheel="ImageControl_MouseWheel" Loaded="ImageControl_Loaded" Stretch="Uniform">
</Image>
</Viewbox>
<Viewbox x:Name="ViewMap" MouseLeftButtonDown="Viewbox_MouseLeftButtonDown" Stretch="Uniform">
<!-- Your image display content -->
<Image x:Name="imageControl" MouseWheel="ImageControl_MouseWheel" Loaded="ImageControl_Loaded" Stretch="Uniform">
</Image>
</Viewbox>
There's only a grid and a page as ancestors. No attributes on the grid. And here are the C# code
C#
private void Viewbox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Get the cursor position relative to the mainGrid
Point cursorPosition = e.GetPosition(imageControl);
Debug.WriteLine($"{cursorPosition}");
// Show the icon at the cursor position
ShowIcon(calibrationIcon, cursorPosition);
}
private void ShowIcon(Image icon, Point position)
{
icon.Margin = new Thickness(position.X - icon.ActualWidth / 2, position.Y - icon.ActualHeight / 2, 0, 0);
Debug.WriteLine($"{icon.Margin}");
// Show the icon
icon.Visibility = Visibility.Visible;

}
C#
private void Viewbox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Get the cursor position relative to the mainGrid
Point cursorPosition = e.GetPosition(imageControl);
Debug.WriteLine($"{cursorPosition}");
// Show the icon at the cursor position
ShowIcon(calibrationIcon, cursorPosition);
}
private void ShowIcon(Image icon, Point position)
{
icon.Margin = new Thickness(position.X - icon.ActualWidth / 2, position.Y - icon.ActualHeight / 2, 0, 0);
Debug.WriteLine($"{icon.Margin}");
// Show the icon
icon.Visibility = Visibility.Visible;

}
Let me know if you need more for this to make sense. Thank you so much in advance.
2 replies
CC#
Created by SilverBrother on 1/26/2024 in #help
WPF Image Zoom fixed to absolute window position and not relative mouseposition
- The title explains the issue briefly. So I'm making a simple App with WPF where I want to be able to zoom in and out on images. When I zoom in on the first mouse position it does as intended, but if I move the mouse to a different position it is like the program assumes that it is zooming in on the mouse position related to the original unzoomed image, instead of the position on the zoomed image. I.e. I have a Europe map and first step I zoom in on Berlin without moving the mouse, second step, I now want to zoom in on a street in the south western Berlin, but instead the App zooms (pans) down to Spain due to the cursor's position in the original image. I hope it makes sense. I don't think there are issues in the WPF, but I'm missing something in the C# file. Here it is
C#
public partial class MainWindow : Window
{
private double zoomScale = 1.0;

public MainWindow()
{
InitializeComponent();
}
private void ZoomedImage_MouseWheel(object sender, MouseWheelEventArgs e)
{
// Get the mouse position relative to the image
Point mousePos = e.GetPosition(imagemap);

// Adjust the zoom scale based on the mouse wheel delta
double delta = e.Delta > 0 ? 0.1 : -0.1;
zoomScale += delta;

// Constrain the zoom scale within reasonable limits
zoomScale = Math.Max(1.0, Math.Min(zoomScale, 10.0));

// Apply the zoom scale using a ScaleTransform
ScaleTransform scaleTransform = new ScaleTransform(zoomScale, zoomScale);

// Set the center of the ScaleTransform to the mouse position
scaleTransform.CenterX = mousePos.X;
scaleTransform.CenterY = mousePos.Y;

imagemap.RenderTransform = scaleTransform;

e.Handled = true; // Prevent scrolling
}
}
C#
public partial class MainWindow : Window
{
private double zoomScale = 1.0;

public MainWindow()
{
InitializeComponent();
}
private void ZoomedImage_MouseWheel(object sender, MouseWheelEventArgs e)
{
// Get the mouse position relative to the image
Point mousePos = e.GetPosition(imagemap);

// Adjust the zoom scale based on the mouse wheel delta
double delta = e.Delta > 0 ? 0.1 : -0.1;
zoomScale += delta;

// Constrain the zoom scale within reasonable limits
zoomScale = Math.Max(1.0, Math.Min(zoomScale, 10.0));

// Apply the zoom scale using a ScaleTransform
ScaleTransform scaleTransform = new ScaleTransform(zoomScale, zoomScale);

// Set the center of the ScaleTransform to the mouse position
scaleTransform.CenterX = mousePos.X;
scaleTransform.CenterY = mousePos.Y;

imagemap.RenderTransform = scaleTransform;

e.Handled = true; // Prevent scrolling
}
}
2 replies
CC#
Created by SilverBrother on 1/18/2024 in #help
WPF Mouse Position and Visible dot not the same place
No description
4 replies