C# WPF图像作为工具提示-DPI问题
我为这个挠头已有一段时间了 在我的主窗口上,我有一个图像,其工具提示应按实际大小(或高度不大于主窗口本身)弹出图像: 以及GetBitMapImageFromDisk方法:C# WPF图像作为工具提示-DPI问题,c#,wpf,C#,Wpf,我为这个挠头已有一段时间了 在我的主窗口上,我有一个图像,其工具提示应按实际大小(或高度不大于主窗口本身)弹出图像: 以及GetBitMapImageFromDisk方法: public static BitmapImage GetBitmapImageFromDisk(string path, UriKind urikind) { if (!File.Exists(path)) return null; try { BitmapImage
public static BitmapImage GetBitmapImageFromDisk(string path, UriKind urikind)
{
if (!File.Exists(path))
return null;
try
{
BitmapImage b = new BitmapImage(new Uri(path, urikind));
return b;
}
catch (System.NotSupportedException ex)
{
BitmapImage c = new BitmapImage();
return c;
}
}
鼠标悬停时会弹出图像工具提示,但问题是图像的大小似乎取决于图像本身的DPI。因此,如果出于某种原因,它以DPI为“762”的图像为目标,那么工具提示图像在显示时非常小
有人能用我当前的代码建议一种缓解这种情况的方法吗?运行时加载的图像几乎可以是任何大小、DPI和纵横比。多亏了Clemens的链接,它确实非常有用(特别是pixelwidth和pixelheight属性) 我在xaml中定义最大值时遇到了一些问题,所以最后我将逻辑抽象到代码中 完整性代码: XAML:
<Image x:Name="ss1" Grid.Column="0" Grid.Row="0" Margin="0">
<Image.ToolTip>
<ToolTip DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
<Border BorderBrush="Black" BorderThickness="1" Margin="5,7,5,5">
<Image Source="{Binding Source}" Stretch="Uniform" ToolTipService.Placement="Top"/>
</Border>
</ToolTip>
</Image.ToolTip>
</Image>
Image img = (Image)mw.FindName("ss1");
SetImage(img, path, UriKind.Absolute);
public static void SetImage(Image img, string path, UriKind urikind)
{
if (!File.Exists(path))
return;
try
{
// load content into the image
BitmapImage b = new BitmapImage(new Uri(path, urikind));
img.Source = b;
// get actual pixel dimensions of image
double pixelWidth = (img.Source as BitmapSource).PixelWidth;
double pixelHeight = (img.Source as BitmapSource).PixelHeight;
// get dimensions of main window
MainWindow mw = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
double windowWidth = mw.ActualWidth;
double windowHeight = mw.ActualHeight;
// set max dimensions on Image.ToolTip
ToolTip tt = (ToolTip)img.ToolTip;
tt.MaxHeight = windowHeight / 1.1;
tt.MaxWidth = windowWidth / 1.1;
img.ToolTip = tt;
}
catch (System.NotSupportedException ex)
{
img.Source = new BitmapImage();
}
}
方法:
<Image x:Name="ss1" Grid.Column="0" Grid.Row="0" Margin="0">
<Image.ToolTip>
<ToolTip DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
<Border BorderBrush="Black" BorderThickness="1" Margin="5,7,5,5">
<Image Source="{Binding Source}" Stretch="Uniform" ToolTipService.Placement="Top"/>
</Border>
</ToolTip>
</Image.ToolTip>
</Image>
Image img = (Image)mw.FindName("ss1");
SetImage(img, path, UriKind.Absolute);
public static void SetImage(Image img, string path, UriKind urikind)
{
if (!File.Exists(path))
return;
try
{
// load content into the image
BitmapImage b = new BitmapImage(new Uri(path, urikind));
img.Source = b;
// get actual pixel dimensions of image
double pixelWidth = (img.Source as BitmapSource).PixelWidth;
double pixelHeight = (img.Source as BitmapSource).PixelHeight;
// get dimensions of main window
MainWindow mw = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
double windowWidth = mw.ActualWidth;
double windowHeight = mw.ActualHeight;
// set max dimensions on Image.ToolTip
ToolTip tt = (ToolTip)img.ToolTip;
tt.MaxHeight = windowHeight / 1.1;
tt.MaxWidth = windowWidth / 1.1;
img.ToolTip = tt;
}
catch (System.NotSupportedException ex)
{
img.Source = new BitmapImage();
}
}
publicstaticvoidsetimage(图像img、字符串路径、UriKind-UriKind)
{
如果(!File.Exists(path))
返回;
尝试
{
//将内容加载到图像中
BitmapImage b=新的BitmapImage(新Uri(路径,urikind));
img.Source=b;
//获取图像的实际像素尺寸
双像素宽度=(img.Source作为位图源)。像素宽度;
双像素高度=(img.Source作为位图源)。像素高度;
//获取主窗口的尺寸
MainWindow mw=Application.Current.Windows.OfType().FirstOrDefault();
双窗宽度=mw.实际宽度;
双窗高度=mw.实际高度;
//设置Image.ToolTip上的最大尺寸
工具提示tt=(工具提示)img.ToolTip;
tt.MaxHeight=窗高/1.1;
tt.MaxWidth=窗宽/1.1;
img.ToolTip=tt;
}
捕获(System.NotSupportedException ex)
{
img.Source=新的BitmapImage();
}
}
一旦我能够识别图像的像素宽度和高度,在工具提示上设置MaxHeight和MaxWidth就相当简单了。可能会有帮助: