16

我的问题是图像加载似乎来自应用程序资源不正确。这是代码:

    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.UriSource = new Uri(@"pack://application:,,,/WpfApplication3;component/Resources/Images/16x16_incorrect.png", UriKind.Absolute);
    bi.EndInit();

    ImageSource s = bi;

图像文件16x16_incorrect.png是 16x16 32bpp PNG 文件,但在执行上述代码后,s.Width = s.Height = 21,59729....我还有另一个文件 - 16x16_correct.png,当它被加载时,ImageSource 的WidthHeight都等于 16,002。

其他图像每个都加载不正确并且看起来模糊(或平滑),因为系统将其从 16x16 拉伸到 21x21。

  • 正确的图像: 正确的图像
  • 图片不正确:图像不正确

这是什么原因造成的?如果源图像文件有问题,我该如何更改ImageSource.Width为所需的大小才能使用这些文件?

4

3 回答 3

21

如果您不想从外部更改 DPI,可以这样做:

public static BitmapSource ConvertBitmapTo96DPI(BitmapImage bitmapImage)
{
    double dpi = 96;
    int width = bitmapImage.PixelWidth;
    int height = bitmapImage.PixelHeight;

    int stride = width * bitmapImage.Format.BitsPerPixel;
    byte[] pixelData = new byte[stride * height];
    bitmapImage.CopyPixels(pixelData, stride, 0);

    return BitmapSource.Create(width, height, dpi, dpi, bitmapImage.Format, null, pixelData, stride);
}

如果您只需要 Image.Source.Width/Height 中的正确值,您可以像我一样执行以下操作:

this.myImage.Tag = new double[] { bitmapImage.DpiX, bitmapImage.DpiY };
this.myImage.Source = bitmapImage;

并像这样调整它的大小:

public static void ResizeImage(Image img, double maxWidth, double maxHeight)
{
    if (img == null || img.Source == null)
        return;

    double srcWidth = img.Source.Width;
    double srcHeight = img.Source.Height;

    // Set your image tag to the sources DPI value for smart resizing if DPI != 96
    if (img.Tag != null && img.Tag.GetType() == typeof(double[]))
    {
        double[] DPI = (double[])img.Tag;
        srcWidth = srcWidth / (96 / DPI[0]);
        srcHeight = srcHeight / (96 / DPI[1]);
    }

    double resizedWidth = srcWidth;
    double resizedHeight = srcHeight;

    double aspect = srcWidth / srcHeight;

    if (resizedWidth > maxWidth)
    {
        resizedWidth = maxWidth;
        resizedHeight = resizedWidth / aspect;
    }
    if (resizedHeight > maxHeight)
    {
        aspect = resizedWidth / resizedHeight;
        resizedHeight = maxHeight;
        resizedWidth = resizedHeight * aspect;
    }

    img.Width = resizedWidth;
    img.Height = resizedHeight;
}
于 2011-04-14T04:31:34.730 回答
12

您需要将图像分辨率设置为 96 DPI(对于不正确的 png,当前为 71.12)。

您可以使用免费的paint.net程序(http://getpaint.net)从图像菜单中选择画布大小并将“分辨率”字段设置为96

于 2010-09-19T15:38:01.433 回答
7

这是因为图像的 DPI。WPF 以 96 dpi 呈现默认值。如果您查看不正确的 png 图像的 dpi。您会看到它设置为 72。这会导致 WPF 将图像缩放到 96 DPI 并保持原始大小。

有两种解决方案。你可以:

  1. 使用例如 XnView 修改 DPI。将其设置为 96。
  2. 将 Width 和 Height 属性设置为 16,将 Stretch 属性设置为 Uniform

    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Image x:Name="MyIncorrectImageFixed" Source="http://i.piccy.info/i5/24/41/504124/16x16_incorrect.png" Width="16" Height="16" Stretch="Uniform"  />
    <Image x:Name="MyIncorrectImage" Source="http://i.piccy.info/i5/24/41/504124/16x16_incorrect.png"  Stretch="None" Grid.Row="1"  />
    <Image x:Name="MyCorrectImage" Source="http://i.piccy.info/i5/22/41/504122/16x16_correct.png" Stretch="None" Grid.Row="2"  />
    

于 2010-09-19T15:33:54.610 回答