Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何正确使用图像作为工具提示?_C#_Wpf_Image_Tooltip_Bitmapsource - Fatal编程技术网

C# 如何正确使用图像作为工具提示?

C# 如何正确使用图像作为工具提示?,c#,wpf,image,tooltip,bitmapsource,C#,Wpf,Image,Tooltip,Bitmapsource,我有一个BitmapSource1690x214(使用代码从EMF文件中获取),我想将此图像用作工具提示。这是使用Paint显示的图像: 所以我写了这段代码: BitmapSource bmp = myBitmapSource; // "Dk01Light.EMF" Image img = new Image() { Source = bmp, Width = bmp.Width, Height = bmp.Height, Stretch = Stretch.

我有一个
BitmapSource
1690x214(使用代码从EMF文件中获取),我想将此图像用作
工具提示。这是使用Paint显示的图像:

所以我写了这段代码:

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

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

myTooltip = img;
这就是结果:


正如您所看到的,右边距和底边距完全不同。为什么?如何解决此问题?

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

我只添加了
bmp.SetResolution(emf.HorizontalResolution,emf.VerticalResolution)并且它工作!