WPF Image Dynamisch wechselnde Bildquelle zur Laufzeit

Ich habe ein Fenster mit einem Titel. Wenn der Benutzer eine Auswahl aus einer Dropdown-Liste auswählt, kann sich das Titelbild ändern. Das Problem ist, dass das Bild beim Laden unscharf, gestreckt und pixelig ist. Dies sind PNG-Dateien, mit denen ich arbeite, und sie sehen gut aus, bevor die Quelle dynamisch festgelegt wird.

Hier ist der Code, mit dem ich die Quelle des Bildes ändere.

string strUri2 = String.Format(@"pack://application:,,,/MyAssembly;component/resources/main titles/{0}", CurrenSelection.TitleImage);
Stream iconStream2 = App.GetResourceStream(new Uri(strUri2)).Stream;
imgTitle.Source = HelperFunctions.returnImage(iconStream2);

Hier sind die Hilfsfunktionen.

    public static BitmapImage returnImage(Stream iconStream)
    {
        Bitmap brush = new Bitmap(iconStream);
        System.Drawing.Image img = brush.GetThumbnailImage(brush.Height, brush.Width, null, System.IntPtr.Zero);
        var imgbrush = new BitmapImage();
        imgbrush.BeginInit();
        imgbrush.StreamSource = ConvertImageToMemoryStream(img);
        imgbrush.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
        imgbrush.EndInit();
        var ib = new ImageBrush(imgbrush);
        return imgbrush;
    }

    public static MemoryStream ConvertImageToMemoryStream(System.Drawing.Image img)
    {
        var ms = new MemoryStream();
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        return ms;
    }

Und die XAML

            <Image x:Name="imgTitle" HorizontalAlignment="Left" VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="1" Stretch="None" d:IsLocked="False"/>

Und für Ref:

xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

Hat jemand eine Idee, was los ist?

Antworten auf die Frage(12)

Ihre Antwort auf die Frage