Question

Mon problème est que le chargement d'image semble être uncorrectly de ressources d'application. Ceci est du code:

    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;

Fichier image "16x16_incorrect.png" est 16x16 32bpp fichier PNG, mais après l'exécution de code ci-dessus, s.Width = s.Height = 21,59729 .... J'ai aussi un autre fichier - "16x16_correct.png", quand Je le charge de la même manière, la largeur et la hauteur de la ImageSource sont égales à 16002.

J'ai un gros paquet de PNG utiles 16x16 images 32bpp, que je compte utiliser dans l'interface utilisateur de mes applications. Malheureusement, chacun d'entre eux le chargement de façon incorrecte et semble floue (ou smoothy), parce que le système, il étend de 16x16 à 21x21.

  • image correcte:
  • l'image incorrecte:

Voulez-vous à être si bien vouloir expliquer la solution de ce problème? Si le problème dans les fichiers d'image source, comment puis-je changer ImageSource.Width à la taille désirée afin d'utiliser ces fichiers?

Était-ce utile?

La solution

Vous devez définir la résolution d'image à 96 DPI (actuellement, il est 71,12 pour le .png incorrect).

Vous pouvez le faire en utilisant le programme paint.net gratuit ( http://getpaint.net ) dans le menu Image sélectionnez la taille toile et définir le champ « résolution » à 96

Autres conseils

Si vous ne voulez pas changer DPI, vous pouvez le faire avec l'extérieur ceci:

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);
}

Si vous avez juste besoin de valeurs correctes dans Image.Source.Width / hauteur, vous pouvez faire quelque chose comme je l'ai fait:

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

et la redimensionner comme ceci:

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;
}

Ceci est dû à la DPI des images. WPF rend par défaut avec 96 ppp. Si vous regardez le dpi de l'image incorrecte png. Vous verrez qu'il est réglé sur 72. Cela provoque WPF à l'échelle de l'image à 96 DPI et conserver la taille d'origine.

Il y a deux solutions. Vous pouvez:

  1. Modifier le DPI en utilisant XnView par exemple. Réglez-le à 96.
  2. Définissez la largeur et Height à 16, et la propriété extensible à 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"  />
    

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top