WPF -转换点,考虑了Windows比例因子



我写了一个小的测试应用程序,其中我有一个图像,上面有一个画布。这个想法是,用户将能够"绘制"在画布上。我有一张宽839像素,高396像素的图片。当我在画布上移动鼠标时,我编写了代码来捕获鼠标位置,在画布中使用e.GetPosition(this)。

问题是,当我接近图像/画布的右下角时,我回到位置559,264,而不是位置839,396。换句话说,我得到的值按1.5缩放,这是我的Windows缩放因子。

我试过myCanvas.TranslatePoint()和myCanvas.PointFromScreen()和myCanvas.PointToScreen()。我不想要一个相对于屏幕的点;我想要相对于图像的点,而不是考虑Windows缩放因子。

如果我简单地将这些值乘以1.5,我得到了我正在寻找的位图相对点,但当然这不是一个解决方案。

是否有"windows批准的";要做到这一点,需要从注册表中读取比例因子并手动应用它吗?我不是在UWP,所以我不能使用windows . ui . viewmanagement . uisets(至少,我认为我不能)。我需要这个工作在Windows 10和Windows 11。

有人能帮忙吗?

编辑:我找不到一种方法来附加我的解决方案这个问题,但我的全部代码如下所示。唯一缺少的是PNG文件,但实际上任何图像都给出相同的结果。

<Window x:Class="StackOverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:StackOverflow"
mc:Ignorable="d"
Title="MainWindow"
Height="500"
Width="900">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Image x:Name="BackgroundImage"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Stretch="None"
Source="/BingMap.PNG"
MouseMove="BackgroundImage_MouseMove"/>
<TextBlock x:Name="TB"
Grid.Row="2" />
</Grid>
</Window>
using System.Windows;
using System.Windows.Input;
namespace StackOverflow
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void BackgroundImage_MouseMove(object sender, MouseEventArgs e)
{
var pt = e.GetPosition(this);
TB.Text = $"{pt.X:n0}, {pt.Y:n0}";
}
}
}

图像文件的DPI肯定不同于WPF的设备无关像素使用的标准96。光栅图像显示的原生、未拉伸大小由其像素尺寸乘以96/DPI决定。

您可以简单地通过将图像元素的WidthHeight绑定到其SourcePixelWidthPixelHeight来解决这个问题。

<Image Source="..."
Width="{Binding Source.PixelWidth, RelativeSource={RelativeSource Self}}"
Height="{Binding Source.PixelHeight, RelativeSource={RelativeSource Self}}"/>

最新更新