加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WPF图像作为工具提示 – DPI问题

发布时间:2020-12-15 21:03:01 所属栏目:百科 来源:网络整理
导读:我一直在摸不着头脑. 在我的MainWindow上,我有一个图像,谁的工具提示应该弹出图像的实际大小(或者高度不大于MainWindow本身): Image x:Name="ss1" Grid.Column="0" Grid.Row="0" Margin="0" Image.ToolTip ToolTip DataContext="{Binding PlacementTarget,R
我一直在摸不着头脑.

在我的MainWindow上,我有一个图像,谁的工具提示应该弹出图像的实际大小(或者高度不大于MainWindow本身):

<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}" MaxHeight="{Binding ElementName=MW,Path=Height}" Stretch="Uniform" ToolTipService.Placement="Top"/>
            </Border>
        </ToolTip>
    </Image.ToolTip>
</Image>

(MainWindow的x:名称是’MW’)

在别处的另一个类中,我将BitmapImage加载到此图像控件中:

Image img = (Image)mw.FindName("ss1");
img.Source = GetBitmapImageFromDisk(path,UriKind.Absolute);

和GetBitMapImageFromDisk方法:

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′,那么ToolTip图像在显示时会非常小.

任何人都可以提出一种方法来减轻我目前的代码吗?在运行时加载的图像可以是几乎任何大小,DPI和纵横比.

解决方法

非常感谢Clemens的链接,它确实非常有用(特别是pixelwidth和pixelheight属性).

我在xaml中定义最大值时遇到了一些问题,所以最后我将逻辑抽象到后面的代码中.

完整性代码:

XAML:

<Image x:Name="ss1" Grid.Column="0" Grid.Row="0" Margin="0">
    <Image.ToolTip>
        <ToolTip DataContext="{Binding PlacementTarget,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();
    }
}

一旦我能识别像素宽度&图像的高度在ToolTip本身上设置MaxHeight和MaxWidth相当简单.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读