1

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

在此处输入图像描述

所以我写了这段代码:

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

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

myTooltip = img;

这是结果:

在此处输入图像描述

如您所见,右边距和下边距完全不同。为什么?我该如何解决这个问题?

4

1 回答 1

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());
    }
}
于 2012-06-19T18:43:51.787 回答