2012-06-19 135 views
1

我有一個BitmapSource 1690x214(使用this代碼從EMF文件中提取),我想將此圖像用作ToolTip。這是圖像使用畫圖顯示:如何正確使用圖像作爲工具提示?

enter image description here

所以我寫了這個代碼:

BitmapSource bmp = myBitmapSource; // "Dk01Light.EMF" 

Image img = new Image() 
{ 
    Source = bmp, 
    Width = bmp.Width, 
    Height = bmp.Height, 
    Stretch = Stretch.Uniform, 
}; 

myTooltip = img; 

這是結果:

enter image description here

正如你所看到的,右邊和底部的保證金完全不同。爲什麼?我該如何解決這個問題?

回答

2

這似乎是一個DPI問題。首先嚐試從圖像初始值設定項中移除寬度和高度。它也應該適合其內容。

你也可以嘗試用以下進行更換您鏈接到該代碼確保正在正確產生的圖像:

using (System.Drawing.Imaging.Metafile emf = new System.Drawing.Imaging.Metafile(path)) 
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(emf.Width, emf.Height)) 
{ 
    bmp.SetResolution(emf.HorizontalResolution, emf.VerticalResolution); 

    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp)) 
    { 
     g.DrawImage(emf, 
      new Rectangle(0, 0, emf.Width, emf.Height), 
      new Rectangle(0, 0, emf.Width, emf.Height), 
      GraphicsUnit.Pixel 
     ); 

     return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 
    } 
} 
+0

我只需添加'bmp.SetResolution(emf.Horizo​​ntalResolution,emf.VerticalResolution); '它的工作! – Nick