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は16x1632bppPNGファイルですが、上記のコードを実行した後、s.Width = s.Height = 21,59729....別のファイルもあります-16x16_correct.pngそれがロードされると、ImageSourceWidthHeight16,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

画像の解像度を96DPIに設定する必要があります(現在、間違ったpngの場合は71.12です)。

無料のpaint.netプログラム(http://getpaint.net)を使用して、[画像]メニューから[キャンバスサイズ]を選択し、[解像度]フィールドを96に設定して行うことができます。

于 2010-09-19T15:38:01.433 に答える
7

これは、画像のDPIが原因です。WPFは、デフォルトで96dpiをレンダリングします。間違ったpng画像のdpiを見ると。72に設定されていることがわかります。これにより、WPFは画像を96 DPIに拡大縮小し、元のサイズを維持します。

2つの解決策があります。あなたはできる:

  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 に答える