2016-11-03 14 views
4

私はこれでしばらく頭を傷つけてきました。私はそれが実際のサイズ(またはメインウィンドウ自体よりも大きくない高さ)だ時にツールチップのイメージが画像をポップアップする必要があり持っている私のメインウィンドウでWPF Image to ToolTip - DPIの問題

<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> 

(メインウィンドウの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'と言う画像をターゲットにしているのであれば、ツールヒントの画像は表示されたときに非常に小さくなります。

誰も私の現在のコードでこれを緩和する方法を提案できますか?実行時にロードされるイメージは、ほとんどすべてのサイズ、DPI、およびアスペクト比になります。

+3

おそらく役に立ちます:http://stackoverflow.com/a/644625/1136211 – Clemens

答えて

0

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(); 
    } 
} 

イメージのピクセル幅&の高さを特定できたら、ツールチップ自体にMaxHeightとMaxWidthを設定するのはかなり簡単でした。