-->

如何正确使用图片作为工具提示?(How to properly use Image as ToolT

2019-10-16 20:55发布

我有一个BitmapSource 1690x214(使用从EMF文件中获取此代码),我想用这个形象ToolTip 。 这是使用画图显示的图像:

所以我写了下面的代码:

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

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

myTooltip = img;

这是结果:

正如你所看到的,右侧和底部边距是完全地不同。 为什么? 我怎样才能解决这个问题?

Answer 1:

这似乎是一个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());
    }
}


文章来源: How to properly use Image as ToolTip?